1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace As3\Modlr\Models; |
4
|
|
|
|
5
|
|
|
use As3\Modlr\Metadata\EmbedMetadata; |
6
|
|
|
use As3\Modlr\Store\Store; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Represents an embedded record fragment of a root level model. |
10
|
|
|
* |
11
|
|
|
* @author Jacob Bare <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Embed extends AbstractModel |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The metadata that defines this Model. |
17
|
|
|
* |
18
|
|
|
* @var EmbedMetadata |
19
|
|
|
*/ |
20
|
|
|
protected $metadata; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructor. |
24
|
|
|
* |
25
|
|
|
* @param EmbedMetadata $metadata The internal embed metadata that supports this Embed. |
26
|
|
|
* @param Store $store The model store service for handling persistence operations. |
27
|
|
|
* @param array|null $properties The embed's properties from the db layer to init the embed with. New embeds will constructed with a null record. |
28
|
|
|
*/ |
29
|
|
|
public function __construct(EmbedMetadata $metadata, Store $store, array $properties = null) |
30
|
|
|
{ |
31
|
|
|
parent::__construct($metadata, $store, $properties); |
32
|
|
|
$this->getState()->setLoaded(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function getCompositeKey() |
39
|
|
|
{ |
40
|
|
|
return spl_object_hash($this); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Gets the unique hash for this embed. |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function getHash() |
49
|
|
|
{ |
50
|
|
|
$hash = []; |
51
|
|
|
foreach ($this->metadata->getAttributes() as $key => $attrMeta) { |
52
|
|
|
$value = $this->get($key); |
53
|
|
|
if (null === $value) { |
54
|
|
|
$hash[$key] = $value; |
55
|
|
|
continue; |
56
|
|
|
} |
57
|
|
|
switch ($attrMeta->dataType) { |
58
|
|
|
case 'date': |
59
|
|
|
$value = $value->getTimestamp(); |
60
|
|
|
break; |
61
|
|
|
case 'object': |
62
|
|
|
$value = (array) $object; |
|
|
|
|
63
|
|
|
ksort($value); |
64
|
|
|
break; |
65
|
|
|
case 'mixed': |
66
|
|
|
$value = serialize($value); |
67
|
|
|
break; |
68
|
|
|
case 'array': |
69
|
|
|
sort($value); |
70
|
|
|
break; |
71
|
|
|
} |
72
|
|
|
$hash[$key] = $value; |
73
|
|
|
} |
74
|
|
|
foreach ($this->metadata->getEmbeds() as $key => $embbedPropMeta) { |
75
|
|
|
if (true === $embbedPropMeta->isOne()) { |
76
|
|
|
$embed = $this->get($key); |
77
|
|
|
$hash[$key] = (null === $embed) ? null : $embed->getHash(); |
78
|
|
|
} else { |
79
|
|
|
$collection = $this->get($key); |
80
|
|
|
$hash[$key] = $collection->getHash(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
ksort($hash); |
84
|
|
|
return md5(serialize($hash)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Gets the metadata for this model. |
89
|
|
|
* |
90
|
|
|
* @api |
91
|
|
|
* @return EmbedMetadata |
92
|
|
|
*/ |
93
|
|
|
public function getMetadata() |
94
|
|
|
{ |
95
|
|
|
return $this->metadata; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Gets the embed name. |
101
|
|
|
* |
102
|
|
|
* @api |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function getName() |
106
|
|
|
{ |
107
|
|
|
return $this->getMetadata()->name; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.