Skip to content

Commit 1eb6e41

Browse files
author
Jacob Bare
authored
Merge pull request #100 from zarathustra323/hotfix-0.3.15
Add embed hashing and proper collection dirty check
2 parents 776a98d + 7ad0047 commit 1eb6e41

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-5
lines changed

src/Models/AbstractModel.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,14 @@ public function apply(array $properties)
119119
continue;
120120
}
121121

122-
// @todo This will always mark the model as dirty, even if the applied embed values are the same as the original.
123-
$this->clear($key);
124122
$collection = $this->getStore()->createEmbedCollection($embeddedPropMeta, $properties[$key]);
123+
if ($collection->getHash() === $this->get($key)->getHash()) {
124+
// The current collection is the same as the incoming collection.
125+
continue;
126+
}
127+
128+
// The incoming collection is different. Clear the current collection and push the new values.
129+
$this->clear($key);
125130
foreach ($collection as $value) {
126131
$this->pushEmbed($key, $value);
127132
}

src/Models/Collections/EmbedCollection.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ public function createNewEmbed()
4444
return $embed;
4545
}
4646

47+
/**
48+
* Gets the unique hash for this collection.
49+
*
50+
* @return string
51+
*/
52+
public function getHash()
53+
{
54+
$hash = [];
55+
foreach ($this as $embed) {
56+
$hash[] = $embed->getHash();
57+
}
58+
sort($hash);
59+
return md5(serialize($hash));
60+
}
61+
4762
/**
4863
* Gets the metadata for the model collection.
4964
*

src/Models/Embed.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,50 @@ public function getCompositeKey()
4040
return spl_object_hash($this);
4141
}
4242

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+
4387
/**
4488
* Gets the metadata for this model.
4589
*

src/Version.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
*/
1010
class Version
1111
{
12-
const VERSION = '0.3.14';
13-
const ID = 3014;
12+
const VERSION = '0.3.15';
13+
const ID = 3015;
1414
const MAJOR = 0;
1515
const MINOR = 3;
16-
const PATCH = 14;
16+
const PATCH = 15;
1717
const EXTRA = '';
1818
}

0 commit comments

Comments
 (0)