| 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\CSSString; |
| 13 |
use Sabberworm\CSS\Value\URL; |
| 14 |
|
| 15 |
/** |
| 16 |
* `CSSNamespace` represents an `@namespace` rule. |
| 17 |
*/ |
| 18 |
class CSSNamespace implements AtRule, Positionable |
| 19 |
{ |
| 20 |
use CommentContainer; |
| 21 |
use Position; |
| 22 |
use ShortClassNameProvider; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var CSSString|URL |
| 26 |
*/ |
| 27 |
private $url; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var string|null |
| 31 |
*/ |
| 32 |
private $prefix; |
| 33 |
|
| 34 |
/** |
| 35 |
* @param CSSString|URL $url |
| 36 |
* @param int<1, max>|null $lineNumber |
| 37 |
*/ |
| 38 |
public function __construct($url, ?string $prefix = null, ?int $lineNumber = null) |
| 39 |
{ |
| 40 |
$this->url = $url; |
| 41 |
$this->prefix = $prefix; |
| 42 |
$this->setPosition($lineNumber); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* @return non-empty-string |
| 47 |
*/ |
| 48 |
public function render(OutputFormat $outputFormat): string |
| 49 |
{ |
| 50 |
return '@namespace ' . ($this->prefix === null ? '' : $this->prefix . ' ') |
| 51 |
. $this->url->render($outputFormat) . ';'; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @return CSSString|URL |
| 56 |
*/ |
| 57 |
public function getUrl() |
| 58 |
{ |
| 59 |
return $this->url; |
| 60 |
} |
| 61 |
|
| 62 |
public function getPrefix(): ?string |
| 63 |
{ |
| 64 |
return $this->prefix; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @param CSSString|URL $url |
| 69 |
*/ |
| 70 |
public function setUrl($url): void |
| 71 |
{ |
| 72 |
$this->url = $url; |
| 73 |
} |
| 74 |
|
| 75 |
public function setPrefix(string $prefix): void |
| 76 |
{ |
| 77 |
$this->prefix = $prefix; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @return non-empty-string |
| 82 |
*/ |
| 83 |
public function atRuleName(): string |
| 84 |
{ |
| 85 |
return 'namespace'; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @return array{0: CSSString|URL|non-empty-string, 1?: CSSString|URL} |
| 90 |
*/ |
| 91 |
public function atRuleArgs(): array |
| 92 |
{ |
| 93 |
$result = [$this->url]; |
| 94 |
if (\is_string($this->prefix) && $this->prefix !== '') { |
| 95 |
\array_unshift($result, $this->prefix); |
| 96 |
} |
| 97 |
return $result; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @return array<string, bool|int|float|string|array<mixed>|null> |
| 102 |
* |
| 103 |
* @internal |
| 104 |
*/ |
| 105 |
public function getArrayRepresentation(): array |
| 106 |
{ |
| 107 |
return [ |
| 108 |
'class' => $this->getShortClassName(), |
| 109 |
// We're using `uri` here instead of `url` to better match the spec. |
| 110 |
'uri' => $this->url->getArrayRepresentation(), |
| 111 |
'prefix' => $this->prefix, |
| 112 |
]; |
| 113 |
} |
| 114 |
} |