1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2016-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 MissingBits/TypeChecks |
38
|
|
|
* @author Stuart Herbert <[email protected]> |
39
|
|
|
* @copyright 2016-present Ganbaro Digital Ltd www.ganbarodigital.com |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @link http://ganbarodigital.github.io/php-the-missing-bits |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace GanbaroDigital\MissingBits\TypeChecks; |
45
|
|
|
|
46
|
|
|
use GanbaroDigital\MissingBits\Checks\Check; |
47
|
|
|
use GanbaroDigital\MissingBits\TypeInspectors\GetClassTypes; |
48
|
|
|
use GanbaroDigital\MissingBits\TypeInspectors\GetPrintableType; |
49
|
|
|
use TypeError; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* is class or object compatible with a given type? |
53
|
|
|
*/ |
54
|
|
|
class IsCompatibleWith implements Check |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* the classname or object that we want to check for |
58
|
|
|
* @var string|object |
59
|
|
|
*/ |
60
|
|
|
private $expectedType; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* our constructor |
64
|
|
|
* |
65
|
|
|
* @param string|object $expectedType |
66
|
|
|
* the classname or object that we want to check for |
67
|
|
|
*/ |
68
|
|
|
public function __construct($expectedType) |
69
|
|
|
{ |
70
|
|
|
$this->expectedType = $expectedType; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* fluent interface entry point |
75
|
|
|
* |
76
|
|
|
* @param string|object $expectedType |
77
|
|
|
* the classname or object that we want to check for |
78
|
|
|
* @return IsCompatibleWith |
79
|
|
|
* the customised Check, ready to use |
80
|
|
|
*/ |
81
|
|
|
public static function using($expectedType) |
82
|
|
|
{ |
83
|
|
|
return new static($expectedType); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* is $fieldOrVar compatible with $expectedType? |
88
|
|
|
* |
89
|
|
|
* @param mixed $fieldOrVar |
90
|
|
|
* the classname or object to check |
91
|
|
|
* @param string|object $expectedType |
92
|
|
|
* the classname or object that we want to check for |
93
|
|
|
* @return bool |
94
|
|
|
* TRUE if $fieldOrVar is compatible |
95
|
|
|
* FALSE otherwise |
96
|
|
|
*/ |
97
|
|
|
public static function check($fieldOrVar, $expectedType) |
98
|
|
|
{ |
99
|
|
|
// robustness |
100
|
|
|
if (!IsStringy::check($expectedType) && !is_object($expectedType)) { |
101
|
|
|
throw new TypeError("\$expectedType must be a classname or an object, is a " . GetPrintableType::of($expectedType)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (is_object($fieldOrVar)) { |
105
|
|
|
return self::checkObject($fieldOrVar, $expectedType); |
106
|
|
|
} |
107
|
|
|
else if (is_string($fieldOrVar)) { |
108
|
|
|
return self::checkString($fieldOrVar, $expectedType); |
109
|
|
|
} |
110
|
|
|
else { |
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* is $fieldOrVar compatible with $expectedType? |
117
|
|
|
* |
118
|
|
|
* @param object $fieldOrVar |
119
|
|
|
* the object to check |
120
|
|
|
* @param string|object $expectedType |
121
|
|
|
* the class or object that $fieldOrVar must be compatible with |
122
|
|
|
* @return bool |
123
|
|
|
* TRUE if $fieldOrVar is compatible |
124
|
|
|
* FALSE otherwise |
125
|
|
|
*/ |
126
|
|
|
private static function checkObject($fieldOrVar, $expectedType) |
127
|
|
|
{ |
128
|
|
|
// this is the easiest test case of all :) |
129
|
|
|
return $fieldOrVar instanceof $expectedType; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* is $fieldOrVar compatible with $expectedType? |
134
|
|
|
* |
135
|
|
|
* @param string $fieldOrVar |
136
|
|
|
* the class name to check |
137
|
|
|
* @param string|object $expectedType |
138
|
|
|
* the class or object that $fieldOrVar must be compatible with |
139
|
|
|
* @return bool |
140
|
|
|
* TRUE if $fieldOrVar is compatible |
141
|
|
|
* FALSE otherwise |
142
|
|
|
*/ |
143
|
|
|
private static function checkString($fieldOrVar, $expectedType) |
144
|
|
|
{ |
145
|
|
|
$compatibleTypes = GetClassTypes::from($fieldOrVar); |
146
|
|
|
if (is_object($expectedType)) { |
147
|
|
|
$expectedType = get_class($expectedType); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// is our expectedType in the list of data types that $fieldOrVar can be? |
151
|
|
|
if (isset($compatibleTypes[$expectedType])) { |
152
|
|
|
return true; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// if we get here, we have run out of ideas |
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* is $fieldOrVar compatible with $expectedType? |
161
|
|
|
* |
162
|
|
|
* @param mixed $fieldOrVar |
163
|
|
|
* the classname or object to check |
164
|
|
|
* @return bool |
165
|
|
|
* TRUE if $fieldOrVar is compatible |
166
|
|
|
* FALSE otherwise |
167
|
|
|
*/ |
168
|
|
|
public function inspect($fieldOrVar) |
169
|
|
|
{ |
170
|
|
|
return static::check($fieldOrVar, $this->expectedType); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|