IsDotNotationPath   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 139
rs 10
c 2
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 4 1
A check() 0 5 1
A in() 0 4 1
A inString() 0 4 1
A checkString() 0 13 2
A nothingMatchesTheInputType() 0 4 1
A hasDotInAcceptablePlace() 0 18 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_UnsupportedType;
48
use GanbaroDigital\Reflection\Requirements\RequireStringy;
49
use GanbaroDigital\Reflection\ValueBuilders\LookupMethodByType;
50
use GanbaroDigital\Reflection\ValueBuilders\SimpleType;
51
52
class IsDotNotationPath
53
{
54
    use LookupMethodByType;
55
56
    /**
57
     * do we have a dot.notation string at all?
58
     *
59
     * @param  mixed $item
60
     *         the item to examine
61
     * @return boolean
62
     *         TRUE if the string is in dot.notation
63
     *         FALSE otherwise
64
     */
65
    public function __invoke($item)
66
    {
67
        return self::check($item);
68
    }
69
70
    /**
71
     * do we have a dot.notation string at all?
72
     *
73
     * @param  mixed $item
74
     *         the item to examine
75
     * @return boolean
76
     *         TRUE if the string is in dot.notation
77
     *         FALSE otherwise
78
     */
79
    public static function check($item)
80
    {
81
        $method = self::lookupMethodFor($item, self::$dispatchTable);
82
        return self::$method($item);
83
    }
84
85
    /**
86
     * do we have a dot.notation string at all?
87
     *
88
     * @deprecated since 2.3.0
89
     * @codeCoverageIgnore
90
     *
91
     * @param  mixed $item
92
     *         the item to examine
93
     * @return boolean
94
     *         TRUE if the string is in dot.notation
95
     *         FALSE otherwise
96
     */
97
    public static function in($item)
98
    {
99
        return self::check($item);
100
    }
101
102
    /**
103
     * do we have a dot.notation string at all?
104
     *
105
     * @deprecated since 2.3.0
106
     * @codeCoverageIgnore
107
     *
108
     * @param  string $item
109
     *         the item to examine
110
     * @return boolean
111
     *         TRUE if the string is in dot.notation
112
     *         FALSE otherwise
113
     */
114
    public static function inString($item)
115
    {
116
        return self::checkString($item);
117
    }
118
119
    /**
120
     * do we have a dot.notation string at all?
121
     *
122
     * @param  string $item
123
     *         the item to examine
124
     * @return boolean
125
     *         TRUE if the string is in dot.notation
126
     *         FALSE otherwise
127
     */
128
    public static function checkString($item)
129
    {
130
        // robustness!!
131
        RequireStringy::check($item, E4xx_UnsupportedType::class);
132
133
        // make sure we have a dot somewhere we like
134
        if (!self::hasDotInAcceptablePlace((string)$item)) {
135
            return false;
136
        }
137
138
        // if we get here, we're happy
139
        return true;
140
    }
141
142
    /**
143
     * called when there's no entry in our dispatch table that matches
144
     * $item's data type
145
     *
146
     * @param  mixed $item
147
     *         the item that we cannot process
148
     * @return void
149
     */
150
    public static function nothingMatchesTheInputType($item)
151
    {
152
        throw new E4xx_UnsupportedType(SimpleType::from($item));
153
    }
154
155
    /**
156
     * do we have a dot, and is it somewhere we're happy with?
157
     *
158
     * @param  string $item
159
     * @return boolean
160
     *         FALSE if there is no '.' anywhere
161
     *         FALSE if the first '.' is at the end of the string
162
     *         TRUE otherwise
163
     */
164
    private static function hasDotInAcceptablePlace($item)
165
    {
166
        // where is the dot?
167
        $firstDot = strpos($item, '.');
168
169
        // do we even have one?
170
        if (false === $firstDot) {
171
            return true;
172
        }
173
174
        // can't be at the end of the string
175
        if ($firstDot === strlen($item) - 1) {
176
            return false;
177
        }
178
179
        // this is okay
180
        return true;
181
    }
182
183
    /**
184
     * our lookup table of which method to call for which supported data type
185
     * @var array
186
     */
187
    private static $dispatchTable = [
188
        'String' => 'checkString',
189
    ];
190
}
191