1 | <?php |
||
13 | class ClassMetadata |
||
14 | { |
||
15 | const LOGIN_PROPERTY = 1; |
||
16 | const PASSWORD_PROPERTY = 2; |
||
17 | const API_KEY_PROPERTY = 3; |
||
18 | const LAST_ACTION_PROPERTY = 4; |
||
19 | |||
20 | /** |
||
21 | * @var int[] |
||
22 | */ |
||
23 | private static $necessaryProperties = array( |
||
24 | self::LOGIN_PROPERTY => 'login', |
||
25 | self::PASSWORD_PROPERTY => 'password', |
||
26 | self::API_KEY_PROPERTY => 'apiKey', |
||
27 | ); |
||
28 | |||
29 | /** |
||
30 | * @var ReflectionClass |
||
31 | */ |
||
32 | private $reflectionInstance; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $className; |
||
38 | |||
39 | /** |
||
40 | * @var ReflectionProperty[] |
||
41 | */ |
||
42 | private $properties = array(); |
||
43 | |||
44 | /** |
||
45 | * @var string[] |
||
46 | */ |
||
47 | private $lazyPropertyNameCache = array(); |
||
48 | |||
49 | /** |
||
50 | * @var string[] |
||
51 | */ |
||
52 | private $lazyValueCache = array(); |
||
53 | |||
54 | /** |
||
55 | * Constructor. |
||
56 | * |
||
57 | * @param ReflectionClass $reflection |
||
58 | * @param string $className |
||
59 | * @param ReflectionProperty[] $properties |
||
60 | * |
||
61 | * @throws \InvalidArgumentException If one necessary property is missing |
||
62 | */ |
||
63 | 10 | public function __construct(ReflectionClass $reflection, $className, array $properties) |
|
78 | |||
79 | /** |
||
80 | * Gets the value of the given property. |
||
81 | * |
||
82 | * @param object $user |
||
83 | * @param int $property |
||
84 | * @param bool $strict |
||
85 | * |
||
86 | * @return mixed |
||
87 | */ |
||
88 | 1 | public function getPropertyValue($user, $property = self::LOGIN_PROPERTY, $strict = false) |
|
105 | |||
106 | /** |
||
107 | * Gets the name of a specific property by its metadata constant. |
||
108 | * |
||
109 | * @param int $property |
||
110 | * @param bool $strict |
||
111 | * |
||
112 | * @return null|string |
||
113 | */ |
||
114 | 3 | public function getPropertyName($property = self::LOGIN_PROPERTY, $strict = false) |
|
115 | { |
||
116 | 3 | if ($this->checkProperty($property, $strict)) { |
|
117 | 3 | if (isset($this->lazyPropertyNameCache[$property])) { |
|
118 | return $this->lazyPropertyNameCache[$property]; |
||
119 | } |
||
120 | |||
121 | 3 | return $this->lazyPropertyNameCache[$property] = $this->properties[$property]->getName(); |
|
122 | } |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Modifies a property and clears the cache. |
||
127 | * |
||
128 | * @param object $user |
||
129 | * @param mixed $newValue |
||
130 | * @param int $property |
||
131 | */ |
||
132 | 2 | public function modifyProperty($user, $newValue, $property = self::LOGIN_PROPERTY) |
|
142 | |||
143 | /** |
||
144 | * Validates a property. |
||
145 | * |
||
146 | * @param int $property |
||
147 | * @param bool $strict |
||
148 | * |
||
149 | * @return bool |
||
150 | */ |
||
151 | 4 | private function checkProperty($property = self::LOGIN_PROPERTY, $strict = false) |
|
166 | } |
||
167 |