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