1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Minotaur |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
8
|
|
|
* the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations under |
16
|
|
|
* the License. |
17
|
|
|
* |
18
|
|
|
* @copyright 2015-2017 Appertly |
19
|
|
|
* @license Apache-2.0 |
20
|
|
|
*/ |
21
|
|
|
namespace Minotaur\Db\Entity; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A trait for entities which can track changes. |
25
|
|
|
*/ |
26
|
|
|
trait Tracking |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var array<string,array<string,mixed>> Changes to persist. |
30
|
|
|
*/ |
31
|
|
|
protected $changes = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Gets the pending changes. |
35
|
|
|
* |
36
|
|
|
* @return array<string,array<string,mixed>> The pending changes |
37
|
|
|
*/ |
38
|
|
|
public function getChanges(): array |
39
|
|
|
{ |
40
|
|
|
return $this->changes; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Whether the object has any changes. |
45
|
|
|
* |
46
|
|
|
* @return - Whether the object has any changes |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
public function isDirty(): bool |
49
|
|
|
{ |
50
|
|
|
return !empty($this->changes); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Sets a field for update. |
55
|
|
|
* |
56
|
|
|
* @param $field - The field name |
57
|
|
|
* @param $value - The field value |
58
|
|
|
* @return - provides a fluent interface |
|
|
|
|
59
|
|
|
*/ |
60
|
|
|
protected function fieldSet(string $field, $value): self |
61
|
|
|
{ |
62
|
|
|
if (!array_key_exists('$set', $this->changes)) { |
63
|
|
|
$this->changes['$set'] = []; |
64
|
|
|
} |
65
|
|
|
$this->changes['$set'][$field] = $value; |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Sets a field for removal. |
71
|
|
|
* |
72
|
|
|
* @param $field - The field name |
73
|
|
|
* @return - provides a fluent interface |
|
|
|
|
74
|
|
|
*/ |
75
|
|
|
protected function fieldUnset(string $field): self |
76
|
|
|
{ |
77
|
|
|
if (!array_key_exists('$unset', $this->changes)) { |
78
|
|
|
$this->changes['$unset'] = []; |
79
|
|
|
} |
80
|
|
|
$this->changes['$unset'][$field] = ''; |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sets a field for increment. |
86
|
|
|
* |
87
|
|
|
* @param $field - The field name |
88
|
|
|
* @param $value - Optional. The increment value. Default is `1`. |
89
|
|
|
* @return - provides a fluent interface |
|
|
|
|
90
|
|
|
*/ |
91
|
|
|
protected function fieldIncrement(string $field, int $value = 1): self |
92
|
|
|
{ |
93
|
|
|
if (!array_key_exists('$inc', $this->changes)) { |
94
|
|
|
$this->changes['$inc'] = []; |
95
|
|
|
} |
96
|
|
|
$this->changes['$inc'][$field] = $value; |
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Sets a field to the current date. |
102
|
|
|
* |
103
|
|
|
* @param $field - The field name |
104
|
|
|
* @return - provides a fluent interface |
|
|
|
|
105
|
|
|
*/ |
106
|
|
|
protected function fieldNow(string $field): self |
107
|
|
|
{ |
108
|
|
|
if (!array_key_exists('$currentDate', $this->changes)) { |
109
|
|
|
$this->changes['$currentDate'] = []; |
110
|
|
|
} |
111
|
|
|
$this->changes['$currentDate'][$field] = true; |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Pushes a value onto a field. |
117
|
|
|
* |
118
|
|
|
* @param $field - The field name |
119
|
|
|
* @param $value - The value to push |
120
|
|
|
* @return - provides a fluent interface |
|
|
|
|
121
|
|
|
*/ |
122
|
|
|
protected function fieldPush(string $field, $value): self |
123
|
|
|
{ |
124
|
|
|
if (!array_key_exists('$push', $this->changes)) { |
125
|
|
|
$this->changes['$push'] = []; |
126
|
|
|
} |
127
|
|
|
$this->changes['$push'][$field] = $value; |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Pushes a value onto an array field. |
133
|
|
|
* |
134
|
|
|
* @param $field - The field name |
135
|
|
|
* @param $value - The values to push |
136
|
|
|
* @return - provides a fluent interface |
|
|
|
|
137
|
|
|
*/ |
138
|
|
|
protected function fieldPushAll(string $field, iterable $value): self |
139
|
|
|
{ |
140
|
|
|
if (!array_key_exists('$push', $this->changes)) { |
141
|
|
|
$this->changes['$push'] = []; |
142
|
|
|
} |
143
|
|
|
$this->changes['$push'][$field] = ['$each' => is_array($value) ? $value : iterator_to_array($value, false)]; |
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Pulls a value from a array field. |
149
|
|
|
* |
150
|
|
|
* In addition to a single value, you can also specify a query document. |
151
|
|
|
* ``` |
152
|
|
|
* $this->fieldPull('vegetables', 'carrot'); |
153
|
|
|
* $this->fieldPull('listOfDocs', ['foo' => 'bar']); |
154
|
|
|
* ``` |
155
|
|
|
* |
156
|
|
|
* @param $field - The field name |
157
|
|
|
* @param $value - The value to pull |
158
|
|
|
* @return - provides a fluent interface |
|
|
|
|
159
|
|
|
*/ |
160
|
|
|
protected function fieldPull(string $field, $value): self |
161
|
|
|
{ |
162
|
|
|
if (!array_key_exists('$pull', $this->changes)) { |
163
|
|
|
$this->changes['$pull'] = []; |
164
|
|
|
} |
165
|
|
|
$this->changes['$pull'][$field] = $value; |
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Takes all the changes from a `Modifiable` and copies them under a field name. |
171
|
|
|
* |
172
|
|
|
* @param $child - The object containing updates |
173
|
|
|
* @param $field - The field name |
174
|
|
|
* @return - provides a fluent interface |
|
|
|
|
175
|
|
|
*/ |
176
|
|
|
protected function aggregateChanges(Modifiable $child, string $field): self |
177
|
|
|
{ |
178
|
|
|
foreach ($child->getChanges() as $op => $sets) { |
179
|
|
|
if (!array_key_exists($op, $this->changes)) { |
180
|
|
|
$this->changes[$op] = []; |
181
|
|
|
} |
182
|
|
|
foreach ($sets as $k => $v) { |
183
|
|
|
$this->changes[$op]["$field.$k"] = $v; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.