This repository has been archived by the owner on Aug 16, 2023. It is now read-only.
/
FieldCollection.php
205 lines (172 loc) · 4.52 KB
/
FieldCollection.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
<?php namespace Rocket\Entities;
use Illuminate\Support\Collection;
use Rocket\Entities\Exceptions\InvalidFieldTypeException;
use Rocket\Entities\Exceptions\ItemCountException;
use Rocket\Entities\Exceptions\NullValueException;
class FieldCollection extends \Illuminate\Support\Collection
{
/**
* @var int The max items this collection can hold
*/
protected $maxItems = 1;
/**
* @var array The Collection configuration
*/
protected $configuration = [];
/**
* @var string The type of this collection
*/
protected $type;
/**
* @var array An array of fields that were deleted
*/
protected $deleted = [];
/**
* Initialize a collection with the configuration
*
* @param array $configuration
* @throws InvalidFieldTypeException
* @return static
*/
public static function initField($configuration = [])
{
if (!array_key_exists('type', $configuration) || !class_exists($configuration['type'])) {
throw new InvalidFieldTypeException('You did not specify a type on this class.');
}
$collection = new static();
$collection->configuration = $configuration;
$collection->type = $configuration['type'];
if (array_key_exists('max_items', $configuration)) {
$collection->maxItems = $configuration['max_items'];
}
return $collection;
}
/**
* Validate input of OffsetSet
*
* @param $key
* @param $value
* @throws ItemCountException
* @throws NullValueException
*/
protected function validateSet($key, $value) {
if ((is_null($key) || !array_key_exists($key, $this->items)) && $this->count() >= $this->getMaxItems()) {
throw new ItemCountException('The maximum number of items has been reached on this field.');
}
if (is_null($key) && is_null($value)) {
throw new NullValueException('You cannot add a null value');
}
}
/**
* Get a field from a value
*
* @param Field|mixed $value
* @return Field
*/
protected function getFieldInstance($value) {
if ($value instanceof Field) {
return $value;
}
$container = new $this->type();
$container->value = $value;
return $container;
}
/**
* {@inheritdoc}
*/
public function offsetSet($key, $value)
{
$this->validateSet($key, $value);
if (is_null($value)) {
$this->offsetUnset($key);
return;
}
if (is_null($key)) {
$this->items[] = $this->getFieldInstance($value);
return;
}
if ($value instanceof Field && $this->has($key)) {
$this->deleted[] = $this->items[$key];
}
$this->items[$key] = $this->getFieldInstance($value);
}
/**
* {@inheritdoc}
*/
public function offsetGet($key)
{
return parent::offsetGet($key)->value;
}
/**
* {@inheritdoc}
*/
public function offsetUnset($key)
{
if ($this->offsetExists($key)) {
$this->deleted[] = $this->items[$key];
}
parent::offsetUnset($key);
}
/**
* Get all deleted fields
*
* @return Collection
*/
public function deleted()
{
return new Collection(array_diff($this->deleted, $this->items));
}
/**
* Mark the content as current and saved
*/
public function syncOriginal()
{
$this->deleted = [];
}
/**
* Remove all items in this collection
*
* @return void
*/
public function clear()
{
foreach ($this->items as $item) {
$this->deleted[] = $item;
}
$this->items = [];
}
/**
* Get the number of items possible in this collection.
*
* @return int
*/
public function getMaxItems()
{
return $this->maxItems;
}
/**
* As we use a field collection even if we have only one value, we use it that way.
*
* @return array|mixed|null
*/
public function toArray()
{
if ($this->maxItems != 1) {
return parent::toArray();
}
if (!array_key_exists(0, $this->items)) {
return null;
}
return $this->get(0)->toArray();
}
/**
* {@inheritdoc}
*/
public function __toString()
{
if ($this->maxItems == 1) {
return $this->items[0]->value;
}
return 'Array';
}
}