Debian Codegraph @

vendor/sabberworm/php-css-parser/src/Value/URL.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Sabberworm\CSS\Value;
6
7 use Sabberworm\CSS\OutputFormat;
8 use Sabberworm\CSS\Parsing\ParserState;
9 use Sabberworm\CSS\Parsing\SourceException;
10 use Sabberworm\CSS\Parsing\UnexpectedEOFException;
11 use Sabberworm\CSS\Parsing\UnexpectedTokenException;
12 use Sabberworm\CSS\ShortClassNameProvider;
13
14 /**
15 * This class represents URLs in CSS. `URL`s always output in `URL("")` notation.
16 */
17 class URL extends PrimitiveValue
18 {
19 use ShortClassNameProvider;
20
21 /**
22 * @var CSSString
23 */
24 private $url;
25
26 /**
27 * @param int<1, max>|null $lineNumber
28 */
29 public function __construct(CSSString $url, ?int $lineNumber = null)
30 {
31 parent::__construct($lineNumber);
32 $this->url = $url;
33 }
34
35 /**
36 * @throws SourceException
37 * @throws UnexpectedEOFException
38 * @throws UnexpectedTokenException
39 *
40 * @internal since V8.8.0
41 */
42 public static function parse(ParserState $parserState): URL
43 {
44 $anchor = $parserState->anchor();
45 $identifier = '';
46 for ($i = 0; $i < 3; $i++) {
47 $character = $parserState->parseCharacter(true);
48 if ($character === null) {
49 break;
50 }
51 $identifier .= $character;
52 }
53 $useUrl = $parserState->streql($identifier, 'url');
54 if ($useUrl) {
55 $parserState->consumeWhiteSpace();
56 $parserState->consume('(');
57 } else {
58 $anchor->backtrack();
59 }
60 $parserState->consumeWhiteSpace();
61 $result = new URL(CSSString::parse($parserState), $parserState->currentLine());
62 if ($useUrl) {
63 $parserState->consumeWhiteSpace();
64 $parserState->consume(')');
65 }
66 return $result;
67 }
68
69 public function setURL(CSSString $url): void
70 {
71 $this->url = $url;
72 }
73
74 public function getURL(): CSSString
75 {
76 return $this->url;
77 }
78
79 /**
80 * @return non-empty-string
81 */
82 public function render(OutputFormat $outputFormat): string
83 {
84 return "url({$this->url->render($outputFormat)})";
85 }
86
87 /**
88 * @return array<string, bool|int|float|string|array<mixed>|null>
89 *
90 * @internal
91 */
92 public function getArrayRepresentation(): array
93 {
94 return [
95 'class' => $this->getShortClassName(),
96 // We're using the term "uri" here to match the wording used in the specs:
97 // https://www.w3.org/TR/CSS22/syndata.html#uri
98 'uri' => $this->url->getArrayRepresentation(),
99 ];
100 }
101 }