1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2015-present Ganbaro Digital Ltd |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
16
|
|
|
* the documentation and/or other materials provided with the |
17
|
|
|
* distribution. |
18
|
|
|
* |
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
20
|
|
|
* contributors may be used to endorse or promote products derived |
21
|
|
|
* from this software without specific prior written permission. |
22
|
|
|
* |
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
* |
36
|
|
|
* @category Libraries |
37
|
|
|
* @package DataContainers/Internal |
38
|
|
|
* @author Stuart Herbert <[email protected]> |
39
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
40
|
|
|
* @copyright 2015-present Ganbaro Digital Ltd www.ganbarodigital.com |
41
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
42
|
|
|
* @link http://code.ganbarodigital.com/php-data-containers |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
namespace GanbaroDigital\DataContainers\Internal\Checks; |
46
|
|
|
|
47
|
|
|
use GanbaroDigital\DataContainers\Exceptions\E4xx_UnsupportedType; |
48
|
|
|
use GanbaroDigital\Reflection\Checks\IsAssignable; |
49
|
|
|
use GanbaroDigital\Reflection\Checks\IsIndexable; |
50
|
|
|
use GanbaroDigital\Reflection\ValueBuilders\LookupMethodByType; |
51
|
|
|
use GanbaroDigital\Reflection\ValueBuilders\SimpleType; |
52
|
|
|
|
53
|
|
|
class ShouldOverwrite |
54
|
|
|
{ |
55
|
|
|
use LookupMethodByType; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* should we overwrite $ours's $property with the value of $theirs? |
59
|
|
|
* |
60
|
|
|
* @param mixed $ours |
61
|
|
|
* the variable where $property may exist |
62
|
|
|
* @param string $property |
63
|
|
|
* the property on $ours whose fate we are deciding |
64
|
|
|
* @param mixed $theirs |
65
|
|
|
* the data we want to assign to the property |
66
|
|
|
* @return boolean |
67
|
|
|
* TRUE if we should overwrite the property's existing value |
68
|
|
|
* with $value |
69
|
|
|
* TRUE if $property currently has no value |
70
|
|
|
* FALSE if we should merge $value into the property's exist |
71
|
|
|
* value |
72
|
|
|
*/ |
73
|
|
|
public function __invoke($ours, $property, $theirs) |
74
|
|
|
{ |
75
|
|
|
return self::into($ours, $property, $theirs); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* should we overwrite $ours's $property with the value of $theirs? |
80
|
|
|
* |
81
|
|
|
* @param mixed $ours |
82
|
|
|
* the variable where $property may exist |
83
|
|
|
* @param string $property |
84
|
|
|
* the property on $ours whose fate we are deciding |
85
|
|
|
* @param mixed $theirs |
86
|
|
|
* the data we want to assign to the property |
87
|
|
|
* @return boolean |
88
|
|
|
* TRUE if we should overwrite the property's existing value |
89
|
|
|
* with $value |
90
|
|
|
* TRUE if $property currently has no value |
91
|
|
|
* FALSE if we should merge $value into the property's exist |
92
|
|
|
* value |
93
|
|
|
*/ |
94
|
|
|
public static function into($ours, $property, $theirs) |
95
|
|
|
{ |
96
|
|
|
$methodName = self::lookupMethodFor($ours, self::$dispatchTable); |
97
|
|
|
return self::$methodName($ours, $property, $theirs); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* called when there's no entry in our dispatch table for a data type |
102
|
|
|
* |
103
|
|
|
* @param mixed $ours |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
private static function nothingMatchesTheInputType($ours) |
107
|
|
|
{ |
108
|
|
|
throw new E4xx_UnsupportedType(SimpleType::from($ours)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* should we overwrite $ours->$property with the value of $theirs? |
113
|
|
|
* |
114
|
|
|
* @param object $ours |
115
|
|
|
* the object where $property may exist |
116
|
|
|
* @param string $property |
117
|
|
|
* the property on $ours whose fate we are deciding |
118
|
|
|
* @param mixed $theirs |
119
|
|
|
* the data we want to assign to the property |
120
|
|
|
* @return boolean |
121
|
|
|
* TRUE if we should overwrite the property's existing value |
122
|
|
|
* with $value |
123
|
|
|
* TRUE if $property currently has no value |
124
|
|
|
* FALSE if we should merge $value into the property's exist |
125
|
|
|
* value |
126
|
|
|
*/ |
127
|
|
|
public static function intoObject($ours, $property, $theirs) |
128
|
|
|
{ |
129
|
|
|
// robustness! |
130
|
|
|
if (!IsAssignable::check($ours)) { |
131
|
|
|
throw new E4xx_UnsupportedType(SimpleType::from($ours)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return self::checkObject($ours, $property, $theirs); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* should we overwrite $ours->$property with the value of $theirs? |
139
|
|
|
* |
140
|
|
|
* @param object $ours |
141
|
|
|
* the object where $property may exist |
142
|
|
|
* @param string $property |
143
|
|
|
* the property on $ours whose fate we are deciding |
144
|
|
|
* @param mixed $theirs |
145
|
|
|
* the data we want to assign to the property |
146
|
|
|
* @return boolean |
147
|
|
|
* TRUE if we should overwrite the property's existing value |
148
|
|
|
* with $value |
149
|
|
|
* TRUE if $property currently has no value |
150
|
|
|
* FALSE if we should merge $value into the property's exist |
151
|
|
|
* value |
152
|
|
|
*/ |
153
|
|
|
private static function checkObject($ours, $property, $theirs) |
154
|
|
|
{ |
155
|
|
|
// special case - property does not exist |
156
|
|
|
if (!isset($ours->$property)) { |
157
|
|
|
return true; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// general case - property exists, and we need to decide what should |
161
|
|
|
// be done about it |
162
|
|
|
return !AreMergeable::into($ours->$property, $theirs); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* should we overwrite $ours[$key] with the value of $theirs? |
167
|
|
|
* |
168
|
|
|
* @param array $ours |
169
|
|
|
* the array where $key may exist |
170
|
|
|
* @param string $key |
171
|
|
|
* the index on $ours whose fate we are deciding |
172
|
|
|
* @param mixed $theirs |
173
|
|
|
* the data we want to assign to the index |
174
|
|
|
* @return boolean |
175
|
|
|
* TRUE if we should overwrite the index's existing value |
176
|
|
|
* with $value |
177
|
|
|
* TRUE if $key currently has no value |
178
|
|
|
* FALSE if we should merge $value into the index's exist |
179
|
|
|
* value |
180
|
|
|
*/ |
181
|
|
|
public static function intoArray($ours, $key, $theirs) |
182
|
|
|
{ |
183
|
|
|
// robustness! |
184
|
|
|
if (!IsIndexable::check($ours)) { |
185
|
|
|
throw new E4xx_UnsupportedType(SimpleType::from($ours)); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return self::checkArray($ours, $key, $theirs); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* should we overwrite $ours[$key] with the value of $theirs? |
193
|
|
|
* |
194
|
|
|
* @param array $ours |
195
|
|
|
* the array where $key may exist |
196
|
|
|
* @param string $key |
197
|
|
|
* the index on $ours whose fate we are deciding |
198
|
|
|
* @param mixed $theirs |
199
|
|
|
* the data we want to assign to the index |
200
|
|
|
* @return boolean |
201
|
|
|
* TRUE if we should overwrite the index's existing value |
202
|
|
|
* with $value |
203
|
|
|
* TRUE if $key currently has no value |
204
|
|
|
* FALSE if we should merge $value into the index's exist |
205
|
|
|
* value |
206
|
|
|
*/ |
207
|
|
|
private static function checkArray(array $ours, $key, $theirs) |
208
|
|
|
{ |
209
|
|
|
// special case - index does not exist |
210
|
|
|
if (!array_key_exists($key, $ours)) { |
211
|
|
|
return true; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// general case - index exists, and we need to decide what should |
215
|
|
|
// be done about it |
216
|
|
|
return !AreMergeable::into($ours[$key], $theirs); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* lookup table of which method to call for which data type |
221
|
|
|
* @var array |
222
|
|
|
*/ |
223
|
|
|
private static $dispatchTable = [ |
224
|
|
|
'Array' => 'intoArray', |
225
|
|
|
'Object' => 'intoObject', |
226
|
|
|
]; |
227
|
|
|
} |