|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.2 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2019 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\Entity; |
|
15
|
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class AbstractEntity |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class AbstractEntity implements EntityInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $container = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $referenceContainer = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Set reference entity. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $referenceName |
|
37
|
|
|
* @param EntityInterface $referenceEntity |
|
38
|
|
|
* @return bool |
|
39
|
|
|
*/ |
|
40
|
|
|
public function setReference(string $referenceName, EntityInterface $referenceEntity): bool |
|
41
|
|
|
{ |
|
42
|
|
|
if (!\array_key_exists($referenceName, $this->referenceContainer)) { |
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->referenceContainer[$referenceName] = $referenceEntity; |
|
47
|
|
|
|
|
48
|
|
|
return true; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get reference entity by key |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $referenceName |
|
55
|
|
|
* @return EntityInterface|null |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getReference(string $referenceName): ? EntityInterface |
|
58
|
|
|
{ |
|
59
|
|
|
if (isset($this->referenceContainer[$referenceName]) |
|
60
|
|
|
&& $this->referenceContainer[$referenceName] instanceof EntityInterface |
|
61
|
|
|
) { |
|
62
|
|
|
return $this->referenceContainer[$referenceName]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Returns entity data as an array. |
|
70
|
|
|
* |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
12 |
|
public function toArray(): array |
|
74
|
|
|
{ |
|
75
|
12 |
|
return $this->container; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Fills entity from an arrray. |
|
80
|
|
|
* |
|
81
|
|
|
* @param array $arrayData |
|
82
|
|
|
*/ |
|
83
|
46 |
|
public function fromArray(array $arrayData): void |
|
84
|
|
|
{ |
|
85
|
46 |
|
foreach ($arrayData as $key => $value) { |
|
86
|
46 |
|
if (!\array_key_exists($key, $this->container)) { |
|
87
|
2 |
|
throw new InvalidArgumentException( |
|
88
|
2 |
|
sprintf('"%s" is not defined in '.static::class, $key), |
|
89
|
2 |
|
1001 |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
44 |
|
$this->container[$key] = $value; |
|
94
|
|
|
} |
|
95
|
44 |
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|