|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.1 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
declare(strict_types = 1); |
|
13
|
|
|
|
|
14
|
|
|
namespace WebHemi\Data\Storage\User; |
|
15
|
|
|
|
|
16
|
|
|
use WebHemi\DateTime; |
|
17
|
|
|
use WebHemi\Data\Entity\DataEntityInterface; |
|
18
|
|
|
use WebHemi\Data\Entity\User\UserEntity; |
|
19
|
|
|
use WebHemi\Data\Storage\AbstractDataStorage; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class UserStorage. |
|
23
|
|
|
*/ |
|
24
|
|
|
class UserStorage extends AbstractDataStorage |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
protected $dataGroup = 'webhemi_user'; |
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
protected $idKey = 'id_user'; |
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
private $userName = 'username'; |
|
32
|
|
|
/** @var string */ |
|
33
|
|
|
private $email = 'email'; |
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
private $password = 'password'; |
|
36
|
|
|
/** @var string */ |
|
37
|
|
|
private $hash = 'hash'; |
|
38
|
|
|
/** @var string */ |
|
39
|
|
|
private $isActive = 'is_active'; |
|
40
|
|
|
/** @var string */ |
|
41
|
|
|
private $isEnabled = 'is_enabled'; |
|
42
|
|
|
/** @var string */ |
|
43
|
|
|
private $dateCreated = 'date_created'; |
|
44
|
|
|
/** @var string */ |
|
45
|
|
|
private $dateModified = 'date_modified'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Populates an entity with storage data. |
|
49
|
|
|
* |
|
50
|
|
|
* @param DataEntityInterface $entity |
|
51
|
4 |
|
* @param array $data |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
4 |
|
protected function populateEntity(DataEntityInterface&$entity, array $data) : void |
|
55
|
4 |
|
{ |
|
56
|
4 |
|
/* @var UserEntity $entity */ |
|
57
|
4 |
|
$entity->setUserId((int) $data[$this->idKey]) |
|
58
|
4 |
|
->setUserName($data[$this->userName]) |
|
59
|
4 |
|
->setEmail($data[$this->email]) |
|
60
|
4 |
|
->setPassword($data[$this->password]) |
|
61
|
4 |
|
->setHash($data[$this->hash]) |
|
62
|
4 |
|
->setActive((bool) $data[$this->isActive]) |
|
63
|
4 |
|
->setEnabled((bool) $data[$this->isEnabled]) |
|
64
|
|
|
->setDateCreated(new DateTime($data[$this->dateCreated] ?? 'now')) |
|
65
|
|
|
->setDateModified(new DateTime($data[$this->dateModified] ?? 'now')); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get data from an entity. |
|
70
|
|
|
* |
|
71
|
3 |
|
* @param DataEntityInterface $entity |
|
72
|
|
|
* @return array |
|
73
|
|
|
*/ |
|
74
|
3 |
|
protected function getEntityData(DataEntityInterface $entity) : array |
|
75
|
3 |
|
{ |
|
76
|
|
|
/** @var UserEntity $entity */ |
|
77
|
|
|
$dateCreated = $entity->getDateCreated(); |
|
78
|
3 |
|
$dateModified = $entity->getDateModified(); |
|
79
|
3 |
|
|
|
80
|
3 |
|
return [ |
|
81
|
3 |
|
$this->idKey => $entity->getKeyData(), |
|
82
|
3 |
|
$this->userName => $entity->getUserName(), |
|
83
|
3 |
|
$this->email => $entity->getEmail(), |
|
84
|
3 |
|
$this->password => $entity->getPassword(), |
|
85
|
3 |
|
$this->hash => $entity->getHash(), |
|
86
|
3 |
|
$this->isActive => (int) $entity->getActive(), |
|
87
|
|
|
$this->isEnabled => (int) $entity->getEnabled(), |
|
88
|
|
|
$this->dateCreated => $dateCreated instanceof DateTime ? $dateCreated->format('Y-m-d H:i:s') : null, |
|
89
|
|
|
$this->dateModified => $dateModified instanceof DateTime ? $dateModified->format('Y-m-d H:i:s') : null |
|
90
|
|
|
]; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Returns a User entity identified by (unique) ID. |
|
95
|
|
|
* |
|
96
|
|
|
* @param int $identifier |
|
97
|
1 |
|
* @return null|UserEntity |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function getUserById(int $identifier) : ? UserEntity |
|
100
|
1 |
|
{ |
|
101
|
|
|
/** @var null|UserEntity $entity */ |
|
102
|
1 |
|
$entity = $this->getDataEntity([$this->idKey => $identifier]); |
|
103
|
|
|
|
|
104
|
1 |
|
return $entity; |
|
105
|
1 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
1 |
|
* Returns a User entity by user name. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $name |
|
111
|
|
|
* @return null|UserEntity |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getUserByUserName(string $name) : ? UserEntity |
|
114
|
|
|
{ |
|
115
|
|
|
/** @var null|UserEntity $entity */ |
|
116
|
|
|
$entity = $this->getDataEntity([$this->userName => $name]); |
|
117
|
|
|
|
|
118
|
1 |
|
return $entity; |
|
119
|
|
|
} |
|
120
|
1 |
|
|
|
121
|
1 |
|
/** |
|
122
|
|
|
* Returns a User entity by email. |
|
123
|
1 |
|
* |
|
124
|
|
|
* @param string $email |
|
125
|
1 |
|
* @return null|UserEntity |
|
126
|
1 |
|
*/ |
|
127
|
|
|
public function getUserByEmail($email) : ? UserEntity |
|
128
|
|
|
{ |
|
129
|
1 |
|
/** @var null|UserEntity $entity */ |
|
130
|
|
|
$entity = $this->getDataEntity([$this->email => $email]); |
|
131
|
|
|
|
|
132
|
|
|
return $entity; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Return a User entity by credentials. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $username |
|
139
|
1 |
|
* @param string $password |
|
140
|
|
|
* @return null|UserEntity |
|
141
|
1 |
|
*/ |
|
142
|
1 |
|
public function getUserByCredentials(string $username, string $password) : ? UserEntity |
|
143
|
|
|
{ |
|
144
|
1 |
|
/** @var null|UserEntity $entity */ |
|
145
|
|
|
$entity = $this->getDataEntity([$this->userName => $username, $this->password => $password]); |
|
146
|
1 |
|
|
|
147
|
1 |
|
return $entity; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|