| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sabberworm\CSS\Property; |
| 4 |
|
| 5 |
use Sabberworm\CSS\Comment\Comment; |
| 6 |
use Sabberworm\CSS\OutputFormat; |
| 7 |
use Sabberworm\CSS\Position\Position; |
| 8 |
use Sabberworm\CSS\Position\Positionable; |
| 9 |
use Sabberworm\CSS\Value\CSSString; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class representing an `@charset` rule. |
| 13 |
* |
| 14 |
* The following restrictions apply: |
| 15 |
* - May not be found in any CSSList other than the Document. |
| 16 |
* - May only appear at the very top of a Document’s contents. |
| 17 |
* - Must not appear more than once. |
| 18 |
*/ |
| 19 |
class Charset implements AtRule, Positionable |
| 20 |
{ |
| 21 |
use Position; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var CSSString |
| 25 |
*/ |
| 26 |
private $oCharset; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var int |
| 30 |
* |
| 31 |
* @internal since 8.8.0 |
| 32 |
*/ |
| 33 |
protected $iLineNo; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var array<array-key, Comment> |
| 37 |
* |
| 38 |
* @internal since 8.8.0 |
| 39 |
*/ |
| 40 |
protected $aComments; |
| 41 |
|
| 42 |
/** |
| 43 |
* @param CSSString $oCharset |
| 44 |
* @param int $iLineNo |
| 45 |
*/ |
| 46 |
public function __construct(CSSString $oCharset, $iLineNo = 0) |
| 47 |
{ |
| 48 |
$this->oCharset = $oCharset; |
| 49 |
$this->setPosition($iLineNo); |
| 50 |
$this->aComments = []; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @param string|CSSString $oCharset |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public function setCharset($sCharset) |
| 59 |
{ |
| 60 |
$sCharset = $sCharset instanceof CSSString ? $sCharset : new CSSString($sCharset); |
| 61 |
$this->oCharset = $sCharset; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
public function getCharset() |
| 68 |
{ |
| 69 |
return $this->oCharset->getString(); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* @return string |
| 74 |
* |
| 75 |
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead. |
| 76 |
*/ |
| 77 |
public function __toString() |
| 78 |
{ |
| 79 |
return $this->render(new OutputFormat()); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @param OutputFormat|null $oOutputFormat |
| 84 |
* |
| 85 |
* @return string |
| 86 |
*/ |
| 87 |
public function render($oOutputFormat) |
| 88 |
{ |
| 89 |
return "{$oOutputFormat->comments($this)}@charset {$this->oCharset->render($oOutputFormat)};"; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* @return string |
| 94 |
*/ |
| 95 |
public function atRuleName() |
| 96 |
{ |
| 97 |
return 'charset'; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @return string |
| 102 |
*/ |
| 103 |
public function atRuleArgs() |
| 104 |
{ |
| 105 |
return $this->oCharset; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @param array<array-key, Comment> $aComments |
| 110 |
* |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
public function addComments(array $aComments) |
| 114 |
{ |
| 115 |
$this->aComments = array_merge($this->aComments, $aComments); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @return array<array-key, Comment> |
| 120 |
*/ |
| 121 |
public function getComments() |
| 122 |
{ |
| 123 |
return $this->aComments; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @param array<array-key, Comment> $aComments |
| 128 |
* |
| 129 |
* @return void |
| 130 |
*/ |
| 131 |
public function setComments(array $aComments) |
| 132 |
{ |
| 133 |
$this->aComments = $aComments; |
| 134 |
} |
| 135 |
} |
| 136 |
|