AreMergeable::into()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 8.8571
cc 5
eloc 6
nc 3
nop 2
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 GanbaroDigial\DataContainers\Exceptions\E4xx_UnsupportedType;
48
use GanbaroDigital\Reflection\Checks\IsAssignable;
49
use GanbaroDigital\Reflection\Checks\IsIndexable;
50
use GanbaroDigital\Reflection\Checks\IsTraversable;
51
use GanbaroDigital\Reflection\ValueBuilders\FirstMethodMatchingType;
52
53
class AreMergeable
54
{
55
    /**
56
     * does it make sense to attempt to merge the contents of $theirs into
57
     * $ours?
58
     *
59
     * @param  mixed $ours
60
     *         where we want to merge to
61
     * @param  mixed $theirs
62
     *         where we want to merge from
63
     * @return boolean
64
     *         true if merging makes sense
65
     *         false otherwise
66
     */
67
    public static function into($ours, $theirs)
68
    {
69
        // we can't merge non-arrays, non-objects
70
        if (!IsTraversable::check($theirs) && !IsAssignable::check($theirs)) {
71
            return false;
72
        }
73
74
        // if we have arrays or databag-type objects, we're good
75
        if (IsIndexable::check($ours) || IsAssignable::check($ours)) {
76
            return true;
77
        }
78
79
        // if we get here, then $ours is a complex object, which
80
        // we do not know how to merge
81
        //
82
        // or it is a scalar, which doesn't support merging at all
83
        return false;
84
    }
85
86
    /**
87
     * does it make sense to attempt to merge the contents of $theirs into
88
     * $ours?
89
     *
90
     * @deprecated since 2.2.0
91
     * @codeCoverageIgnore
92
     *
93
     * @param  mixed $ours
94
     *         where we want to merge to
95
     * @param  mixed $theirs
96
     *         where we want to merge from
97
     * @return boolean
98
     *         true if merging makes sense
99
     *         false otherwise
100
     */
101
    public static function intoMixed($ours, $theirs)
102
    {
103
        return self::into($ours, $theirs);
104
    }
105
106
    /**
107
     * does it make sense to attempt to merge the contents of $theirs into
108
     * $ours?
109
     *
110
     * @param  mixed $ours
111
     *         where we want to merge to
112
     * @param  mixed $theirs
113
     *         where we want to merge from
114
     * @return boolean
115
     *         true if merging makes sense
116
     *         false otherwise
117
     */
118
    public function __invoke($ours, $theirs)
119
    {
120
        return self::into($ours, $theirs);
121
    }
122
123
}
124