|
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\EntityInterface; |
|
18
|
|
|
use WebHemi\Data\Entity\User\UserMetaEntity; |
|
19
|
|
|
use WebHemi\Data\Storage\AbstractStorage; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class UserMetaStorage. |
|
23
|
|
|
*/ |
|
24
|
|
|
class UserMetaStorage extends AbstractStorage |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
protected $dataGroup = 'webhemi_user_meta'; |
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
protected $idKey = 'id_user_meta'; |
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
private $userId = 'fk_user'; |
|
32
|
|
|
/** @var string */ |
|
33
|
|
|
private $metaKey = 'meta_key'; |
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
private $metaData = 'meta_data'; |
|
36
|
|
|
/** @var string */ |
|
37
|
|
|
private $dateCreated = 'date_created'; |
|
38
|
|
|
/** @var string */ |
|
39
|
|
|
private $dateModified = 'date_modified'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Populates an entity with storage data. |
|
43
|
|
|
* |
|
44
|
|
|
* @param EntityInterface $dataEntity |
|
45
|
|
|
* @param array $data |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
2 |
|
protected function populateEntity(EntityInterface&$dataEntity, array $data) : void |
|
49
|
|
|
{ |
|
50
|
|
|
/* @var UserMetaEntity $dataEntity */ |
|
51
|
2 |
|
$dataEntity->setUserMetaId((int) $data[$this->idKey]) |
|
52
|
2 |
|
->setUserId((int) $data[$this->userId]) |
|
53
|
2 |
|
->setMetaKey($data[$this->metaKey]) |
|
54
|
2 |
|
->setMetaData($data[$this->metaData]) |
|
55
|
2 |
|
->setDateCreated(new DateTime($data[$this->dateCreated] ?? 'now')) |
|
56
|
2 |
|
->setDateModified(new DateTime($data[$this->dateModified] ?? 'now')); |
|
57
|
2 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get data from an entity. |
|
61
|
|
|
* |
|
62
|
|
|
* @param EntityInterface $dataEntity |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function getEntityData(EntityInterface $dataEntity) : array |
|
66
|
|
|
{ |
|
67
|
|
|
/** @var UserMetaEntity $dataEntity */ |
|
68
|
|
|
$dateCreated = $dataEntity->getDateCreated(); |
|
69
|
|
|
$dateModified = $dataEntity->getDateModified(); |
|
70
|
|
|
|
|
71
|
|
|
return [ |
|
72
|
|
|
$this->idKey => $dataEntity->getKeyData(), |
|
73
|
|
|
$this->userId => $dataEntity->getUserId(), |
|
74
|
|
|
$this->metaKey => $dataEntity->getMetaKey(), |
|
75
|
|
|
$this->metaData => $dataEntity->getMetaData(), |
|
76
|
|
|
$this->dateCreated => $dateCreated instanceof DateTime ? $dateCreated->format('Y-m-d H:i:s') : null, |
|
77
|
|
|
$this->dateModified => $dateModified instanceof DateTime ? $dateModified->format('Y-m-d H:i:s') : null |
|
78
|
|
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Returns a User Meta entity identified by (unique) ID. |
|
83
|
|
|
* |
|
84
|
|
|
* @param int $identifier |
|
85
|
|
|
* @return null|UserMetaEntity |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function getUserMetaById($identifier) : ? UserMetaEntity |
|
88
|
|
|
{ |
|
89
|
|
|
/** @var null|UserMetaEntity $dataEntity */ |
|
90
|
1 |
|
$dataEntity = $this->getDataEntity([$this->idKey => $identifier]); |
|
91
|
|
|
|
|
92
|
1 |
|
return $dataEntity; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Returns a User Meta data list identified by user ID. |
|
97
|
|
|
* |
|
98
|
|
|
* @param mixed $userId |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function getUserMetaSetForUserId($userId) : array |
|
102
|
|
|
{ |
|
103
|
1 |
|
$userMetaEntitySet = $this->getDataEntitySet([$this->userId => $userId]); |
|
104
|
1 |
|
$userMetaSet = []; |
|
105
|
|
|
|
|
106
|
|
|
/** @var UserMetaEntity $metaEntity */ |
|
107
|
1 |
|
foreach ($userMetaEntitySet as $metaEntity) { |
|
108
|
1 |
|
$userMetaSet[$metaEntity->getMetaKey()] = $metaEntity->getMetaData(); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
return $userMetaSet; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Returns a User Meta entity list identified by user ID. |
|
116
|
|
|
* |
|
117
|
|
|
* @param mixed $userId |
|
118
|
|
|
* @return UserMetaEntity[] |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getUserMetaForUserId($userId) : array |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->getDataEntitySet([$this->userId => $userId]); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: