Debian Codegraph @

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

raw
1 <?php
2
3 namespace Wikimedia\RemexHtml\Tokenizer;
4
5 /**
6 * An Attributes implementation which is a simple array proxy.
7 */
8 class PlainAttributes implements Attributes {
9 /** @var array */
10 protected $data;
11 /** @var array<string,Attribute> */
12 protected $attrObjects;
13
14 /**
15 * @param array $data
16 */
17 public function __construct( $data = [] ) {
18 $this->data = $data;
19 }
20
21 /** @inheritDoc */
22 public function merge( Attributes $other ) {
23 foreach ( $other as $name => $value ) {
24 if ( !isset( $this[$name] ) ) {
25 $this[$name] = $value;
26 }
27 }
28 }
29
30 /** @inheritDoc */
31 public function offsetExists( $key ): bool {
32 return isset( $this->data[$key] );
33 }
34
35 /** @inheritDoc */
36 public function &offsetGet( $key ): string {
37 return $this->data[$key];
38 }
39
40 /** @inheritDoc */
41 public function offsetSet( $key, $value ): void {
42 $this->data[$key] = $value;
43 if ( $this->attrObjects !== null ) {
44 $this->attrObjects[$key] = new Attribute( $key, null, null, $key, $value );
45 }
46 }
47
48 /** @inheritDoc */
49 public function offsetUnset( $key ): void {
50 unset( $this->data[$key] );
51 unset( $this->attrObjects[$key] );
52 }
53
54 public function getIterator(): \ArrayIterator {
55 return new \ArrayIterator( $this->data );
56 }
57
58 /** @inheritDoc */
59 public function getValues() {
60 return $this->data;
61 }
62
63 /** @inheritDoc */
64 public function getObjects() {
65 if ( $this->attrObjects === null ) {
66 $result = [];
67 foreach ( $this->data as $name => $value ) {
68 $result[$name] = new Attribute( $name, null, null, $name, $value );
69 }
70 $this->attrObjects = $result;
71 }
72 return $this->attrObjects;
73 }
74
75 /** @inheritDoc */
76 public function count() {
77 return count( $this->data );
78 }
79
80 /** @inheritDoc */
81 public function clone() {
82 return $this;
83 }
84 }