Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
EntitySchemaDiffer | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
diffSchemas | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | |
3 | declare( strict_types = 1 ); |
4 | |
5 | namespace EntitySchema\Services\Diff; |
6 | |
7 | use Diff\Differ\MapDiffer; |
8 | use Diff\DiffOp\Diff\Diff; |
9 | use EntitySchema\Services\Converter\FullArrayEntitySchemaData; |
10 | |
11 | /** |
12 | * Computes the difference between two schemas. |
13 | * The difference is represented as an associative {@link Diff} with the following operations: |
14 | * |
15 | * - labels: an associative {@link Diff} where the keys are language codes |
16 | * and the values are {@link AtomicDiffOp}s for label addition, removal or change. |
17 | * - descriptions: an associative Diff where the keys are language codes |
18 | * and the values are {@link AtomicDiffOp}s for description addition, removal or change. |
19 | * - aliases: an associative {@link Diff} where the keys are language codes |
20 | * and the values are non-associative {@link Diff}s |
21 | * containing {@link DiffOpAdd}s and {@link DiffOpRemove}s. |
22 | * (A “change” to an alias appears as a removal+addition pair.) |
23 | * - schemaText: a single {@link AtomicDiffOp} for schema addition, removal or change. |
24 | * (Empty schema strings are considered absent. No fine-grained diffing on the text occurs.) |
25 | * |
26 | * @license GPL-2.0-or-later |
27 | */ |
28 | class EntitySchemaDiffer { |
29 | |
30 | private MapDiffer $recursiveMapDiffer; |
31 | |
32 | public function __construct() { |
33 | $this->recursiveMapDiffer = new MapDiffer( true ); |
34 | } |
35 | |
36 | public function diffSchemas( FullArrayEntitySchemaData $from, FullArrayEntitySchemaData $to ): Diff { |
37 | $from = $from->data; |
38 | $to = $to->data; |
39 | |
40 | if ( array_key_exists( 'schemaText', $from ) && $from['schemaText'] === '' ) { |
41 | unset( $from['schemaText'] ); |
42 | } |
43 | if ( array_key_exists( 'schemaText', $to ) && $to['schemaText'] === '' ) { |
44 | unset( $to['schemaText'] ); |
45 | } |
46 | |
47 | $diff = $this->recursiveMapDiffer->doDiff( $from, $to ); |
48 | |
49 | return new Diff( $diff, true ); |
50 | } |
51 | |
52 | } |