1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace As3\Modlr\Models\Embeds; |
4
|
|
|
|
5
|
|
|
use As3\Modlr\Models\Attributes; |
6
|
|
|
use As3\Modlr\Models\Embed; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Represents the has-one embeds of a Model. |
10
|
|
|
* |
11
|
|
|
* @author Jacob Bare <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class HasOne extends Attributes |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Constructor. |
17
|
|
|
* |
18
|
|
|
* @param Embed[] $original Any original properties to apply. |
19
|
|
|
*/ |
20
|
|
|
public function __construct(array $original = []) |
21
|
|
|
{ |
22
|
|
View Code Duplication |
foreach ($original as $key => $embed) { |
|
|
|
|
23
|
|
|
if (!$embed instanceof Embed) { |
24
|
|
|
continue; |
25
|
|
|
} |
26
|
|
|
$this->current[$key] = clone $embed; |
27
|
|
|
} |
28
|
|
|
parent::__construct($original); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function areDirty() |
35
|
|
|
{ |
36
|
|
|
if (!empty($this->remove)) { |
37
|
|
|
return true; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
foreach ($this->getCurrentAll() as $current) { |
41
|
|
|
if (true === $current->isDirty()) { |
42
|
|
|
return true; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
foreach ($this->getOriginalAll() as $original) { |
46
|
|
|
if (true === $original->isDirty()) { |
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function calculateChangeSet() |
57
|
|
|
{ |
58
|
|
|
$set = []; |
59
|
|
View Code Duplication |
foreach ($this->original as $key => $current) { |
|
|
|
|
60
|
|
|
if (false === $current->isDirty()) { |
61
|
|
|
continue; |
62
|
|
|
} |
63
|
|
|
$original = isset($this->original[$key]) ? $this->original[$key] : null; |
64
|
|
|
$set[$key]['old'] = $original; |
65
|
|
|
$set[$key]['new'] = $current; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
foreach ($this->current as $key => $current) { |
|
|
|
|
69
|
|
|
if (false === $current->isDirty()) { |
70
|
|
|
continue; |
71
|
|
|
} |
72
|
|
|
$original = isset($this->original[$key]) ? $this->original[$key] : null; |
73
|
|
|
$set[$key]['old'] = $original; |
74
|
|
|
$set[$key]['new'] = $current; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
foreach ($this->remove as $key) { |
78
|
|
|
$set[$key]['old'] = $this->original[$key]; |
79
|
|
|
$set[$key]['new'] = null; |
80
|
|
|
} |
81
|
|
|
ksort($set); |
82
|
|
|
return $set; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function rollback() |
89
|
|
|
{ |
90
|
|
|
$this->current = []; |
91
|
|
View Code Duplication |
foreach ($this->original as $key => $embed) { |
|
|
|
|
92
|
|
|
if (!$embed instanceof Embed) { |
93
|
|
|
continue; |
94
|
|
|
} |
95
|
|
|
$this->current[$key] = clone $embed; |
96
|
|
|
} |
97
|
|
|
$this->remove = []; |
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.