| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Sabberworm\CSS\Property; |
| 6 |
|
| 7 |
use Sabberworm\CSS\Comment\CommentContainer; |
| 8 |
use Sabberworm\CSS\OutputFormat; |
| 9 |
use Sabberworm\CSS\Position\Position; |
| 10 |
use Sabberworm\CSS\Position\Positionable; |
| 11 |
use Sabberworm\CSS\ShortClassNameProvider; |
| 12 |
use Sabberworm\CSS\Value\URL; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class representing an `@import` rule. |
| 16 |
*/ |
| 17 |
class Import implements AtRule, Positionable |
| 18 |
{ |
| 19 |
use CommentContainer; |
| 20 |
use Position; |
| 21 |
use ShortClassNameProvider; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var URL |
| 25 |
*/ |
| 26 |
private $location; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var string|null |
| 30 |
*/ |
| 31 |
private $mediaQuery; |
| 32 |
|
| 33 |
/** |
| 34 |
* @param int<1, max>|null $lineNumber |
| 35 |
*/ |
| 36 |
public function __construct(URL $location, ?string $mediaQuery, ?int $lineNumber = null) |
| 37 |
{ |
| 38 |
$this->location = $location; |
| 39 |
$this->mediaQuery = $mediaQuery; |
| 40 |
$this->setPosition($lineNumber); |
| 41 |
} |
| 42 |
|
| 43 |
public function setLocation(URL $location): void |
| 44 |
{ |
| 45 |
$this->location = $location; |
| 46 |
} |
| 47 |
|
| 48 |
public function getLocation(): URL |
| 49 |
{ |
| 50 |
return $this->location; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @return non-empty-string |
| 55 |
*/ |
| 56 |
public function render(OutputFormat $outputFormat): string |
| 57 |
{ |
| 58 |
return $outputFormat->getFormatter()->comments($this) . '@import ' . $this->location->render($outputFormat) |
| 59 |
. ($this->mediaQuery === null ? '' : ' ' . $this->mediaQuery) . ';'; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @return non-empty-string |
| 64 |
*/ |
| 65 |
public function atRuleName(): string |
| 66 |
{ |
| 67 |
return 'import'; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @return array{0: URL, 1?: non-empty-string} |
| 72 |
*/ |
| 73 |
public function atRuleArgs(): array |
| 74 |
{ |
| 75 |
$result = [$this->location]; |
| 76 |
if (\is_string($this->mediaQuery) && $this->mediaQuery !== '') { |
| 77 |
$result[] = $this->mediaQuery; |
| 78 |
} |
| 79 |
|
| 80 |
return $result; |
| 81 |
} |
| 82 |
|
| 83 |
public function getMediaQuery(): ?string |
| 84 |
{ |
| 85 |
return $this->mediaQuery; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @return array<string, bool|int|float|string|array<mixed>|null> |
| 90 |
* |
| 91 |
* @internal |
| 92 |
*/ |
| 93 |
public function getArrayRepresentation(): array |
| 94 |
{ |
| 95 |
return [ |
| 96 |
'class' => $this->getShortClassName(), |
| 97 |
// We're using the term "uri" here to match the wording used in the specs: |
| 98 |
// https://www.w3.org/TR/CSS22/cascade.html#at-import |
| 99 |
'uri' => $this->location->getArrayRepresentation(), |
| 100 |
]; |
| 101 |
} |
| 102 |
} |