These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Diff\Differ; |
||
4 | |||
5 | use Diff\ArrayComparer\ArrayComparer; |
||
6 | use Diff\ArrayComparer\NativeArrayComparer; |
||
7 | use Diff\ArrayComparer\StrictArrayComparer; |
||
8 | use Diff\DiffOp\DiffOp; |
||
9 | use Diff\DiffOp\DiffOpAdd; |
||
10 | use Diff\DiffOp\DiffOpRemove; |
||
11 | use InvalidArgumentException; |
||
12 | |||
13 | /** |
||
14 | * Differ that only looks at the values of the arrays (and thus ignores key differences). |
||
15 | * |
||
16 | * By default values are compared using a StrictArrayComparer. |
||
17 | * You can alter the ArrayComparer used by providing one in the constructor. |
||
18 | * |
||
19 | * @since 0.4 |
||
20 | * |
||
21 | * @licence GNU GPL v2+ |
||
22 | * @author Jeroen De Dauw < [email protected] > |
||
23 | */ |
||
24 | class ListDiffer implements Differ { |
||
25 | |||
26 | /** |
||
27 | * Use non-strict comparison and do not care about quantity. |
||
28 | * This makes use of @see array_diff |
||
29 | * |
||
30 | * @since 0.4 |
||
31 | * @deprecated since 0.8, use new NativeArrayComparer() instead |
||
32 | */ |
||
33 | const MODE_NATIVE = 0; |
||
34 | |||
35 | /** |
||
36 | * Use strict comparison and care about quantity. |
||
37 | * This makes use of @see ListDiffer::strictDiff |
||
38 | * |
||
39 | * @since 0.4 |
||
40 | * @deprecated since 0.8, use null instead |
||
41 | */ |
||
42 | const MODE_STRICT = 1; |
||
43 | |||
44 | /** |
||
45 | * @var ArrayComparer |
||
46 | */ |
||
47 | private $arrayComparer; |
||
48 | |||
49 | /** |
||
50 | * @param ArrayComparer|int|null $arrayComparer Defaults to a StrictArrayComparer. The use of |
||
51 | * the self::MODE_... constants is deprecated. |
||
52 | * |
||
53 | * The first argument is an ArrayComparer since version 0.8. |
||
54 | * Before this it was an element of the ListDiffer::MODE_ enum. |
||
55 | * The later is still supported though deprecated. |
||
56 | */ |
||
57 | public function __construct( $arrayComparer = null ) { |
||
58 | $this->arrayComparer = $this->getRealArrayComparer( $arrayComparer ); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param ArrayComparer|int|null $arrayComparer Defaults to a StrictArrayComparer. The use of |
||
63 | * the self::MODE_... constants is deprecated. |
||
64 | * |
||
65 | * @return ArrayComparer |
||
66 | * @throws InvalidArgumentException |
||
67 | */ |
||
68 | private function getRealArrayComparer( $arrayComparer ) { |
||
69 | if ( $arrayComparer === null || $arrayComparer === self::MODE_STRICT ) { |
||
0 ignored issues
–
show
|
|||
70 | return new StrictArrayComparer(); |
||
71 | } |
||
72 | |||
73 | if ( $arrayComparer === self::MODE_NATIVE ) { |
||
0 ignored issues
–
show
The constant
Diff\Differ\ListDiffer::MODE_NATIVE has been deprecated with message: since 0.8, use new NativeArrayComparer() instead
This class constant has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.
Loading history...
|
|||
74 | return new NativeArrayComparer(); |
||
75 | } |
||
76 | |||
77 | if ( is_object( $arrayComparer ) && $arrayComparer instanceof ArrayComparer ) { |
||
78 | return $arrayComparer; |
||
79 | } |
||
80 | |||
81 | throw new InvalidArgumentException( 'Got an invalid $arrayComparer' ); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @see Differ::doDiff |
||
86 | * |
||
87 | * @since 0.4 |
||
88 | * |
||
89 | * @param array $oldValues The first array |
||
90 | * @param array $newValues The second array |
||
91 | * |
||
92 | * @return DiffOp[] |
||
93 | */ |
||
94 | public function doDiff( array $oldValues, array $newValues ) { |
||
95 | $operations = array(); |
||
96 | |||
97 | foreach ( $this->diffArrays( $newValues, $oldValues ) as $addition ) { |
||
98 | $operations[] = new DiffOpAdd( $addition ); |
||
99 | } |
||
100 | |||
101 | foreach ( $this->diffArrays( $oldValues, $newValues ) as $removal ) { |
||
102 | $operations[] = new DiffOpRemove( $removal ); |
||
103 | } |
||
104 | |||
105 | return $operations; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param array $arrayOne |
||
110 | * @param array $arrayTwo |
||
111 | * |
||
112 | * @return array |
||
113 | */ |
||
114 | private function diffArrays( array $arrayOne, array $arrayTwo ) { |
||
115 | return $this->arrayComparer->diffArrays( $arrayOne, $arrayTwo ); |
||
116 | } |
||
117 | |||
118 | } |
||
119 |
This class constant has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.