/
DataReferenceUpdater.php
249 lines (205 loc) · 5.97 KB
/
DataReferenceUpdater.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
namespace Statamic\Data;
use Statamic\Fields\Fields;
use Statamic\Support\Arr;
abstract class DataReferenceUpdater
{
/**
* @var mixed
*/
protected $item;
/**
* @var mixed
*/
protected $originalValue;
/**
* @var mixed
*/
protected $newValue;
/**
* @var bool
*/
protected $updated;
/**
* Instantiate data reference updater.
*
* @param mixed $item
*/
public function __construct($item)
{
$this->item = $item;
}
/**
* Instantiate data reference updater.
*
* @param mixed $item
* @return static
*/
public static function item($item)
{
return new static($item);
}
/**
* Update references.
*
* @param mixed $originalValue
* @param mixed $newValue
*/
public function updateReferences($originalValue, $newValue)
{
$this->originalValue = $originalValue;
$this->newValue = $newValue;
$this->recursivelyUpdateFields($this->getTopLevelFields());
if ($this->updated) {
$this->item->save();
}
}
/**
* Get top level fields off item blueprint.
*
* @return \Illuminate\Support\Collection
*/
protected function getTopLevelFields()
{
return $this->item->blueprint()->fields()->all();
}
/**
* Recursively update fields (call `updateNestedFieldValues()` to initiate recursion).
*
* @param \Statamic\Fields\Fields $fields
* @param null|string $dottedPrefix
*/
abstract protected function recursivelyUpdateFields($fields, $dottedPrefix = null);
/**
* Update nested field values.
*
* @param \Illuminate\Support\Collection $fields
* @param null|string $dottedPrefix
* @return $this
*/
protected function updateNestedFieldValues($fields, $dottedPrefix)
{
$fields
->filter(function ($field) {
return in_array($field->type(), ['replicator', 'grid', 'bard']);
})
->each(function ($field) use ($dottedPrefix) {
$method = 'update'.ucfirst($field->type()).'Children';
$dottedKey = $dottedPrefix.$field->handle();
$this->{$method}($field, $dottedKey);
});
return $this;
}
/**
* Update replicator field children.
*
* @param \Statamic\Fields\Field $field
* @param string $dottedKey
*/
protected function updateReplicatorChildren($field, $dottedKey)
{
$data = $this->item->data();
$sets = Arr::get($data, $dottedKey);
collect($sets)->each(function ($set, $setKey) use ($dottedKey, $field) {
$dottedPrefix = "{$dottedKey}.{$setKey}.";
$setHandle = Arr::get($set, 'type');
$fields = Arr::get($field->config(), "sets.{$setHandle}.fields");
if ($setHandle && $fields) {
$this->recursivelyUpdateFields((new Fields($fields))->all(), $dottedPrefix);
}
});
}
/**
* Update grid field children.
*
* @param \Statamic\Fields\Field $field
* @param string $dottedKey
*/
protected function updateGridChildren($field, $dottedKey)
{
$data = $this->item->data();
$sets = Arr::get($data, $dottedKey);
collect($sets)->each(function ($set, $setKey) use ($dottedKey, $field) {
$dottedPrefix = "{$dottedKey}.{$setKey}.";
$fields = Arr::get($field->config(), 'fields');
if ($fields) {
$this->recursivelyUpdateFields((new Fields($fields))->all(), $dottedPrefix);
}
});
}
/**
* Update bard field children.
*
* @param \Statamic\Fields\Field $field
* @param string $dottedKey
*/
protected function updateBardChildren($field, $dottedKey)
{
$data = $this->item->data();
$sets = Arr::get($data, $dottedKey);
collect($sets)->each(function ($set, $setKey) use ($dottedKey, $field) {
$dottedPrefix = "{$dottedKey}.{$setKey}.attrs.values.";
$setHandle = Arr::get($set, 'attrs.values.type');
$fields = Arr::get($field->config(), "sets.{$setHandle}.fields");
if ($setHandle && $fields) {
$this->recursivelyUpdateFields((new Fields($fields))->all(), $dottedPrefix);
}
});
}
/**
* Get original value.
*
* @return mixed
*/
public function originalValue()
{
return $this->originalValue;
}
/**
* Get new value.
*
* @return mixed
*/
public function newValue()
{
return $this->newValue;
}
/**
* Update string value on item.
*
* @param \Statamic\Fields\Field $field
* @param null|string $dottedPrefix
*/
protected function updateStringValue($field, $dottedPrefix)
{
$data = $this->item->data()->all();
$dottedKey = $dottedPrefix.$field->handle();
if (Arr::get($data, $dottedKey) !== $this->originalValue()) {
return;
}
Arr::set($data, $dottedKey, $this->newValue());
$this->item->data($data);
$this->updated = true;
}
/**
* Update array value on item.
*
* @param \Statamic\Fields\Field $field
* @param null|string $dottedPrefix
*/
protected function updateArrayValue($field, $dottedPrefix)
{
$data = $this->item->data()->all();
$dottedKey = $dottedPrefix.$field->handle();
$fieldData = collect(Arr::dot(Arr::get($data, $dottedKey, [])));
if (! $fieldData->contains($this->originalValue())) {
return;
}
$fieldData->transform(function ($value) {
return $value === $this->originalValue() ? $this->newValue() : $value;
});
Arr::set($data, $dottedKey, $fieldData->all());
$this->item->data($data);
$this->updated = true;
}
}