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/Editors |
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\Editors; |
46
|
|
|
|
47
|
|
|
use GanbaroDigital\DataContainers\Exceptions\E4xx_UnsupportedType; |
48
|
|
|
use GanbaroDigital\DataContainers\Internal\Checks\ShouldOverwrite; |
49
|
|
|
use GanbaroDigital\Reflection\Checks\IsAssignable; |
50
|
|
|
use GanbaroDigital\Reflection\Checks\IsIndexable; |
51
|
|
|
use GanbaroDigital\Reflection\Checks\IsTraversable; |
52
|
|
|
use GanbaroDigital\Reflection\Requirements\RequireAssignable; |
53
|
|
|
use GanbaroDigital\Reflection\Requirements\RequireIndexable; |
54
|
|
|
use GanbaroDigital\Reflection\Requirements\RequireTraversable; |
55
|
|
|
use GanbaroDigital\Reflection\ValueBuilders\FirstMethodMatchingType; |
56
|
|
|
use GanbaroDigital\Reflection\ValueBuilders\SimpleType; |
57
|
|
|
|
58
|
|
|
class MergeIntoIndexable |
59
|
|
|
{ |
60
|
|
|
/** |
61
|
|
|
* merge their array into our array |
62
|
|
|
* |
63
|
|
|
* @param array &$ours |
64
|
|
|
* the array that we want to merge into |
65
|
|
|
* @param array|Traversable $theirs |
66
|
|
|
* the array that we want to merge from |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
public static function fromArray(&$ours, $theirs) |
70
|
|
|
{ |
71
|
|
|
// robustness! |
72
|
|
|
RequireIndexable::check($ours, E4xx_UnsupportedType::class); |
73
|
|
|
RequireTraversable::check($theirs, E4xx_UnsupportedType::class); |
74
|
|
|
|
75
|
|
|
// copy from them to us |
76
|
|
|
foreach ($theirs as $key => $value) { |
77
|
|
|
self::mergeKeyIntoArray($ours, $key, $value); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// all done |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* merge a single key into our array |
85
|
|
|
* |
86
|
|
|
* @param array &$ours |
87
|
|
|
* the array to merge into |
88
|
|
|
* @param mixed $key |
89
|
|
|
* the array key to merge into |
90
|
|
|
* @param mixed $value |
91
|
|
|
* the data to merge in |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
private static function mergeKeyIntoArray(&$ours, $key, $value) |
95
|
|
|
{ |
96
|
|
|
// general case - overwrite because merging isn't possible |
97
|
|
|
if (ShouldOverwrite::intoArray($ours, $key, $value)) { |
98
|
|
|
$ours[$key] = $value; |
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// special case - we are merging into an object |
103
|
|
|
if (is_object($ours[$key])) { |
104
|
|
|
MergeIntoAssignable::from($ours[$key], $value); |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// at this point, we are merging into an array, using recursion |
109
|
|
|
// for which I am going to hell |
110
|
|
|
MergeIntoIndexable::from($ours[$key], $value); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* merge their object into our array |
115
|
|
|
* |
116
|
|
|
* @param array &$ours |
117
|
|
|
* the array that we want to merge into |
118
|
|
|
* @param object $theirs |
119
|
|
|
* the object that we want to merge from |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
public static function fromObject(&$ours, $theirs) |
123
|
|
|
{ |
124
|
|
|
// robustness! |
125
|
|
|
RequireAssignable::check($theirs, E4xx_UnsupportedType::class); |
126
|
|
|
|
127
|
|
|
self::fromArray($ours, get_object_vars($theirs)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* merge their data into our array |
132
|
|
|
* |
133
|
|
|
* @param array &$ours |
134
|
|
|
* the array that we want to merge into |
135
|
|
|
* @param array|object $theirs |
136
|
|
|
* the data that we want to merge from |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public static function from(&$ours, $theirs) |
140
|
|
|
{ |
141
|
|
|
if (IsAssignable::check($theirs)) { |
142
|
|
|
return self::fromObject($ours, $theirs); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (IsTraversable::check($theirs)) { |
146
|
|
|
return self::fromArray($ours, $theirs); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// at this point, we want to append onto the end of $ours |
150
|
|
|
$ours[] = $theirs; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* merge their data into our array |
155
|
|
|
* |
156
|
|
|
* @deprecated since 2.2.0 |
157
|
|
|
* @codeCoverageIgnore |
158
|
|
|
* |
159
|
|
|
* @param array &$ours |
160
|
|
|
* the array that we want to merge into |
161
|
|
|
* @param array|object $theirs |
162
|
|
|
* the data that we want to merge from |
163
|
|
|
* @return void |
164
|
|
|
*/ |
165
|
|
|
public static function fromMixed(&$ours, $theirs) |
166
|
|
|
{ |
167
|
|
|
return self::from($ours, $theirs); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* merge their data into our array |
172
|
|
|
* |
173
|
|
|
* @param array &$ours |
174
|
|
|
* the array that we want to merge into |
175
|
|
|
* @param array|object $theirs |
176
|
|
|
* the data that we want to merge from |
177
|
|
|
* @return void |
178
|
|
|
*/ |
179
|
|
|
public function __invoke(&$ours, $theirs) |
180
|
|
|
{ |
181
|
|
|
return self::from($ours, $theirs); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.