HasUsingDotNotationPath   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 8
dl 0
loc 107
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 4 1
A in() 0 7 1
A inArray() 0 17 3
A inObject() 0 17 3
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/Checks
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\Checks;
46
47
use GanbaroDigital\DataContainers\Exceptions\E4xx_NoSuchIndex;
48
use GanbaroDigital\DataContainers\Exceptions\E4xx_NoSuchProperty;
49
use GanbaroDigital\DataContainers\Exceptions\E4xx_NotDotNotationPath;
50
use GanbaroDigital\DataContainers\Exceptions\E4xx_UnsupportedType;
51
use GanbaroDigital\DataContainers\Requirements\RequireDotNotationPath;
52
use GanbaroDigital\DataContainers\Requirements\RequireReadableContainer;
53
use GanbaroDigital\DataContainers\ValueBuilders\DescendDotNotationPath;
54
use GanbaroDigital\Defensive\Requirements\RequireAnyOneOf;
55
use GanbaroDigital\Reflection\ValueBuilders\LookupMethodByType;
56
use GanbaroDigital\Reflection\Checks\IsAssignable;
57
use GanbaroDigital\Reflection\Checks\IsIndexable;
58
use GanbaroDigital\Reflection\Checks\IsStringy;
59
use GanbaroDigital\Reflection\Requirements\RequireAssignable;
60
use GanbaroDigital\Reflection\Requirements\RequireIndexable;
61
use GanbaroDigital\Reflection\ValueBuilders\SimpleType;
62
63
class HasUsingDotNotationPath
64
{
65
    use LookupMethodByType;
66
67
    /**
68
     * does $path point to a valid piece of data inside $container?
69
     *
70
     * @param  mixed $container
71
     *         the container to look inside
72
     * @param  string $path
73
     *         the dot.notation.support path to walk
74
     * @return boolean
75
     *         TRUE if $path points to a vlaid piece of data
76
     *         FALSE otherwise
77
     */
78
    public function __invoke($container, $path)
79
    {
80
        return self::in($container, $path);
81
    }
82
83
    /**
84
     * does $path point to a valid piece of data inside $container?
85
     *
86
     * @param  mixed $container
87
     *         the container to look inside
88
     * @param  string $path
89
     *         the dot.notation.support path to walk
90
     * @return boolean
91
     *         TRUE if $path points to a vlaid piece of data
92
     *         FALSE otherwise
93
     */
94
    public static function in($container, $path)
95
    {
96
        RequireReadableContainer::check($container);
97
98
        $method = self::lookupMethodFor($container, self::$dispatchTable);
99
        return self::$method($container, $path);
100
    }
101
102
    /**
103
     * does $path point to a valid piece of data inside $container?
104
     *
105
     * @param  array $container
106
     *         the container to look inside
107
     * @param  string $path
108
     *         the dot.notation.support path to walk
109
     * @return boolean
110
     *         TRUE if $path points to a vlaid piece of data
111
     *         FALSE otherwise
112
     */
113
    public static function inArray($container, $path)
114
    {
115
        // defensive programming!
116
        RequireIndexable::check($container, E4xx_UnsupportedType::class);
117
        RequireAnyOneOf::check([new IsDotNotationPath, new IsStringy], [$path], E4xx_NotDotNotationPath::class);
118
119
        try {
120
            DescendDotNotationPath::intoArray($container, $path);
121
            return true;
122
        }
123
        catch (E4xx_NoSuchIndex $e) {
124
            return false;
125
        }
126
        catch (E4xx_NoSuchProperty $e) {
127
            return false;
128
        }
129
    }
130
131
    /**
132
     * does $path point to a valid piece of data inside $container?
133
     *
134
     * @param  object $container
135
     *         the container to look inside
136
     * @param  string $path
137
     *         the dot.notation.support path to walk
138
     * @return boolean
139
     *         TRUE if $path points to a vlaid piece of data
140
     *         FALSE otherwise
141
     */
142
    public static function inObject($container, $path)
143
    {
144
        // defensive programming!
145
        RequireAssignable::check($container, E4xx_UnsupportedType::class);
146
        RequireAnyOneOf::check([new IsDotNotationPath, new IsStringy], [$path], E4xx_NotDotNotationPath::class);
147
148
        try {
149
            DescendDotNotationPath::intoObject($container, $path);
150
            return true;
151
        }
152
        catch (E4xx_NoSuchIndex $e) {
153
            return false;
154
        }
155
        catch (E4xx_NoSuchProperty $e) {
156
            return false;
157
        }
158
    }
159
160
    /**
161
     * lookup table of which method to call for which supported type
162
     *
163
     * @var array
164
     */
165
    private static $dispatchTable = [
166
        'Array' => 'inArray',
167
        'Object' => 'inObject',
168
    ];
169
}