|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Helldar\Support\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Helldar\Support\Facades\Helpers\Arr as ArrHelper; |
|
6
|
|
|
use Helldar\Support\Facades\Helpers\Is as IsHelper; |
|
7
|
|
|
use Helldar\Support\Facades\Helpers\Reflection as ReflectionHelper; |
|
8
|
|
|
use ReflectionClass; |
|
9
|
|
|
|
|
10
|
|
|
final class Instance |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Checks if the item being checked inherits from other objects and interfaces. |
|
14
|
|
|
* |
|
15
|
|
|
* @param object|string $haystack |
|
16
|
|
|
* @param string|string[] $needles |
|
17
|
|
|
* |
|
18
|
|
|
* @return bool |
|
19
|
|
|
*/ |
|
20
|
88 |
|
public function of($haystack, $needles): bool |
|
21
|
|
|
{ |
|
22
|
88 |
|
if (! $this->exists($haystack)) { |
|
23
|
76 |
|
return false; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
36 |
|
$reflection = $this->resolve($haystack); |
|
27
|
36 |
|
$classname = $this->classname($haystack); |
|
28
|
|
|
|
|
29
|
36 |
|
foreach (ArrHelper::wrap($needles) as $needle) { |
|
30
|
36 |
|
if (! $this->exists($needle)) { |
|
31
|
2 |
|
continue; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if ( |
|
35
|
36 |
|
$haystack instanceof $needle || |
|
36
|
14 |
|
$classname === $this->classname($needle) || |
|
37
|
14 |
|
$reflection->isSubclassOf($needle) || |
|
38
|
36 |
|
($reflection->isInterface() && $reflection->implementsInterface($needle)) |
|
39
|
|
|
) { |
|
40
|
28 |
|
return true; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
14 |
|
return false; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Extract the trailing name component from a file path. |
|
49
|
|
|
* |
|
50
|
|
|
* @param object|string $class |
|
51
|
|
|
* |
|
52
|
|
|
* @return string|null |
|
53
|
|
|
*/ |
|
54
|
2 |
|
public function basename($class): ?string |
|
55
|
|
|
{ |
|
56
|
2 |
|
$class = $this->classname($class); |
|
57
|
|
|
|
|
58
|
2 |
|
return basename(str_replace('\\', '/', $class)) ?: null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Gets the class name of the object. |
|
63
|
|
|
* |
|
64
|
|
|
* @param object|string|null $class |
|
65
|
|
|
* |
|
66
|
|
|
* @return string|null |
|
67
|
|
|
*/ |
|
68
|
42 |
|
public function classname($class = null): ?string |
|
69
|
|
|
{ |
|
70
|
42 |
|
if (IsHelper::object($class)) { |
|
71
|
40 |
|
return get_class($class); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
20 |
|
return class_exists($class) || interface_exists($class) ? $class : null; |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Checks if the object exists. |
|
79
|
|
|
* |
|
80
|
|
|
* @param object|string $haystack |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool |
|
83
|
|
|
*/ |
|
84
|
90 |
|
public function exists($haystack): bool |
|
85
|
|
|
{ |
|
86
|
90 |
|
if (IsHelper::object($haystack)) { |
|
87
|
38 |
|
return true; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
90 |
|
return IsHelper::string($haystack) ? class_exists($haystack) || interface_exists($haystack) : false; |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Creates a ReflectionClass object. |
|
95
|
|
|
* |
|
96
|
|
|
* @param object|ReflectionClass|string $class |
|
97
|
|
|
* |
|
98
|
|
|
* @return \ReflectionClass |
|
99
|
|
|
*/ |
|
100
|
36 |
|
protected function resolve($class): ReflectionClass |
|
101
|
|
|
{ |
|
102
|
36 |
|
return ReflectionHelper::resolve($class); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|