1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of Underscore.php |
6
|
|
|
* |
7
|
|
|
* (c) Maxime Fabre <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Underscore; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Various helpers relatives to methods. |
17
|
|
|
* |
18
|
|
|
* @see \Underscore\MethodTest |
19
|
|
|
*/ |
20
|
|
|
class Method |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* A list of methods to automatically defer to PHP. |
24
|
|
|
*/ |
25
|
|
|
public static array $defer = [ |
26
|
|
|
'trim', |
27
|
|
|
'count', |
28
|
|
|
'round', |
29
|
|
|
'ceil', |
30
|
|
|
'floor', |
31
|
|
|
'substr', |
32
|
|
|
'str_pad' => 'pad', |
33
|
|
|
'ucfirst', |
34
|
|
|
'lcfirst', |
35
|
|
|
'ucwords', |
36
|
|
|
'strtolower', |
37
|
|
|
'strtoupper', |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* A list of methods where the subject |
42
|
|
|
* isn't to be added to the arguments. |
43
|
|
|
*/ |
44
|
|
|
protected static array $subjectless = [ |
45
|
|
|
'fill', |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* A list of methods that are allowed |
50
|
|
|
* to break the chain. |
51
|
|
|
*/ |
52
|
|
|
protected static array $breakers = [ |
53
|
|
|
'get', |
54
|
|
|
'sum', |
55
|
|
|
'count', |
56
|
|
|
'fromJSON', |
57
|
|
|
'toJSON', |
58
|
|
|
'fromXML', |
59
|
|
|
'fromCSV', |
60
|
|
|
'toCSV', |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Unchainable methods. |
65
|
|
|
*/ |
66
|
|
|
protected static array $unchainable = [ |
67
|
|
|
'Arrays::range', |
68
|
|
|
'Arrays::repeat', |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
//////////////////////////////////////////////////////////////////// |
73
|
|
|
////////////////////////////// HELPERS ///////////////////////////// |
74
|
|
|
//////////////////////////////////////////////////////////////////// |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the Methods class from the Type class. |
78
|
|
|
* |
79
|
|
|
* @param string $class The Type class |
80
|
|
|
* |
81
|
|
|
* @return string The Methods class |
82
|
|
|
*/ |
83
|
|
|
public static function getMethodsFromType(string $class) : string |
84
|
|
|
{ |
85
|
|
|
return str_replace('Types', 'Methods', $class.'Methods'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Whether a native method requires a subject or not. |
90
|
|
|
* |
91
|
|
|
* @param string $method The function |
92
|
|
|
*/ |
93
|
|
|
public static function isSubjectless(string $method) : bool |
94
|
164 |
|
{ |
95
|
|
|
return \in_array($method, static::$subjectless, true); |
96
|
164 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Whether a method should not be chained. |
100
|
|
|
* |
101
|
|
|
* @param string $class The class |
102
|
|
|
* @param string $method The method |
103
|
|
|
*/ |
104
|
|
|
public static function isUnchainable(string $class, string $method) : bool |
105
|
|
|
{ |
106
|
22 |
|
$class = str_replace('Underscore\Types\\', '', $class); |
107
|
|
|
|
108
|
22 |
|
return \in_array($class.'::'.$method, static::$unchainable, true); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Whether a method is a breaker. |
113
|
|
|
* |
114
|
|
|
* @param string $method The method |
115
|
|
|
*/ |
116
|
|
|
public static function isBreaker(string $method) : bool |
117
|
|
|
{ |
118
|
|
|
return \in_array($method, static::$breakers, true); |
119
|
23 |
|
} |
120
|
|
|
|
121
|
23 |
|
/** |
122
|
|
|
* Get a method name by its alias. |
123
|
23 |
|
* |
124
|
|
|
* @param string $method The method |
125
|
|
|
* |
126
|
|
|
* @return string|null The real method name |
127
|
|
|
*/ |
128
|
|
|
public static function getAliasOf(string $method) : ?string |
129
|
|
|
{ |
130
|
|
|
return Underscore::option('aliases.'.$method); |
131
|
|
|
} |
132
|
|
|
|
133
|
22 |
|
/** |
134
|
|
|
* Get the native function corresponding to a method. |
135
|
22 |
|
* |
136
|
|
|
* @param string $method The method to look for |
137
|
|
|
* |
138
|
|
|
* @return string|false The native function |
139
|
|
|
*/ |
140
|
|
|
public static function getNative(string $method) : bool|string |
141
|
|
|
{ |
142
|
|
|
// If a defered method exist |
143
|
|
|
if (\in_array($method, static::$defer, true)) { |
144
|
|
|
$native = array_search($method, static::$defer, true); |
145
|
34 |
|
|
146
|
|
|
return \is_int($native) ? $method : $native; |
147
|
34 |
|
} |
148
|
|
|
|
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Find a method in the type classes. |
154
|
|
|
* |
155
|
|
|
* @param string $originalClass The class calling the method |
156
|
|
|
* @param string $method The method |
157
|
13 |
|
* |
158
|
|
|
* @return string The class name |
159
|
|
|
*/ |
160
|
13 |
|
public static function findInClasses(string $originalClass, string $method) : string |
161
|
2 |
|
{ |
162
|
|
|
$classes = ['Arrays', 'Collection', 'Functions', 'Number', 'BaseObject', 'Strings']; |
163
|
2 |
|
foreach ($classes as $class) { |
164
|
|
|
if (method_exists('\Underscore\Methods\\'.$class.'Methods', $method)) { |
165
|
|
|
return '\Underscore\Types\\'.$class; |
166
|
12 |
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $originalClass; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|