1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org) |
5
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
6
|
|
|
* |
7
|
|
|
* Licensed under The MIT License |
8
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
9
|
|
|
* Redistributions of files must retain the above copyright notice. |
10
|
|
|
* |
11
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
12
|
|
|
* @link https://cakephp.org CakePHP(tm) Project |
13
|
|
|
* @since 1.0.0 |
14
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
declare(strict_types=1); |
18
|
|
|
|
19
|
|
|
namespace Phauthentic\Authentication\Identity; |
20
|
|
|
|
21
|
|
|
use ArrayAccess; |
22
|
|
|
use BadMethodCallException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Identity object |
26
|
|
|
*/ |
27
|
|
|
class Identity implements IdentityInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Default configuration. |
31
|
|
|
* |
32
|
|
|
* - `fieldMap` Mapping of fields |
33
|
|
|
* |
34
|
|
|
* @var array<string, mixed> |
35
|
|
|
*/ |
36
|
|
|
protected array $defaultConfig = [ |
37
|
|
|
'fieldMap' => [ |
38
|
|
|
'id' => 'id' |
39
|
|
|
] |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Config |
44
|
|
|
* |
45
|
|
|
* @var array<string, mixed> |
46
|
|
|
*/ |
47
|
|
|
protected array $config = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Identity data |
51
|
|
|
* |
52
|
|
|
* @var \ArrayAccess<mixed, mixed> |
53
|
|
|
*/ |
54
|
|
|
protected ArrayAccess $data; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Constructor |
58
|
|
|
* |
59
|
|
|
* @param \ArrayAccess $data Identity data |
60
|
52 |
|
* @param array<string, mixed> $config Config options |
61
|
|
|
* @throws \InvalidArgumentException When invalid identity data is passed. |
62
|
52 |
|
*/ |
63
|
52 |
|
public function __construct(ArrayAccess $data, array $config = []) |
64
|
52 |
|
{ |
65
|
|
|
$this->config = array_merge_recursive($this->defaultConfig, $config); |
66
|
|
|
$this->data = $data; |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
4 |
|
*/ |
72
|
|
|
public function getIdentifier() |
73
|
|
|
{ |
74
|
|
|
return $this->get('id'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get data from the identity using object access. |
79
|
|
|
* |
80
|
8 |
|
* @param string $field Field in the user data. |
81
|
|
|
* @return mixed |
82
|
8 |
|
*/ |
83
|
|
|
public function __get($field) |
84
|
|
|
{ |
85
|
|
|
return $this->get($field); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check if the field isset() using object access. |
90
|
|
|
* |
91
|
4 |
|
* @param string $field Field in the user data. |
92
|
|
|
* @return mixed |
93
|
4 |
|
*/ |
94
|
|
|
public function __isset($field) |
95
|
|
|
{ |
96
|
|
|
return $this->get($field) !== null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get data from the identity |
101
|
|
|
* |
102
|
20 |
|
* @param string $field Field in the user data. |
103
|
|
|
* @return mixed |
104
|
20 |
|
*/ |
105
|
20 |
|
protected function get($field) |
106
|
8 |
|
{ |
107
|
|
|
$map = $this->config['fieldMap']; |
108
|
|
|
if (isset($map[$field])) { |
109
|
20 |
|
$field = $map[$field]; |
110
|
20 |
|
} |
111
|
|
|
|
112
|
|
|
if (isset($this->data[$field])) { |
113
|
4 |
|
return $this->data[$field]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Whether a offset exists |
121
|
|
|
* |
122
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
123
|
4 |
|
* @param mixed $offset Offset |
124
|
|
|
* @return bool |
125
|
4 |
|
*/ |
126
|
|
|
public function offsetExists($offset): bool |
127
|
|
|
{ |
128
|
|
|
return $this->get($offset) !== null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Offset to retrieve |
133
|
|
|
* |
134
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
135
|
16 |
|
* @param mixed $offset Offset |
136
|
|
|
* @return mixed |
137
|
16 |
|
*/ |
138
|
|
|
public function offsetGet($offset): mixed |
139
|
|
|
{ |
140
|
|
|
return $this->get($offset); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Offset to set |
145
|
|
|
* |
146
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
147
|
|
|
* @param mixed $offset The offset to assign the value to. |
148
|
|
|
* @param mixed $value Value |
149
|
4 |
|
* @throws \BadMethodCallException |
150
|
|
|
* @return void |
151
|
4 |
|
*/ |
152
|
|
|
public function offsetSet($offset, $value): void |
153
|
|
|
{ |
154
|
|
|
throw new BadMethodCallException('Identity does not allow wrapped data to be mutated.'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Offset to unset |
159
|
|
|
* |
160
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
161
|
|
|
* @param mixed $offset Offset |
162
|
4 |
|
* @throws \BadMethodCallException |
163
|
|
|
* @return void |
164
|
4 |
|
*/ |
165
|
|
|
public function offsetUnset($offset): void |
166
|
|
|
{ |
167
|
|
|
throw new BadMethodCallException('Identity does not allow wrapped data to be mutated.'); |
168
|
|
|
} |
169
|
|
|
|
170
|
12 |
|
/** |
171
|
|
|
* {@inheritDoc} |
172
|
12 |
|
*/ |
173
|
|
|
public function getOriginalData(): ArrayAccess |
174
|
|
|
{ |
175
|
|
|
return $this->data; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|