1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2017 NanoSector |
5
|
|
|
* |
6
|
|
|
* You should have received a copy of the MIT license with the project. |
7
|
|
|
* See the LICENSE file for more information. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace ValidationClosures; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Reflection |
14
|
|
|
* @package ValidationClosures |
15
|
|
|
* |
16
|
|
|
* Taken from \ReflectionClass; all methods which could be used properly as a validation closure. |
17
|
|
|
* Nothing stops you from using the other methods though, but they'll be less useful as a validation mechanism. |
18
|
|
|
* @method static bool hasConstant(string $name) |
19
|
|
|
* @method static bool hasMethod(string $name) |
20
|
|
|
* @method static bool hasProperty(string $name) |
21
|
|
|
* @method static bool implementsInterface(string $interface) |
22
|
|
|
* @method static bool inNamespace() |
23
|
|
|
* @method static bool isAbstract() |
24
|
|
|
* @method static bool isAnonymous() |
25
|
|
|
* @method static bool isCloneable() |
26
|
|
|
* @method static bool isFinal() |
27
|
|
|
* @method static bool isInstance(object $object) |
28
|
|
|
* @method static bool isInstantiable() |
29
|
|
|
* @method static bool isInterface() |
30
|
|
|
* @method static bool isInternal() |
31
|
|
|
* @method static bool isIterateable() |
32
|
|
|
* @method static bool isSubclassOf(string $class) |
33
|
|
|
* @method static bool isTrait() |
34
|
|
|
* @method static bool isUserDefined() |
35
|
|
|
* |
36
|
|
|
*/ |
37
|
|
|
class Reflection |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @param string $class |
41
|
|
|
* |
42
|
|
|
* @return \ReflectionClass |
43
|
|
|
*/ |
44
|
2 |
|
public static function createReflectionObject(string $class): \ReflectionClass |
45
|
|
|
{ |
46
|
2 |
|
if (!class_exists($class)) |
47
|
1 |
|
throw new \InvalidArgumentException('The given class does not exist'); |
48
|
|
|
|
49
|
2 |
|
return new \ReflectionClass($class); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $method |
54
|
|
|
* @param array $arguments |
55
|
|
|
* |
56
|
|
|
* @return \Closure |
57
|
|
|
*/ |
58
|
1 |
|
public static function __callStatic(string $method, array $arguments): \Closure |
59
|
|
|
{ |
60
|
1 |
|
if (!method_exists(\ReflectionClass::class, $method)) |
61
|
1 |
|
throw new \InvalidArgumentException('Cannot create closure from method ReflectionClass::' . $method . ', it does not exist'); |
62
|
|
|
|
63
|
1 |
|
return function ($value) use ($method, $arguments) |
64
|
|
|
{ |
65
|
1 |
|
if (!Types::string()($value)) |
66
|
|
|
return false; |
67
|
|
|
|
68
|
1 |
|
$reflection = static::createReflectionObject($value); |
69
|
|
|
|
70
|
1 |
|
return call_user_func_array([$reflection, $method], $arguments); |
71
|
1 |
|
}; |
72
|
|
|
} |
73
|
|
|
} |