1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ma27\ApiKeyAuthenticationBundle\Model\User; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use ReflectionProperty; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Metadata object that contains the necessary data of the user model. |
10
|
|
|
* |
11
|
|
|
* @internal This code is part of the internal API to gather the appropriate model information and shouldn't be used for else use-cases |
12
|
|
|
*/ |
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 ReflectionProperty[] |
31
|
|
|
*/ |
32
|
|
|
private $properties = array(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string[] |
36
|
|
|
*/ |
37
|
|
|
private $lazyPropertyNameCache = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string[] |
41
|
|
|
*/ |
42
|
|
|
private $lazyValueCache = array(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor. |
46
|
|
|
* |
47
|
|
|
* @param ReflectionProperty[] $properties |
48
|
|
|
* |
49
|
|
|
* @throws \InvalidArgumentException If one necessary property is missing |
50
|
|
|
*/ |
51
|
12 |
|
public function __construct(array $properties) |
52
|
|
|
{ |
53
|
12 |
|
$this->properties = $properties; |
54
|
|
|
|
55
|
12 |
|
foreach (self::$necessaryProperties as $propertyIndex => $alias) { |
56
|
12 |
|
if (!isset($this->properties[$propertyIndex])) { |
57
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
58
|
1 |
|
'Missing required property "%s"!', |
59
|
|
|
$alias |
60
|
1 |
|
)); |
61
|
|
|
} |
62
|
12 |
|
} |
63
|
11 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Gets the value of the given property. |
67
|
|
|
* |
68
|
|
|
* @param object $user |
69
|
|
|
* @param int $property |
70
|
|
|
* @param bool $strict |
71
|
|
|
* |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
5 |
|
public function getPropertyValue($user, $property = self::LOGIN_PROPERTY, $strict = false) |
75
|
|
|
{ |
76
|
5 |
|
if ($this->checkProperty($property, $strict)) { |
77
|
5 |
|
$oid = spl_object_hash($user); |
78
|
5 |
|
if (isset($this->lazyValueCache[$oid])) { |
79
|
2 |
|
if (isset($this->lazyValueCache[$oid][$property])) { |
80
|
2 |
|
return $this->lazyValueCache[$oid][$property]; |
81
|
|
|
} |
82
|
|
|
} else { |
83
|
5 |
|
$this->lazyValueCache[$oid] = array(); |
84
|
|
|
} |
85
|
|
|
|
86
|
5 |
|
$this->properties[$property]->setAccessible(true); |
87
|
|
|
|
88
|
5 |
|
return $this->lazyValueCache[$oid][$property] = $this->properties[$property]->getValue($user); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Gets the name of a specific property by its metadata constant. |
94
|
|
|
* |
95
|
|
|
* @param int $property |
96
|
|
|
* @param bool $strict |
97
|
|
|
* |
98
|
|
|
* @return null|string |
99
|
|
|
*/ |
100
|
9 |
|
public function getPropertyName($property = self::LOGIN_PROPERTY, $strict = false) |
101
|
|
|
{ |
102
|
9 |
|
if ($this->checkProperty($property, $strict)) { |
103
|
9 |
|
if (isset($this->lazyPropertyNameCache[$property])) { |
104
|
6 |
|
return $this->lazyPropertyNameCache[$property]; |
105
|
|
|
} |
106
|
|
|
|
107
|
9 |
|
return $this->lazyPropertyNameCache[$property] = $this->properties[$property]->getName(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Modifies a property and clears the cache. |
113
|
|
|
* |
114
|
|
|
* @param object $user |
115
|
|
|
* @param mixed $newValue |
116
|
|
|
* @param int $property |
117
|
|
|
*/ |
118
|
6 |
|
public function modifyProperty($user, $newValue, $property = self::LOGIN_PROPERTY) |
119
|
|
|
{ |
120
|
6 |
|
$this->checkProperty($property, true); |
121
|
|
|
|
122
|
5 |
|
$propertyObject = $this->properties[$property]; |
123
|
5 |
|
$propertyObject->setAccessible(true); |
124
|
5 |
|
$propertyObject->setValue($user, $newValue); |
125
|
|
|
|
126
|
5 |
|
unset($this->lazyValueCache[spl_object_hash($user)]); |
127
|
5 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Validates a property. |
131
|
|
|
* |
132
|
|
|
* @param int $property |
133
|
|
|
* @param bool $strict |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
10 |
|
private function checkProperty($property = self::LOGIN_PROPERTY, $strict = false) |
138
|
|
|
{ |
139
|
10 |
|
if (!isset($this->properties[$property])) { |
140
|
1 |
|
if ($strict) { |
141
|
1 |
|
throw new \LogicException(sprintf( |
142
|
1 |
|
'Cannot get property "%s"!', |
143
|
|
|
$property |
144
|
1 |
|
)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
9 |
|
return true; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|