1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpWinTools\WmiScripting\Testing\Wmi; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use ReflectionProperty; |
7
|
|
|
use ReflectionException; |
8
|
|
|
use PhpWinTools\WmiScripting\Models\Win32Model; |
9
|
|
|
use PhpWinTools\WmiScripting\Collections\ArrayCollection; |
10
|
|
|
|
11
|
|
|
class ModelProperties |
12
|
|
|
{ |
13
|
|
|
protected $class_name; |
14
|
|
|
|
15
|
|
|
protected $namespace; |
16
|
|
|
|
17
|
|
|
/** @var ArrayCollection */ |
18
|
|
|
protected $properties; |
19
|
|
|
|
20
|
2 |
|
public function __construct($class_name) |
21
|
|
|
{ |
22
|
2 |
|
$this->class_name = $class_name; |
23
|
|
|
|
24
|
2 |
|
$this->setProperties(); |
25
|
2 |
|
} |
26
|
|
|
|
27
|
2 |
|
public function getProperties() |
28
|
|
|
{ |
29
|
2 |
|
return $this->properties; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @throws ReflectionException |
34
|
|
|
*/ |
35
|
2 |
|
protected function setProperties() |
36
|
|
|
{ |
37
|
|
|
/** @var Win32Model $class */ |
38
|
2 |
|
$reflectionClass = new ReflectionClass($class = new $this->class_name()); |
39
|
|
|
|
40
|
2 |
|
$this->properties = (new ArrayCollection($reflectionClass->getProperties()))->mapWithKeys( |
41
|
|
|
function (ReflectionProperty $property) use ($class, $reflectionClass) { |
42
|
2 |
|
return [$property->name => [ |
43
|
2 |
|
'name' => $property->name, |
44
|
2 |
|
'origin' => $this->getPropertyOrigin($property, $reflectionClass)->getName(), |
45
|
2 |
|
'cast' => $class->getCast($property->name), |
46
|
2 |
|
'doc' => $property->getDocComment(), |
47
|
2 |
|
'value' => $class->getAttribute($property->name), |
48
|
|
|
]]; |
49
|
2 |
|
} |
50
|
|
|
)->diffKeys($class->getHiddenAttributes())->filter(function ($property) { |
51
|
2 |
|
return $property['origin'] !== Win32Model::class; |
52
|
2 |
|
}); |
53
|
2 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Finds the origin of the model property. Could be made slightly more efficient, but haven't figured it out yet. |
57
|
|
|
* |
58
|
|
|
* @param ReflectionProperty $property |
59
|
|
|
* @param ReflectionClass $current_class |
60
|
|
|
* @param ReflectionClass|null $previous_class |
61
|
|
|
* |
62
|
|
|
* @return ReflectionClass |
63
|
|
|
*/ |
64
|
2 |
|
protected function getPropertyOrigin( |
65
|
|
|
ReflectionProperty $property, |
66
|
|
|
ReflectionClass $current_class, |
67
|
|
|
ReflectionClass $previous_class = null |
68
|
|
|
) { |
69
|
2 |
|
if ($property->getDeclaringClass()->getName() !== $current_class->getName() && !$previous_class) { |
70
|
2 |
|
return $property->getDeclaringClass(); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
if (!$current_class->hasProperty($property->getName())) { |
74
|
1 |
|
return $previous_class; |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
if ($class = $current_class->getParentClass()) { |
78
|
1 |
|
return $this->getPropertyOrigin($property, $class, $current_class); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
return $current_class; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|