IsCompatibleWith::checkString()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 3
eloc 9
nc 4
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   Reflection/Checks
38
 * @author    Stuart Herbert <[email protected]>
39
 * @copyright 2015-present Ganbaro Digital Ltd www.ganbarodigital.com
40
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
41
 * @link      http://code.ganbarodigital.com/php-reflection
42
 */
43
44
namespace GanbaroDigital\Reflection\Checks;
45
46
use GanbaroDigital\Defensive\Requirements\RequireAnyOneOf;
47
use GanbaroDigital\Reflection\Checks\IsObject;
48
use GanbaroDigital\Reflection\Checks\IsStringy;
49
use GanbaroDigital\Reflection\Exceptions\E4xx_UnsupportedType;
50
use GanbaroDigital\Reflection\Requirements\RequireObject;
51
use GanbaroDigital\Reflection\Requirements\RequireStringy;
52
use GanbaroDigital\Reflection\ValueBuilders\AllMatchingTypesList;
53
use GanbaroDigital\Reflection\ValueBuilders\LookupMethodByType;
54
use GanbaroDigital\Reflection\ValueBuilders\SimpleType;
55
56
class IsCompatibleWith
57
{
58
    use LookupMethodByType;
59
60
    /**
61
     * is $data compatible with $constraint?
62
     *
63
     * @param  mixed $data
64
     *         the object to check
65
     * @param  string|object $constraint
66
     * @return boolean
67
     *         TRUE if $data is compatible
68
     *         FALSE otherwise
69
     */
70
    public function __invoke($data, $constraint)
71
    {
72
        return self::check($data, $constraint);
73
    }
74
75
    /**
76
     * is $data compatible with $constraint?
77
     *
78
     * @param  mixed $data
79
     *         the object to check
80
     * @param  string|object $constraint
81
     * @return boolean
82
     *         TRUE if $data is compatible
83
     *         FALSE otherwise
84
     */
85
    public static function check($data, $constraint)
86
    {
87
        $method = self::lookupMethodFor($data, self::$dispatchTable);
88
        return self::$method($data, $constraint);
89
    }
90
91
    /**
92
     * is $data compatible with $constraint?
93
     *
94
     * @param  object $data
95
     *         the object to check
96
     * @param  string|object $constraint
97
     *         the class or object that $data must be compatible with
98
     * @return boolean
99
     *         TRUE if $data is compatible
100
     *         FALSE otherwise
101
     */
102
    public static function checkObject($data, $constraint)
103
    {
104
        // defensive programming!
105
        RequireObject::check($data, E4xx_UnsupportedType::class);
106
        RequireAnyOneOf::check([ new IsObject, new IsStringy], [$constraint], E4xx_UnsupportedType::class);
107
108
        // this is the easiest test case of all :)
109
        return $data instanceof $constraint;
110
    }
111
112
    /**
113
     * is $data compatible with $constraint?
114
     *
115
     * @param  string $data
116
     *         the class name to check
117
     * @param  string|object $constraint
118
     *         the class or object that $data must be compatible with
119
     * @return boolean
120
     *         TRUE if $data is compatible
121
     *         FALSE otherwise
122
     */
123
    public static function checkString($data, $constraint)
124
    {
125
        // defensive programming!
126
        RequireStringy::check($data, E4xx_UnsupportedType::class);
127
        RequireAnyOneOf::check([ new IsObject, new IsStringy], [$constraint], E4xx_UnsupportedType::class);
128
129
        $compatibleTypes = AllMatchingTypesList::from($data);
130
        if (is_object($constraint)) {
131
            $constraint = get_class($constraint);
132
        }
133
134
        // is our constraint in the list of data types that $data can be?
135
        if (in_array($constraint, $compatibleTypes)) {
136
            return true;
137
        }
138
139
        // if we get here, we have run out of ideas
140
        return false;
141
    }
142
143
    /**
144
     * called when $data is a data type that we do not support
145
     *
146
     * @param  mixed $data
147
     * @return void
148
     */
149
    public static function nothingMatchesTheInputType($data)
150
    {
151
        throw new E4xx_UnsupportedType(SimpleType::from($data));
152
    }
153
154
    /**
155
     * our lookup table of which method to call for which supported data type
156
     * @var array
157
     */
158
    private static $dispatchTable = [
159
        'Object' => 'checkObject',
160
        'String' => 'checkString',
161
    ];
162
}
163