|
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\Requirements\RequireAssignable; |
|
52
|
|
|
use GanbaroDigital\Reflection\Requirements\RequireIndexable; |
|
53
|
|
|
use GanbaroDigital\Reflection\ValueBuilders\SimpleType; |
|
54
|
|
|
|
|
55
|
|
|
class MergeIntoProperty |
|
56
|
|
|
{ |
|
57
|
|
|
/** |
|
58
|
|
|
* merge their data into one of our array's properties |
|
59
|
|
|
* |
|
60
|
|
|
* @param array &$arr |
|
61
|
|
|
* the array that we want to merge into |
|
62
|
|
|
* @param string $property |
|
63
|
|
|
* the property to merge into |
|
64
|
|
|
* @param mixed $value |
|
65
|
|
|
* the data that we want to merge from |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function ofArray(&$arr, $property, $value) |
|
69
|
|
|
{ |
|
70
|
|
|
// robustness! |
|
71
|
|
|
RequireIndexable::check($arr, E4xx_UnsupportedType::class); |
|
72
|
|
|
|
|
73
|
|
|
// easiest case - no clash |
|
74
|
|
|
if (ShouldOverwrite::intoArray($arr, $property, $value)) { |
|
75
|
|
|
$arr[$property] = $value; |
|
76
|
|
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// special case - we are merging into an object |
|
80
|
|
|
if (is_object($arr[$property])) { |
|
81
|
|
|
MergeIntoAssignable::from($arr[$property], $value); |
|
82
|
|
|
return; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// if we get here, then we must be merging into an array |
|
86
|
|
|
MergeIntoIndexable::from($arr[$property], $value); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* merge their data into one of our object's properties |
|
91
|
|
|
* |
|
92
|
|
|
* @param object &$obj |
|
93
|
|
|
* the object that we want to merge into |
|
94
|
|
|
* @param string $property |
|
95
|
|
|
* the property to merge into |
|
96
|
|
|
* @param mixed $value |
|
97
|
|
|
* the data that we want to merge from |
|
98
|
|
|
* @return void |
|
99
|
|
|
*/ |
|
100
|
|
|
public static function ofObject($obj, $property, $value) |
|
101
|
|
|
{ |
|
102
|
|
|
// robustness! |
|
103
|
|
|
RequireAssignable::check($obj, E4xx_UnsupportedType::class); |
|
104
|
|
|
|
|
105
|
|
|
// easiest case - no clash |
|
106
|
|
|
if (ShouldOverwrite::intoObject($obj, $property, $value)) { |
|
107
|
|
|
$obj->$property = $value; |
|
108
|
|
|
return; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// special case - we are merging into an object |
|
112
|
|
|
if (is_object($obj->$property)) { |
|
113
|
|
|
MergeIntoAssignable::from($obj->$property, $value); |
|
114
|
|
|
return; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// if we get here, then we must be merging into an array |
|
118
|
|
|
MergeIntoIndexable::from($obj->$property, $value); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* merge their data into one of our data's properties |
|
123
|
|
|
* |
|
124
|
|
|
* @param mixed $ours |
|
125
|
|
|
* the data that we want to merge into |
|
126
|
|
|
* @param string $property |
|
127
|
|
|
* the property to merge into |
|
128
|
|
|
* @param mixed $value |
|
129
|
|
|
* the data that we want to merge from |
|
130
|
|
|
* @return void |
|
131
|
|
|
*/ |
|
132
|
|
|
public static function of(&$ours, $property, $value) |
|
133
|
|
|
{ |
|
134
|
|
|
if (IsIndexable::check($ours)) { |
|
135
|
|
|
return self::ofArray($ours, $property, $value); |
|
136
|
|
|
} |
|
137
|
|
|
if (IsAssignable::check($ours)) { |
|
138
|
|
|
return self::ofObject($ours, $property, $value); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
throw new E4xx_UnsupportedType(SimpleType::from($ours)); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* merge their data into one of our data's properties |
|
146
|
|
|
* |
|
147
|
|
|
* @deprecated since 2.10.0 |
|
148
|
|
|
* @codeCoverageIgnore |
|
149
|
|
|
* |
|
150
|
|
|
* @param mixed $ours |
|
151
|
|
|
* the data that we want to merge into |
|
152
|
|
|
* @param string $property |
|
153
|
|
|
* the property to merge into |
|
154
|
|
|
* @param mixed $value |
|
155
|
|
|
* the data that we want to merge from |
|
156
|
|
|
* @return void |
|
157
|
|
|
*/ |
|
158
|
|
|
public static function ofMixed(&$ours, $property, $value) |
|
159
|
|
|
{ |
|
160
|
|
|
return self::of($ours, $property, $value); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* merge their data into one of our data's properties |
|
165
|
|
|
* |
|
166
|
|
|
* @param mixed $ours |
|
167
|
|
|
* the data that we want to merge into |
|
168
|
|
|
* @param string $property |
|
169
|
|
|
* the property to merge into |
|
170
|
|
|
* @param mixed $value |
|
171
|
|
|
* the data that we want to merge from |
|
172
|
|
|
* @return void |
|
173
|
|
|
*/ |
|
174
|
|
|
public function __invoke(&$ours, $property, $value) |
|
175
|
|
|
{ |
|
176
|
|
|
return self::of($ours, $property, $value); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|