Debian Codegraph @

vendor/sabberworm/php-css-parser/src/Property/Charset.php

raw
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
14 /**
15 * Class representing an `@charset` rule.
16 *
17 * The following restrictions apply:
18 * - May not be found in any CSSList other than the Document.
19 * - May only appear at the very top of a Document’s contents.
20 * - Must not appear more than once.
21 */
22 class Charset implements AtRule, Positionable
23 {
24 use CommentContainer;
25 use Position;
26 use ShortClassNameProvider;
27
28 /**
29 * @var CSSString
30 */
31 private $charset;
32
33 /**
34 * @param int<1, max>|null $lineNumber
35 */
36 public function __construct(CSSString $charset, ?int $lineNumber = null)
37 {
38 $this->charset = $charset;
39 $this->setPosition($lineNumber);
40 }
41
42 /**
43 * @param string|CSSString $charset
44 */
45 public function setCharset($charset): void
46 {
47 $charset = $charset instanceof CSSString ? $charset : new CSSString($charset);
48 $this->charset = $charset;
49 }
50
51 public function getCharset(): string
52 {
53 return $this->charset->getString();
54 }
55
56 /**
57 * @return non-empty-string
58 */
59 public function render(OutputFormat $outputFormat): string
60 {
61 return "{$outputFormat->getFormatter()->comments($this)}@charset {$this->charset->render($outputFormat)};";
62 }
63
64 /**
65 * @return non-empty-string
66 */
67 public function atRuleName(): string
68 {
69 return 'charset';
70 }
71
72 public function atRuleArgs(): CSSString
73 {
74 return $this->charset;
75 }
76
77 /**
78 * @return array<string, bool|int|float|string|array<mixed>|null>
79 *
80 * @internal
81 */
82 public function getArrayRepresentation(): array
83 {
84 return [
85 'class' => $this->getShortClassName(),
86 'charset' => $this->charset->getArrayRepresentation(),
87 ];
88 }
89 }