1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP: Nelson Martell Library file |
4
|
|
|
* |
5
|
|
|
* Copyright © 2014-2019 Nelson Martell (http://nelson6e65.github.io) |
6
|
|
|
* |
7
|
|
|
* Licensed under The MIT License (MIT) |
8
|
|
|
* For full copyright and license information, please see the LICENSE |
9
|
|
|
* Redistributions of files must retain the above copyright notice. |
10
|
|
|
* |
11
|
|
|
* @copyright 2014-2019 Nelson Martell |
12
|
|
|
* @link http://nelson6e65.github.io/php_nml/ |
13
|
|
|
* @since 0.1.1 |
14
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) |
15
|
|
|
* */ |
16
|
|
|
|
17
|
|
|
namespace NelsonMartell; |
18
|
|
|
|
19
|
|
|
use NelsonMartell\Extensions\Text; |
20
|
|
|
use NelsonMartell\Extensions\Arrays; |
21
|
|
|
use NelsonMartell\Extensions\Numbers; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Base class that encapsulates strict properties and other basic features. |
25
|
|
|
* |
26
|
|
|
* |
27
|
|
|
* @author Nelson Martell <[email protected]> |
28
|
|
|
* @since 0.1.1 |
29
|
|
|
* @see PropertiesHandler |
30
|
|
|
* */ |
31
|
|
|
class StrictObject implements IComparer, IStrictPropertiesContainer, IConvertibleToString |
32
|
|
|
{ |
33
|
|
|
use PropertiesHandler; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor. |
37
|
|
|
*/ |
38
|
436 |
|
public function __construct() |
39
|
|
|
{ |
40
|
436 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Convierte esta instancia en su representación de cadena. |
44
|
|
|
* Para modificar el funcionamiento de esta función, debe reemplazarse |
45
|
|
|
* la función ObjectClass::toString() |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
* @see StrictObject::toString() |
49
|
|
|
* */ |
50
|
34 |
|
final public function __toString() |
51
|
|
|
{ |
52
|
|
|
//$args = null; |
53
|
|
|
//list($args) = func_get_args(); |
54
|
34 |
|
return $this->toString(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Convierte la instancia actual en su representación de cadena. |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
* */ |
62
|
|
|
public function toString() |
63
|
|
|
{ |
64
|
|
|
$type = typeof($this); |
65
|
|
|
|
66
|
|
|
if (defined('CODE_ANALYSIS')) { |
67
|
|
|
if ($type->name != 'NelsonMartell\StrictObject') { |
68
|
|
|
$args = [ |
69
|
|
|
'access' => 'public', |
70
|
|
|
'base_class' => __CLASS__, |
71
|
|
|
'class' => $type->name, |
72
|
|
|
'function' => __FUNCTION__, |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
$msg = msg('Using default "{base_class}::{function}" ({access}) method.', $args); |
|
|
|
|
76
|
|
|
$msg .= msg( |
77
|
|
|
' You can replace (override) its behavior by creating "{class}::{function}" ({access}) method.', |
78
|
|
|
$args |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
trigger_error($msg, E_USER_NOTICE); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return '{ '.$type.' }'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Indicates whether the specified object is equal to the current instance. |
90
|
|
|
* |
91
|
|
|
* @param mixed $other Another object to compare equality. |
92
|
|
|
* |
93
|
|
|
* @return bool Retuns default behaviour of `==`. ***This method must be overridden***. |
94
|
|
|
* */ |
95
|
|
|
public function equals($other) |
96
|
|
|
{ |
97
|
|
|
if (defined('CODE_ANALYSIS')) { |
98
|
|
|
if ($this instanceof IEquatable) { |
99
|
|
|
$type = typeof($this); |
100
|
|
|
|
101
|
|
|
$args = [ |
102
|
|
|
'access' => 'public', |
103
|
|
|
'base_class' => __CLASS__, |
104
|
|
|
'class' => $type->name, |
105
|
|
|
'function' => __FUNCTION__, |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
$msg = msg( |
109
|
|
|
'You implemented IEquatable, but using default "{base_class}::{function}" ({access}) method.', |
110
|
|
|
$args |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
$msg .= msg( |
114
|
|
|
' You can replace (override) its behavior by creating "{class}::{function}" ({access}) method.', |
115
|
|
|
$args |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
trigger_error($msg, E_USER_NOTICE); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this == $other; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Determines the relative position of the object on the left with respect to the one on the right. |
127
|
|
|
* |
128
|
|
|
* This method is compatible with core types and other types. You can implement `NelsonMartell\IComparable` |
129
|
|
|
* in order to improve the beaviout for other classes. |
130
|
|
|
* |
131
|
|
|
* This method can be used as sorting function for `usort()` function. |
132
|
|
|
* |
133
|
|
|
* **Notes:** |
134
|
|
|
* - Comparison is made in natural way if they are of the same type. If not, is used the PHP standard |
135
|
|
|
* comparison. |
136
|
|
|
* - If ``$left`` and ``$right`` are arrays, comparison is made by first by 'key' (as strings) and then by |
137
|
|
|
* 'values' (using this method recursively). |
138
|
|
|
* |
139
|
|
|
* **Override:** |
140
|
|
|
* You can override this method to implement a contextual sorting behaviour for `usort()` function. |
141
|
|
|
* If you only need to compare instances of your class with other objects, implement `NelsonMartell\IComparable` |
142
|
|
|
* instead. |
143
|
|
|
* |
144
|
|
|
* @param mixed $left Left object. |
145
|
|
|
* @param mixed $right Right object. |
146
|
|
|
* |
147
|
|
|
* @return int|null |
148
|
|
|
* Returns: |
149
|
|
|
* - ``= 0`` if $left is considered equivalent to $other; |
150
|
|
|
* - ``> 0`` if $left is considered greater than $other; |
151
|
|
|
* - ``< 0`` if $left is considered less than $other; |
152
|
|
|
* - ``null`` if $left can't be compared to $other . |
153
|
|
|
* |
154
|
|
|
* @see \strnatcmp() |
155
|
|
|
* @see \usort() |
156
|
|
|
* @see Arrays::compare() |
157
|
|
|
* @see IComparable |
158
|
|
|
* @see IComparable::compareTo() |
159
|
|
|
* @see IComparer::compare() |
160
|
|
|
* @see Numbers::compare() |
161
|
|
|
* @see Text::compare() |
162
|
|
|
* */ |
163
|
127 |
|
public static function compare($left, $right) |
164
|
|
|
{ |
165
|
127 |
|
$r = null; |
166
|
|
|
|
167
|
127 |
|
if ($left instanceof IComparable) { |
168
|
10 |
|
$r = $left->compareTo($right); |
169
|
122 |
|
} elseif ($right instanceof IComparable) { |
170
|
11 |
|
$r = $right->compareTo($left); |
171
|
|
|
|
172
|
11 |
|
if ($r !== null) { |
173
|
11 |
|
$r *= -1; // Invert result |
174
|
|
|
} |
175
|
|
|
} else { |
176
|
112 |
|
$ltype = typeof($left); |
177
|
112 |
|
$rtype = typeof($right); |
178
|
|
|
|
179
|
112 |
|
if (typeof((bool) true)->isIn($left, $right)) { |
180
|
|
|
// Boolean compare ----------------------------------------- |
181
|
8 |
|
if (typeof((bool) true)->is($left, $right)) { |
182
|
4 |
|
$r = (int) $left - (int) $right; |
183
|
|
|
} else { |
184
|
8 |
|
$r = null; |
185
|
|
|
} |
186
|
104 |
|
} elseif (typeof((int) 0)->isIn($left, $right) || typeof((float) 0)->isIn($left, $right)) { |
187
|
|
|
// Numeric compare ----------------------------------------- |
188
|
47 |
|
$r = Numbers::compare($left, $right); |
189
|
57 |
|
} elseif (typeof((string) '')->isIn($left, $right)) { |
190
|
|
|
// String compare ------------------------------------------ |
191
|
29 |
|
$r = Text::compare($left, $right); |
192
|
36 |
|
} elseif (typeof((array) [])->isIn($left, $right)) { |
193
|
|
|
// Array compare ------------------------------------------- |
194
|
22 |
|
$r = Arrays::compare($left, $right); |
195
|
|
|
} else { |
196
|
14 |
|
if ($ltype->isCustom()) { |
197
|
12 |
|
if ($rtype->isCustom()) { |
198
|
10 |
|
if ($left == $right) { |
199
|
4 |
|
$r = 0; |
200
|
6 |
|
} elseif ($ltype->equals($rtype)) { |
201
|
4 |
|
$r = ($left > $right) ? +1 : -1; |
202
|
|
|
} else { |
203
|
10 |
|
$r = null; |
204
|
|
|
} |
205
|
|
|
} else { |
206
|
12 |
|
$r = 1; |
207
|
|
|
} |
208
|
2 |
|
} elseif ($rtype->isCustom()) { |
209
|
2 |
|
$r = -1; |
210
|
|
|
} else { |
211
|
|
|
$r = null; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
127 |
|
return $r; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.