1
|
|
|
<?php namespace Rocket\Entities; |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Collection; |
4
|
|
|
use Rocket\Entities\Exceptions\InvalidFieldTypeException; |
5
|
|
|
use Rocket\Entities\Exceptions\ItemCountException; |
6
|
|
|
use Rocket\Entities\Exceptions\NullValueException; |
7
|
|
|
|
8
|
|
|
class FieldCollection extends \Illuminate\Support\Collection |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var int The max items this collection can hold |
12
|
|
|
*/ |
13
|
|
|
protected $maxItems = 1; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array The Collection configuration |
17
|
|
|
*/ |
18
|
|
|
protected $configuration = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string The type of this collection |
22
|
|
|
*/ |
23
|
|
|
protected $type; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array An array of fields that were deleted |
27
|
|
|
*/ |
28
|
|
|
protected $deleted = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Initialize a collection with the configuration |
32
|
|
|
* |
33
|
|
|
* @param array $configuration |
34
|
|
|
* @throws InvalidFieldTypeException |
35
|
|
|
* @return static |
36
|
|
|
*/ |
37
|
102 |
|
public static function initField($configuration = []) |
38
|
|
|
{ |
39
|
102 |
|
if (!array_key_exists('type', $configuration) || !class_exists($configuration['type'])) { |
40
|
3 |
|
throw new InvalidFieldTypeException('You did not specify a type on this class.'); |
41
|
|
|
} |
42
|
|
|
|
43
|
99 |
|
$collection = new static(); |
44
|
99 |
|
$collection->configuration = $configuration; |
45
|
99 |
|
$collection->type = $configuration['type']; |
46
|
|
|
|
47
|
99 |
|
if (array_key_exists('max_items', $configuration)) { |
48
|
84 |
|
$collection->maxItems = $configuration['max_items']; |
49
|
84 |
|
} |
50
|
|
|
|
51
|
99 |
|
return $collection; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
81 |
|
public function offsetSet($key, $value) |
58
|
|
|
{ |
59
|
81 |
|
if ((is_null($key) || !array_key_exists($key, $this->items)) && $this->count() >= $this->getMaxItems()) { |
60
|
3 |
|
throw new ItemCountException('The maximum number of items has been reached on this field.'); |
61
|
|
|
} |
62
|
|
|
|
63
|
81 |
|
if (is_null($key) && is_null($value)) { |
64
|
3 |
|
throw new NullValueException('You cannot add a null value'); |
65
|
|
|
} |
66
|
|
|
|
67
|
78 |
|
if (is_null($value)) { |
68
|
6 |
|
$this->offsetUnset($key); |
69
|
|
|
|
70
|
6 |
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
78 |
|
if ($value instanceof Field) { |
74
|
18 |
|
$container = $value; |
75
|
18 |
|
} else { |
76
|
78 |
|
$container = new $this->type(); |
77
|
78 |
|
$container->value = $value; |
78
|
|
|
} |
79
|
|
|
|
80
|
78 |
|
if (is_null($key)) { |
81
|
54 |
|
$this->items[] = $container; |
82
|
54 |
|
} else { |
83
|
45 |
|
if ($value instanceof Field && $this->has($key)) { |
84
|
6 |
|
$this->deleted[] = $this->items[$key]; |
85
|
6 |
|
} |
86
|
45 |
|
$this->items[$key] = $container; |
87
|
|
|
} |
88
|
78 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
6 |
|
public function offsetGet($key) |
94
|
|
|
{ |
95
|
6 |
|
return parent::offsetGet($key)->value; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
24 |
|
public function offsetUnset($key) |
102
|
|
|
{ |
103
|
24 |
|
if ($this->offsetExists($key)) { |
104
|
24 |
|
$this->deleted[] = $this->items[$key]; |
105
|
24 |
|
} |
106
|
24 |
|
parent::offsetUnset($key); |
107
|
24 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get all deleted fields |
111
|
|
|
* |
112
|
|
|
* @return Collection |
113
|
|
|
*/ |
114
|
27 |
|
public function deleted() |
115
|
|
|
{ |
116
|
27 |
|
return new Collection(array_diff($this->deleted, $this->items)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Mark the content as current and saved |
121
|
|
|
*/ |
122
|
15 |
|
public function syncOriginal() |
123
|
|
|
{ |
124
|
15 |
|
$this->deleted = []; |
125
|
15 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Remove all items in this collection |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
15 |
|
public function clear() |
133
|
|
|
{ |
134
|
15 |
|
foreach ($this->items as $item) { |
135
|
6 |
|
$this->deleted[] = $item; |
136
|
15 |
|
} |
137
|
|
|
|
138
|
15 |
|
$this->items = []; |
139
|
15 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get the number of items possible in this collection. |
143
|
|
|
* |
144
|
|
|
* @return int |
145
|
|
|
*/ |
146
|
84 |
|
public function getMaxItems() |
147
|
|
|
{ |
148
|
84 |
|
return $this->maxItems; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* As we use a field collection even if we have only one value, we use it that way. |
153
|
|
|
* |
154
|
|
|
* @return array|mixed|null |
155
|
|
|
*/ |
156
|
43 |
|
public function toArray() |
157
|
|
|
{ |
158
|
43 |
|
if ($this->maxItems != 1) { |
159
|
31 |
|
return parent::toArray(); |
160
|
|
|
} |
161
|
|
|
|
162
|
28 |
|
if (!array_key_exists(0, $this->items)) { |
163
|
16 |
|
return null; |
164
|
|
|
} |
165
|
|
|
|
166
|
12 |
|
return $this->get(0)->toArray(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
6 |
|
public function __toString() |
173
|
|
|
{ |
174
|
6 |
|
if ($this->maxItems == 1) { |
175
|
6 |
|
return $this->items[0]->value; |
176
|
|
|
} |
177
|
|
|
|
178
|
3 |
|
return 'Array'; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|