1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SixtyEightPublishers\User\Common; |
6
|
|
|
|
7
|
|
|
use ArrayAccess; |
8
|
|
|
use SixtyEightPublishers\User\Common\Exception\RuntimeException; |
9
|
|
|
use SixtyEightPublishers\User\Common\Exception\InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
final class UserMapping implements ArrayAccess |
12
|
|
|
{ |
13
|
|
|
public const FIELD_ID = 'id'; |
14
|
|
|
public const FILED_EMAIL = 'email'; |
15
|
|
|
public const FIELD_PASSWORD = 'password'; |
16
|
|
|
public const FIELD_USERNAME = 'username'; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
private $className; |
20
|
|
|
|
21
|
|
|
/** @var array */ |
22
|
|
|
private $fields = [ |
23
|
|
|
self::FIELD_ID => 'id', |
24
|
|
|
self::FILED_EMAIL => 'email', |
25
|
|
|
self::FIELD_PASSWORD => 'password', |
26
|
|
|
self::FIELD_USERNAME => 'username', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $className |
31
|
|
|
* @param array $fields |
32
|
|
|
*/ |
33
|
|
|
public function __construct(string $className, array $fields) |
34
|
|
|
{ |
35
|
|
|
$this->className = $className; |
36
|
|
|
$this->fields = array_merge($this->fields, $fields); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function getClassName(): string |
43
|
|
|
{ |
44
|
|
|
return $this->className; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param mixed $name |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function __isset($name): bool |
53
|
|
|
{ |
54
|
|
|
return isset($this->fields[$name]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param mixed $name |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function __get($name): string |
63
|
|
|
{ |
64
|
|
|
if (!$this->__isset($name)) { |
65
|
|
|
throw new InvalidArgumentException(sprintf( |
66
|
|
|
'Missing field with name "%s"', |
67
|
|
|
$name |
68
|
|
|
)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->fields[$name]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param mixed $name |
76
|
|
|
* @param mixed $value |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
* @throws \SixtyEightPublishers\User\Common\Exception\RuntimeException |
80
|
|
|
*/ |
81
|
|
|
public function __set($name, $value): void |
82
|
|
|
{ |
83
|
|
|
throw new RuntimeException(sprintf( |
84
|
|
|
'Calling of method %s is not supported.', |
85
|
|
|
__METHOD__ |
86
|
|
|
)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param mixed $name |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
* @throws \SixtyEightPublishers\User\Common\Exception\RuntimeException |
94
|
|
|
*/ |
95
|
|
|
public function __unset($name): void |
96
|
|
|
{ |
97
|
|
|
throw new RuntimeException(sprintf( |
98
|
|
|
'Calling of method %s is not supported.', |
99
|
|
|
__METHOD__ |
100
|
|
|
)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function offsetExists($offset): bool |
107
|
|
|
{ |
108
|
|
|
return $this->__isset($offset); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public function offsetGet($offset): string |
115
|
|
|
{ |
116
|
|
|
return $this->__get($offset); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
public function offsetSet($offset, $value): void |
123
|
|
|
{ |
124
|
|
|
$this->__set($offset, $value); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
|
public function offsetUnset($offset): void |
131
|
|
|
{ |
132
|
|
|
$this->__unset($offset); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|