MergeUsingDotNotationPath::intoMixed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 4
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\Checks\IsDotNotationPath;
48
use GanbaroDigital\DataContainers\Containers\DataBag;
49
use GanbaroDigital\DataContainers\Exceptions\E4xx_NotDotNotationPath;
50
use GanbaroDigital\DataContainers\Exceptions\E4xx_UnsupportedType;
51
use GanbaroDigital\DataContainers\Filters\FilterDotNotationParts;
52
use GanbaroDigital\DataContainers\Requirements\RequireDotNotationPath;
53
use GanbaroDigital\DataContainers\ValueBuilders\DescendDotNotationPath;
54
use GanbaroDigital\Reflection\Checks\IsAssignable;
55
use GanbaroDigital\Reflection\Checks\IsIndexable;
56
use GanbaroDigital\Reflection\Requirements\RequireAssignable;
57
use GanbaroDigital\Reflection\Requirements\RequireIndexable;
58
use GanbaroDigital\Reflection\ValueBuilders\SimpleType;
59
60
class MergeUsingDotNotationPath
61
{
62
    /**
63
     * merge their data into our array, using dot.notation.support to
64
     * find the point where the merge starts
65
     *
66
     * @param  array &$arr
67
     *         the array that we want to merge into
68
     * @param  string $path
69
     *         the dot.notation.support path to where the merge should start
70
     * @param  mixed $value
71
     *         the data that we want to merge from
72
     * @param  array|callable|string|null
73
     *         if $path goes beyond what exists in $ours, how do we want to
74
     *         extend $ours?
75
     * @return void
76
     */
77
    public static function intoArray(&$arr, $path, $value, $extendingItem = null)
78
    {
79
        // robustness!
80
        RequireIndexable::check($arr, E4xx_UnsupportedType::class);
81
        RequireDotNotationPath::check($path);
82
83
        // find the point where we want to merge
84
        list ($firstPart, $finalPart) = self::splitPathInTwo($path);
85
        if ($firstPart !== null) {
86
            $leaf =& DescendDotNotationPath::intoArray($arr, $firstPart, $extendingItem);
87
        }
88
        else {
89
            $leaf =& $arr;
90
        }
91
92
        // merge it
93
        MergeIntoProperty::of($leaf, $finalPart, $value);
94
    }
95
96
    /**
97
     * merge their data into our object, using dot.notation.support to
98
     * find the point where the merge starts
99
     *
100
     * @param  object $obj
101
     *         the object that we want to merge into
102
     * @param  string $path
103
     *         the dot.notation.support path to where the merge should start
104
     * @param  mixed $value
105
     *         the data that we want to merge from
106
     * @param  array|callable|string|null
107
     *         if $path goes beyond what exists in $ours, how do we want to
108
     *         extend $ours?
109
     * @return void
110
     */
111
    public static function intoObject($obj, $path, $value, $extendingItem = null)
112
    {
113
        // robustness!
114
        RequireAssignable::check($obj, E4xx_UnsupportedType::class);
115
        RequireDotNotationPath::check($path);
116
117
        // find the point where we want to merge
118
        list ($firstPart, $finalPart) = self::splitPathInTwo($path);
119
        if ($firstPart !== null) {
120
            $leaf =& DescendDotNotationPath::intoObject($obj, $firstPart, $extendingItem);
121
        }
122
        else {
123
            $leaf = $obj;
124
        }
125
126
        // merge it
127
        MergeIntoProperty::of($leaf, $finalPart, $value);
128
    }
129
130
    /**
131
     * merge their data into our data, using dot.notation.support to
132
     * find the point where the merge starts
133
     *
134
     * @param  object $ours
135
     *         the object that we want to merge into
136
     * @param  string $path
137
     *         the dot.notation.support path to where the merge should start
138
     * @param  mixed $value
139
     *         the data that we want to merge from
140
     * @param  array|callable|string|null
141
     *         if $path goes beyond what exists in $ours, how do we want to
142
     *         extend $ours?
143
     * @return void
144
     */
145
    public static function into(&$ours, $path, $value, $extendingItem = null)
146
    {
147
        if (IsAssignable::check($ours)) {
148
            return self::intoObject($ours, $path, $value, $extendingItem);
149
        }
150
        if (IsIndexable::check($ours)) {
151
            return self::intoArray($ours, $path, $value, $extendingItem);
152
        }
153
154
        throw new E4xx_UnsupportedType(SimpleType::from($ours));
155
    }
156
157
    /**
158
     * merge their data into our data, using dot.notation.support to
159
     * find the point where the merge starts
160
     *
161
     * @deprecated since 2.2.0
162
     * @codeCoverageIgnore
163
     *
164
     * @param  mixed $ours
165
     *         the data that we want to merge into
166
     * @param  string $path
167
     *         the dot.notation.support path to where the merge should start
168
     * @param  mixed $value
169
     *         the data that we want to merge from
170
     * @param  array|callable|string|null
171
     *         if $path goes beyond what exists in $ours, how do we want to
172
     *         extend $ours?
173
     * @return void
174
     */
175
    public static function intoMixed(&$ours, $path, $value, $extendingItem = null)
176
    {
177
        return self::into($ours, $path, $value, $extendingItem);
178
    }
179
180
    /**
181
     * merge their data into our data, using dot.notation.support to
182
     * find the point where the merge starts
183
     *
184
     * @param  mixed $ours
185
     *         the data that we want to merge into
186
     * @param  string $path
187
     *         the dot.notation.support path to where the merge should start
188
     * @param  mixed $value
189
     *         the data that we want to merge from
190
     * @param  array|callable|string|null
191
     *         if $path goes beyond what exists in $ours, how do we want to
192
     *         extend $ours?
193
     * @return void
194
     */
195
    public function __invoke(&$ours, $path, $value, $extendingItem = null)
196
    {
197
        return self::into($ours, $path, $value, $extendingItem);
198
    }
199
200
    /**
201
     * split the dot.notation.support path up into two parts - where we are
202
     * inserting, and the name of what we are inserting
203
     *
204
     * @param  string $path
205
     *         the dot.notation.support path that needs splitting
206
     * @return array
207
     *         the two parts of the path
208
     */
209
    private static function splitPathInTwo($path)
210
    {
211
        // find the point where we want to merge
212
        $firstPart = FilterDotNotationParts::fromString($path, 0, -1);
213
        $finalPart = FilterDotNotationParts::fromString($path, -1, 1);
214
215
        // what do we have?
216
        // var_dump("path", $path, "firstPart", $firstPart, "finalPart", $finalPart);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
217
218
        // special case - we're already at the end of the path
219
        if (strlen($firstPart) === 0) {
220
            return [ null, $finalPart ];
221
        }
222
223
        // general case - we're not at the end of the path
224
        return [ $firstPart, $finalPart ];
225
    }
226
}
227