Debian Codegraph @

vendor/wikimedia/remex-html/src/Tokenizer/LazyAttributes.php

raw
1 <?php
2
3 namespace Wikimedia\RemexHtml\Tokenizer;
4
5 /**
6 * An Attributes implementation which defers interpretation of regex match
7 * results until the caller requires them.
8 *
9 * This should not be directly instantiated outside of Tokenizer.
10 */
11 class LazyAttributes implements Attributes {
12 /** @var callable */
13 private $interpreter;
14
15 /** @var mixed */
16 private $data;
17
18 /** @var string[] */
19 private $attributes;
20
21 /** @var Attribute[] */
22 private $attrObjects;
23
24 /**
25 * @param mixed $data
26 * @param callable $interpreter
27 */
28 public function __construct( $data, callable $interpreter ) {
29 $this->interpreter = $interpreter;
30 $this->data = $data;
31 }
32
33 /**
34 * Initialize the attributes array
35 */
36 private function init() {
37 if ( $this->attributes === null ) {
38 $func = $this->interpreter;
39 $this->attributes = $func( $this->data );
40 // @phan-suppress-next-line PhanTypeMismatchPropertyProbablyReal
41 $this->interpreter = null;
42 $this->data = null;
43 }
44 }
45
46 /** @inheritDoc */
47 public function offsetExists( $offset ): bool {
48 if ( $this->attributes === null ) {
49 $this->init();
50 }
51 return isset( $this->attributes[$offset] );
52 }
53
54 /** @inheritDoc */
55 public function &offsetGet( $offset ): string {
56 if ( $this->attributes === null ) {
57 $this->init();
58 }
59 return $this->attributes[$offset];
60 }
61
62 /** @inheritDoc */
63 public function offsetSet( $offset, $value ): void {
64 if ( $this->attributes === null ) {
65 $this->init();
66 }
67 $this->attributes[$offset] = $value;
68 if ( $this->attrObjects !== null ) {
69 $this->attrObjects[$offset] = new Attribute( $offset, null, null, $offset, $value );
70 }
71 }
72
73 /** @inheritDoc */
74 public function offsetUnset( $offset ): void {
75 if ( $this->attributes === null ) {
76 $this->init();
77 }
78 unset( $this->attributes[$offset] );
79 unset( $this->attrObjects[$offset] );
80 }
81
82 /** @inheritDoc */
83 public function getValues() {
84 if ( $this->attributes === null ) {
85 $this->init();
86 }
87 return $this->attributes;
88 }
89
90 /** @inheritDoc */
91 public function getObjects() {
92 if ( $this->attrObjects === null ) {
93 if ( $this->attributes === null ) {
94 $this->init();
95 }
96 $result = [];
97 foreach ( $this->attributes as $name => $value ) {
98 $result[$name] = new Attribute( $name, null, null, $name, $value );
99 }
100 $this->attrObjects = $result;
101 }
102 return $this->attrObjects;
103 }
104
105 /** @inheritDoc */
106 public function count(): int {
107 if ( $this->attributes === null ) {
108 return count( $this->data );
109 }
110 return count( $this->attributes );
111 }
112
113 public function getIterator(): \ArrayIterator {
114 if ( $this->attributes === null ) {
115 $this->init();
116 }
117 return new \ArrayIterator( $this->attributes );
118 }
119
120 /** @inheritDoc */
121 public function merge( Attributes $other ) {
122 if ( $this->attributes === null ) {
123 $this->init();
124 }
125 foreach ( $other as $name => $value ) {
126 if ( !isset( $this->attributes[$name] ) ) {
127 $this->attributes[$name] = $value;
128 }
129 }
130 }
131
132 /** @inheritDoc */
133 public function clone() {
134 return $this;
135 }
136 }