]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/wikimedia/remex-html/RemexHtml/Tokenizer/LazyAttributes.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / wikimedia / remex-html / RemexHtml / Tokenizer / LazyAttributes.php
1 <?php
2
3 namespace 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         private $tokenizer;
13         private $data;
14         private $attributes;
15         private $attrObjects;
16
17         public function __construct( $data, callable $interpreter ) {
18                 $this->interpreter = $interpreter;
19                 $this->data = $data;
20         }
21
22         private function init() {
23                 if ( $this->attributes === null ) {
24                         $func = $this->interpreter;
25                         $this->attributes = $func( $this->data );
26                         $this->interpreter = null;
27                 }
28         }
29
30         public function offsetExists( $offset ) {
31                 if ( $this->attributes === null ) {
32                         $this->init();
33                 }
34                 return isset( $this->attributes[$offset] );
35         }
36
37         public function &offsetGet( $offset ) {
38                 if ( $this->attributes === null ) {
39                         $this->init();
40                 }
41                 return $this->attributes[$offset];
42         }
43
44         public function offsetSet( $offset, $value ) {
45                 if ( $this->attributes === null ) {
46                         $this->init();
47                 }
48                 $this->attributes[$offset] = $value;
49         }
50
51         public function offsetUnset( $offset ) {
52                 if ( $this->attributes === null ) {
53                         $this->init();
54                 }
55                 unset( $this->attributes[$offset] );
56         }
57
58         public function getValues() {
59                 if ( $this->attributes === null ) {
60                         $this->init();
61                 }
62                 return $this->attributes;
63         }
64
65         public function getObjects() {
66                 if ( $this->attrObjects === null ) {
67                         if ( $this->attributes === null ) {
68                                 $this->init();
69                         }
70                         $result = [];
71                         foreach ( $this->attributes as $name => $value ) {
72                                 $result[$name] = new Attribute( $name, null, null, $name, $value );
73                         }
74                         $this->attrObjects = $result;
75                 }
76                 return $this->attrObjects;
77         }
78
79         public function count() {
80                 return is_object( $this->data ) ? $this->data->count() : count( $this->data );
81         }
82
83         public function getIterator() {
84                 if ( $this->attributes === null ) {
85                         $this->init();
86                 }
87                 return new \ArrayIterator( $this->attributes );
88         }
89
90         public function merge( Attributes $other ) {
91                 if ( $this->attributes === null ) {
92                         $this->init();
93                 }
94                 foreach ( $other as $name => $value ) {
95                         if ( !isset( $this->attributes[$name] ) ) {
96                                 $this->attributes[$name] = $value;
97                         }
98                 }
99         }
100 }