Properties::getCurrentAll()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace As3\Modlr\Models;
4
5
use As3\Modlr\Store\Store;
6
use As3\Modlr\Metadata\EntityMetadata;
7
8
/**
9
 * Represents the properties of a Model.
10
 *
11
 * @author Jacob Bare <[email protected]>
12
 */
13
abstract class Properties
14
{
15
    /**
16
     * The original property values.
17
     *
18
     * @var array
19
     */
20
    protected $original = [];
21
22
    /**
23
     * The current/modified property values.
24
     *
25
     * @var array
26
     */
27
    protected $current = [];
28
29
    /**
30
     * Any properties that have been flagged for removal.
31
     *
32
     * @var array
33
     */
34
    protected $remove = [];
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param   array   $original   Any original properties to apply.
40
     */
41
    public function __construct(array $original = [])
42
    {
43
        $this->original = $original;
44
    }
45
46
    /**
47
     * Gets the current value of an property.
48
     *
49
     * @param   string  $key    The property key.
50
     * @return  mixed
51
     */
52
    public function get($key)
53
    {
54
        if ($this->willRemove($key)) {
55
            return null;
56
        }
57
        if (true === $this->willChange($key)) {
58
            return $this->getCurrent($key);
59
        }
60
        return $this->getOriginal($key);
61
    }
62
63
    /**
64
     * Sets a new value to an property.
65
     *
66
     * @param   string  $key    The property key.
67
     * @param   mixed   $value  The value to set.
68
     * @return  mixed
69
     */
70
    public function set($key, $value)
71
    {
72
        if (null === $value) {
73
            return $this->remove($key);
74
        }
75
        $this->clearRemoval($key);
76
77
        $original = $this->getOriginal($key);
78
79
        if ($value instanceof \stdClass && $original instanceof \stdClass) {
80
            if ($value == $original) {
81
                $this->clearChange($key);
82
            } else {
83
                $this->current[$key] = $value;
84
            }
85
            return $this;
86
        }
87
88
        if ($value === $original) {
89
            $this->clearChange($key);
90
        } else {
91
            if (($value instanceof \DateTime && $original instanceof \DateTime) && ($value->getTimestamp() === $original->getTimestamp())) {
92
                $this->clearChange($key);
93
            } else {
94
                $this->current[$key] = $value;
95
            }
96
        }
97
        return $this;
98
    }
99
100
    /**
101
     * Sets a new value to an property.
102
     *
103
     * @param   string  $key    The property key.
104
     * @return  self
105
     */
106
    public function remove($key)
107
    {
108
        if (false === $this->willRemove($key)) {
109
            $this->clearChange($key);
110
            if (true === $this->hasOriginal($key)) {
111
                $this->remove[] = $key;
112
            }
113
        }
114
        return $this;
115
    }
116
117
    /**
118
     * Rolls back the properties to their original state.
119
     *
120
     * @return  self
121
     */
122
    public function rollback()
123
    {
124
        $this->current = [];
125
        $this->remove = [];
126
        return $this;
127
    }
128
129
    /**
130
     * Replaces the current properties with new ones.
131
     * Will revert/rollback any current changes.
132
     *
133
     * @param   array   $original
134
     * @return  self
135
     */
136
    public function replace(array $original)
137
    {
138
        $this->original = $original;
139
        $this->rollback();
140
        return $this;
141
    }
142
143
144
    /**
145
     * Deteremines if the properties have different values from their original state.
146
     *
147
     * @return  bool
148
     */
149
    public function areDirty()
150
    {
151
        return !empty($this->current) || !empty($this->remove);
152
    }
153
154
    /**
155
     * Calculates any property changes.
156
     *
157
     * @return  array
158
     */
159
    public function calculateChangeSet()
160
    {
161
        $set = [];
162 View Code Duplication
        foreach ($this->current as $key => $current) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
163
            $original = isset($this->original[$key]) ? $this->original[$key] : null;
164
            $set[$key]['old'] = $original;
165
            $set[$key]['new'] = $current;
166
        }
167 View Code Duplication
        foreach ($this->remove as $key) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
168
            $set[$key]['old'] = $this->original[$key];
169
            $set[$key]['new'] = null;
170
        }
171
        ksort($set);
172
        return $set;
173
    }
174
175
    /**
176
     * Clears an property from the removal queue.
177
     *
178
     * @param   string  $key    The field key.
179
     * @return  self
180
     */
181
    protected function clearRemoval($key)
182
    {
183
        if (false === $this->willRemove($key)) {
184
            return $this;
185
        }
186
        $key = array_search($key, $this->remove);
187
        unset($this->remove[$key]);
188
        $this->remove = array_values($this->remove);
189
        return $this;
190
    }
191
192
    /**
193
     * Clears an property as having been changed.
194
     *
195
     * @param   string  $key    The field key.
196
     * @return  self
197
     */
198
    protected function clearChange($key)
199
    {
200
        if (true === $this->willChange($key)) {
201
            unset($this->current[$key]);
202
        }
203
        return $this;
204
    }
205
206
    /**
207
     * Determines if an property is in the removal queue.
208
     *
209
     * @param   string  $key    The field key.
210
     * @return  bool
211
     */
212
    protected function willRemove($key)
213
    {
214
        return in_array($key, $this->remove);
215
    }
216
217
    /**
218
     * Determines if an property has a new value.
219
     *
220
     * @param   string  $key    The field key.
221
     * @return  bool
222
     */
223
    protected function willChange($key)
224
    {
225
        return null !== $this->getCurrent($key);
226
    }
227
228
    /**
229
     * Determines if an property has an original value.
230
     *
231
     * @param   string  $key    The field key.
232
     * @return  bool
233
     */
234
    protected function hasOriginal($key)
235
    {
236
        return null !== $this->getOriginal($key);
237
    }
238
239
    /**
240
     * Gets the property's original value.
241
     *
242
     * @param   string  $key    The field key.
243
     * @return  mixed
244
     */
245
    protected function getOriginal($key)
246
    {
247
        if (isset($this->original[$key])) {
248
            return $this->original[$key];
249
        }
250
        return null;
251
    }
252
253
    /**
254
     * Gets all original properties.
255
     *
256
     * @return  array
257
     */
258
    protected function getOriginalAll()
259
    {
260
        return $this->original;
261
    }
262
263
    /**
264
     * Gets all current properties.
265
     *
266
     * @return  array
267
     */
268
    protected function getCurrentAll()
269
    {
270
        return $this->current;
271
    }
272
273
    /**
274
     * Gets the property's current value.
275
     *
276
     * @param   string  $key    The field key.
277
     * @return  mixed
278
     */
279
    protected function getCurrent($key)
280
    {
281
        if (isset($this->current[$key])) {
282
            return $this->current[$key];
283
        }
284
        return null;
285
    }
286
}
287