-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathClassUtils.php
More file actions
185 lines (165 loc) · 5.95 KB
/
ClassUtils.php
File metadata and controls
185 lines (165 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* This file is part of the Cloudinary PHP package.
*
* (c) Cloudinary
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cloudinary;
use ReflectionClass;
use ReflectionException;
/**
* Class ClassUtils
*
* @internal
*/
class ClassUtils
{
/**
* Gets class name from the instance object.
*
* @param object $instance The instance object
*
*/
public static function getClassName(object $instance): string
{
return self::getBaseName($instance::class);
}
/**
* Gets class base name from fully qualified class name.
*
* @param string $className The fully qualified class name.
*
* @return string class base name
*/
public static function getBaseName(string $className): string
{
$pos = strrpos($className, '\\');
if ($pos) {
return substr($className, $pos + 1);
}
return $className;
}
/**
* Gets class constants.
*
* @param object|class-string $instance The instance object.
* @param array $exclusions The list of constants to exclude.
*
* @return array of class constants
*/
public static function getConstants(object|string $instance, array $exclusions = []): array
{
$constants = [];
try {
$reflectionClass = new ReflectionClass($instance);
$constants = array_values($reflectionClass->getConstants());
} catch (ReflectionException) {
//TODO: log it?
}
return array_diff($constants, $exclusions);
}
/**
* Verifies that provided object is an instance of the $baseClass.
*
* If not, the new instance of the $instanceClass is created (with fallback to the $baseClass, if not provided)
* Additional $params can be passed to the constructor as well.
*
* Example: $t = ClassUtils::verifyInstance($notSureIfT, CommonTransformation::class, Transformation::class)
*
* If $notSureIfT is a string, then $t will be a new Transformation initialized with the value of $notSureIfT.
* In case $notSureIfT is already an instance of CommonTransformation, it is returned as is.
*
* @param mixed $object The value to verify
* @param string $baseClass Base class name
* @param string|null $instanceClass Instance class to create in case $object is not derivative of the $baseClass.
* $baseClass is used in case it is not provided.
* @param array $params Additional parameters for the constructor
*
*/
public static function forceInstance(
mixed $object,
string $baseClass,
?string $instanceClass = null,
...$params
): mixed {
if (! $object instanceof $baseClass) {
$instanceClass = $instanceClass ?: $baseClass;
$object = new $instanceClass($object, ...$params);
}
return $object;
}
/**
* Similar to ClassUtils::forceInstance, but does not propagate null values.
*
* @param object|mixed $object The value to verify
* @param string $baseClass Base class name
* @param string|null $instanceClass Instance class to create in case $object is not derivative of the $baseClass.
* $baseClass is used in case it is not provided.
* @param array $params Additional parameters for the constructor
*
*
* @see ClassUtils::forceInstance
*/
public static function verifyInstance(
mixed $object,
string $baseClass,
?string $instanceClass = null,
...$params
): mixed {
if ($object === null) { // no propagation of null objects
return null;
}
return self::forceInstance($object, $baseClass, $instanceClass, ...$params);
}
/**
* Variable arguments version of the ClassUtils::forceInstance.
*
* @param mixed $varArgs Arguments to verify
* @param string $baseClass Base class name
* @param string|null $instanceClass Instance class to create in case $object is not derivative of the $baseClass.
* $baseClass is used in case it is not provided.
*
*
* @see ClassUtils::verifyInstance
*/
public static function forceVarArgsInstance(mixed $varArgs, string $baseClass, ?string $instanceClass = null): mixed
{
$varArgs = ArrayUtils::build($varArgs);
// When passing array instead of varargs, unwrap it and proceed
if (count($varArgs) === 1 && is_array($varArgs[0])) {
$varArgs = $varArgs[0];
}
if (count($varArgs) === 1) {
return self::verifyInstance($varArgs[0], $baseClass, $instanceClass);
}
// At this point we create a new instance of a desired class and pass all args to it,
// hopefully it'll know what to do with them :)
$instanceClass = $instanceClass ?: $baseClass;
return new $instanceClass(...$varArgs);
}
/**
* Variable arguments version of the ClassUtils::verifyInstance.
*
* @param mixed $varArgs Arguments to verify
* @param string $baseClass Base class name
* @param string|null $instanceClass Instance class to create in case $object is not derivative of the $baseClass.
* $baseClass is used in case it is not provided.
*
*
* @see ClassUtils::verifyInstance
*/
public static function verifyVarArgsInstance(
mixed $varArgs,
string $baseClass,
?string $instanceClass = null
): mixed {
// No args, nothing to verify
if (empty($varArgs)) {
return null;
}
return self::forceVarArgsInstance($varArgs, $baseClass, $instanceClass);
}
}