|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP: Nelson Martell Library file |
|
4
|
|
|
* |
|
5
|
|
|
* Content: |
|
6
|
|
|
* - Class definition: [NelsonMartell] Object |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright © 2014-2017 Nelson Martell (http://nelson6e65.github.io) |
|
9
|
|
|
* |
|
10
|
|
|
* Licensed under The MIT License (MIT) |
|
11
|
|
|
* For full copyright and license information, please see the LICENSE |
|
12
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
13
|
|
|
* |
|
14
|
|
|
* @copyright 2014-2017 Nelson Martell |
|
15
|
|
|
* @link http://nelson6e65.github.io/php_nml/ |
|
16
|
|
|
* @since 0.1.1 |
|
17
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) |
|
18
|
|
|
* */ |
|
19
|
|
|
|
|
20
|
|
|
namespace NelsonMartell; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Base class that encapsulates strict properties and other basic features. |
|
24
|
|
|
* |
|
25
|
|
|
* |
|
26
|
|
|
* @author Nelson Martell <[email protected]> |
|
27
|
|
|
* @since 0.1.1 |
|
28
|
|
|
* @see PropertiesHandler |
|
29
|
|
|
* */ |
|
30
|
|
|
class StrictObject implements IComparer, IStrictPropertiesContainer, IConvertibleToString |
|
31
|
|
|
{ |
|
32
|
|
|
use PropertiesHandler; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Constructor. |
|
36
|
|
|
*/ |
|
37
|
320 |
|
public function __construct() |
|
38
|
|
|
{ |
|
39
|
320 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Convierte esta instancia en su representación de cadena. |
|
43
|
|
|
* Para modificar el funcionamiento de esta función, debe reemplazarse |
|
44
|
|
|
* la función ObjectClass::toString() |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
* @see StrictObject::toString() |
|
48
|
|
|
* */ |
|
49
|
34 |
|
final public function __toString() |
|
50
|
|
|
{ |
|
51
|
|
|
//$args = null; |
|
52
|
|
|
//list($args) = func_get_args(); |
|
53
|
34 |
|
return $this->toString(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Convierte la instancia actual en su representación de cadena. |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
* */ |
|
61
|
|
|
public function toString() |
|
62
|
|
|
{ |
|
63
|
|
|
$type = typeof($this); |
|
64
|
|
|
|
|
65
|
|
|
if (defined('CODE_ANALYSIS')) { |
|
66
|
|
|
if ($type->Name != 'NelsonMartell\StrictObject') { |
|
67
|
|
|
$args = [ |
|
68
|
|
|
'access' => 'public', |
|
69
|
|
|
'base_class' => __CLASS__, |
|
70
|
|
|
'class' => $type->Name, |
|
71
|
|
|
'function' => __FUNCTION__, |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
$msg = msg('Using default "{base_class}::{function}" ({access}) method.', $args); |
|
|
|
|
|
|
75
|
|
|
$msg .= msg( |
|
76
|
|
|
' You can replace (override) its behavior by creating "{class}::{function}" ({access}) method.', |
|
|
|
|
|
|
77
|
|
|
$args |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
trigger_error($msg, E_USER_NOTICE); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return '{ '.$type.' }'; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Indica si el objeto especificado es igual a la instancia actual. |
|
89
|
|
|
* |
|
90
|
|
|
* Note: This methods must to be overriden. |
|
91
|
|
|
* |
|
92
|
|
|
* @param mixed $other Another object to compare equality. |
|
93
|
|
|
* |
|
94
|
|
|
* @return bool |
|
95
|
|
|
* */ |
|
96
|
|
|
public function equals($other) |
|
97
|
|
|
{ |
|
98
|
|
|
if (defined('CODE_ANALYSIS')) { |
|
99
|
|
|
if ($this instanceof IEquatable) { |
|
100
|
|
|
$type = typeof($this); |
|
101
|
|
|
|
|
102
|
|
|
$args = [ |
|
103
|
|
|
'access' => 'public', |
|
104
|
|
|
'base_class' => __CLASS__, |
|
105
|
|
|
'class' => $type->Name, |
|
106
|
|
|
'function' => __FUNCTION__, |
|
107
|
|
|
]; |
|
108
|
|
|
|
|
109
|
|
|
$msg = msg( |
|
110
|
|
|
'You implemented IEquatable, but using default "{base_class}::{function}" ({access}) method.', |
|
|
|
|
|
|
111
|
|
|
$args |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
$msg .= msg( |
|
115
|
|
|
' You can replace (override) its behavior by creating "{class}::{function}" ({access}) method.', |
|
|
|
|
|
|
116
|
|
|
$args |
|
117
|
|
|
); |
|
118
|
|
|
|
|
119
|
|
|
trigger_error($msg, E_USER_NOTICE); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $this == $other; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Determina la posición relativa del objeto de la izquierda con respecto al de la derecha. |
|
128
|
|
|
* |
|
129
|
|
|
* Compatible with, strings, integers, boolean, arrays and classes implementing ``IComparable`` interface. |
|
|
|
|
|
|
130
|
|
|
* |
|
131
|
|
|
* Puede usarse como segundo argumento en la función de ordenamiento de |
|
132
|
|
|
* arrays 'usort'. |
|
133
|
|
|
* |
|
134
|
|
|
* Notes: |
|
135
|
|
|
* - Comparison is made in natural way if they are of the same type. If not, is used the PHP standard |
|
|
|
|
|
|
136
|
|
|
* comparison. |
|
137
|
|
|
* - If ``$left`` and ``$right`` are arrays, comparison is made by first by 'key' (as strings) and then by |
|
|
|
|
|
|
138
|
|
|
* 'values' (using this method recursively). |
|
139
|
|
|
* |
|
140
|
|
|
* # Override |
|
141
|
|
|
* You can override this method to implement a contextual sorting behaviour for ``usort()`` function. |
|
|
|
|
|
|
142
|
|
|
* If you only need to compare instances of your class with other objects, implement |
|
143
|
|
|
* ``NelsonMartell\IComparable`` instead. |
|
144
|
|
|
* |
|
145
|
|
|
* @param mixed $left Left object. |
|
146
|
|
|
* @param mixed $right Right object. |
|
147
|
|
|
* |
|
148
|
|
|
* @return int|null |
|
149
|
|
|
* Returns: |
|
150
|
|
|
* - ``= 0`` if $left is considered equivalent to $other; |
|
151
|
|
|
* - ``> 0`` if $left is considered greater than $other; |
|
152
|
|
|
* - ``< 0`` if $left is considered less than $other; |
|
153
|
|
|
* - ``null`` if $left can't be compared to $other . |
|
154
|
|
|
* @see IComparer::compare() |
|
155
|
|
|
* @see IComparable::compareTo() |
|
156
|
|
|
* @see \strnatcmp() |
|
157
|
|
|
* @see \usort() |
|
158
|
|
|
* */ |
|
159
|
64 |
|
public static function compare($left, $right) |
|
160
|
|
|
{ |
|
161
|
64 |
|
$r = null; |
|
162
|
|
|
|
|
163
|
64 |
|
if ($left instanceof IComparable) { |
|
164
|
7 |
|
$r = $left->compareTo($right); |
|
165
|
58 |
|
} elseif ($right instanceof IComparable) { |
|
166
|
12 |
|
$r = $right->compareTo($left); |
|
167
|
|
|
|
|
168
|
12 |
|
if ($r !== null) { |
|
169
|
12 |
|
$r *= -1; // Invert result |
|
170
|
|
|
} |
|
171
|
|
|
} else { |
|
172
|
50 |
|
$ltype = typeof($left); |
|
173
|
50 |
|
$rtype = typeof($right); |
|
174
|
|
|
|
|
175
|
|
|
// If they are of the same type. |
|
176
|
50 |
|
if ($ltype->Name === $rtype->Name) { |
|
177
|
50 |
|
switch ($ltype->Name) { |
|
178
|
50 |
|
case 'string': |
|
179
|
22 |
|
$r = strnatcmp($left, $right); |
|
180
|
22 |
|
break; |
|
181
|
|
|
|
|
182
|
36 |
|
case 'boolean': |
|
183
|
|
|
$r = (int) $left - (int) $right; |
|
184
|
|
|
break; |
|
185
|
|
|
|
|
186
|
36 |
|
case 'integer': |
|
187
|
18 |
|
$r = $left - $right; |
|
188
|
18 |
|
break; |
|
189
|
|
|
|
|
190
|
18 |
|
case 'float': |
|
191
|
18 |
|
case 'double': |
|
192
|
|
|
$r = (int) ceil($left - $right); |
|
193
|
|
|
break; |
|
194
|
|
|
|
|
195
|
18 |
|
case 'array': |
|
196
|
14 |
|
$r = count($left) - count($right); |
|
197
|
|
|
|
|
198
|
14 |
|
if ($r === 0) { |
|
199
|
8 |
|
reset($left); |
|
200
|
8 |
|
reset($right); |
|
201
|
|
|
|
|
202
|
|
|
do { |
|
203
|
8 |
|
$lKey = key($left); |
|
|
|
|
|
|
204
|
8 |
|
$lValue = current($left); |
|
205
|
8 |
|
$rKey = key($right); |
|
|
|
|
|
|
206
|
8 |
|
$rValue = current($right); |
|
207
|
|
|
|
|
208
|
8 |
|
$r = static::compare((string) $lKey, (string) $rKey); |
|
209
|
|
|
|
|
210
|
8 |
|
if ($r === 0) { |
|
211
|
|
|
// Recursive call to compare values |
|
212
|
6 |
|
$r = static::compare($lValue, $rValue); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
8 |
|
next($left); |
|
216
|
8 |
|
next($right); |
|
217
|
8 |
|
} while (key($left) !== null && key($right) !== null && $r === 0); |
|
218
|
|
|
} |
|
219
|
14 |
|
break; |
|
220
|
|
|
|
|
221
|
|
|
default: |
|
222
|
4 |
|
if ($left == $right) { |
|
223
|
2 |
|
$r = 0; |
|
224
|
|
|
} else { |
|
225
|
50 |
|
$r = ($left > $right) ? +1 : -1; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
} else { |
|
229
|
|
|
if ($left == $right) { |
|
230
|
|
|
$r = 0; |
|
231
|
|
|
} elseif ($left > $right) { |
|
232
|
|
|
$r = 1; |
|
233
|
|
|
} elseif ($left < $right) { |
|
234
|
|
|
$r = -1; |
|
235
|
|
|
} else { |
|
236
|
|
|
// If can't determinate, retuns null |
|
237
|
|
|
$r = null; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
64 |
|
return $r; |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
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.