Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
DataParsoidDiff | |
0.00% |
0 / 17 |
|
0.00% |
0 / 9 |
156 | |
0.00% |
0 / 1 |
isEmpty | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addDiffMarker | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
hasDiffMarker | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
hasOnlyDiffMarkers | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
defaultValue | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
hint | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
flatten | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
toJsonArray | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
newFromJsonArray | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace Wikimedia\Parsoid\NodeData; |
5 | |
6 | use Wikimedia\JsonCodec\JsonCodecable; |
7 | use Wikimedia\JsonCodec\JsonCodecableTrait; |
8 | use Wikimedia\Parsoid\Utils\RichCodecable; |
9 | |
10 | /** |
11 | * Diff markers for a DOM node. Managed by |
12 | * DOMDataUtils::get/setDataParsoidDiff and DiffUtils::get/setDiffMark. |
13 | */ |
14 | class DataParsoidDiff implements JsonCodecable, RichCodecable { |
15 | use JsonCodecableTrait; |
16 | |
17 | /** |
18 | * @var array<string, bool> Set of diff markers. |
19 | * @see DiffMarkers class |
20 | */ |
21 | private array $diff = []; |
22 | |
23 | public function isEmpty(): bool { |
24 | return count( $this->diff ) === 0; |
25 | } |
26 | |
27 | /** |
28 | * Add the given mark to this set. |
29 | */ |
30 | public function addDiffMarker( string $mark ): void { |
31 | $this->diff[$mark] = true; |
32 | } |
33 | |
34 | /** |
35 | * Returns true if the given mark is present. |
36 | */ |
37 | public function hasDiffMarker( string $mark ): bool { |
38 | return $this->diff[$mark] ?? false; |
39 | } |
40 | |
41 | /** |
42 | * Returns true if no marks other than the given ones are present. |
43 | */ |
44 | public function hasOnlyDiffMarkers( string ...$marks ): bool { |
45 | // Count the given marks, then compare that to the count of all marks. |
46 | $count = 0; |
47 | foreach ( $marks as $m ) { |
48 | $count += ( $this->diff[$m] ?? false ) ? 1 : 0; |
49 | } |
50 | return $count === count( $this->diff ); |
51 | } |
52 | |
53 | // RichCodecable |
54 | |
55 | /** @inheritDoc */ |
56 | public static function defaultValue(): DataParsoidDiff { |
57 | return new DataParsoidDiff; |
58 | } |
59 | |
60 | /** @return class-string<DataParsoidDiff> */ |
61 | public static function hint(): string { |
62 | return self::class; |
63 | } |
64 | |
65 | /** No flattened value. */ |
66 | public function flatten(): ?string { |
67 | return null; |
68 | } |
69 | |
70 | // JsonCodecable |
71 | |
72 | /** @inheritDoc */ |
73 | public function toJsonArray(): array { |
74 | $markers = array_keys( $this->diff ); |
75 | sort( $markers ); // keep order consistent |
76 | return [ 'diff' => $markers ]; |
77 | } |
78 | |
79 | /** @inheritDoc */ |
80 | public static function newFromJsonArray( array $json ): DataParsoidDiff { |
81 | $dpd = new DataParsoidDiff; |
82 | foreach ( $json['diff'] as $mark ) { |
83 | $dpd->addDiffMarker( $mark ); |
84 | } |
85 | return $dpd; |
86 | } |
87 | } |