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
|
|
|
* @return static |
35
|
|
|
*/ |
36
|
93 |
|
public static function initField($configuration = []) |
37
|
|
|
{ |
38
|
93 |
|
if (!array_key_exists('type', $configuration) || !class_exists($configuration['type'])) { |
39
|
3 |
|
throw new InvalidFieldTypeException('You did not specify a type on this class.'); |
40
|
|
|
} |
41
|
|
|
|
42
|
90 |
|
$collection = new static(); |
43
|
90 |
|
$collection->configuration = $configuration; |
44
|
90 |
|
$collection->type = $configuration['type']; |
45
|
|
|
|
46
|
90 |
|
if (array_key_exists('max_items', $configuration)) { |
47
|
75 |
|
$collection->maxItems = $configuration['max_items']; |
48
|
75 |
|
} |
49
|
|
|
|
50
|
90 |
|
return $collection; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
72 |
|
public function offsetSet($key, $value) |
57
|
|
|
{ |
58
|
72 |
|
if ((is_null($key) || !array_key_exists($key, $this->items)) && $this->count() >= $this->getMaxItems()) { |
59
|
3 |
|
throw new ItemCountException('The maximum number of items has been reached on this field.'); |
60
|
|
|
} |
61
|
|
|
|
62
|
72 |
|
if (is_null($key) && is_null($value)) { |
63
|
3 |
|
throw new NullValueException('You cannot add a null value'); |
64
|
|
|
} |
65
|
|
|
|
66
|
69 |
|
if (is_null($value)) { |
67
|
6 |
|
$this->offsetUnset($key); |
68
|
6 |
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
69 |
|
if ($value instanceof Field) { |
72
|
12 |
|
$container = $value; |
73
|
12 |
|
} else { |
74
|
69 |
|
$container = new $this->type(); |
75
|
69 |
|
$container->value = $value; |
76
|
|
|
} |
77
|
|
|
|
78
|
69 |
|
if (is_null($key)) { |
79
|
54 |
|
$this->items[] = $container; |
80
|
54 |
|
} else { |
81
|
36 |
|
$this->items[$key] = $container; |
82
|
|
|
} |
83
|
69 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get an item at a given offset. |
87
|
|
|
* |
88
|
|
|
* @param mixed $key |
89
|
|
|
* @return mixed |
90
|
|
|
*/ |
91
|
6 |
|
public function offsetGet($key) |
92
|
|
|
{ |
93
|
6 |
|
return parent::offsetGet($key)->value; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Unset the item at a given offset. |
98
|
|
|
* |
99
|
|
|
* @param string $key |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
21 |
|
public function offsetUnset($key) |
103
|
|
|
{ |
104
|
21 |
|
if ($this->offsetExists($key)) { |
105
|
21 |
|
$this->deleted[] = $this->items[$key]; |
106
|
21 |
|
} |
107
|
21 |
|
parent::offsetUnset($key); |
108
|
21 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get all deleted fields |
112
|
|
|
* |
113
|
|
|
* @return Collection |
114
|
|
|
*/ |
115
|
18 |
|
public function deleted() |
116
|
|
|
{ |
117
|
18 |
|
return new Collection($this->deleted); |
118
|
|
|
} |
119
|
|
|
|
120
|
15 |
|
public function syncOriginal() |
121
|
|
|
{ |
122
|
15 |
|
$this->deleted = []; |
123
|
15 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Remove all items in this collection |
127
|
|
|
* |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
12 |
|
public function clear() |
131
|
|
|
{ |
132
|
12 |
|
$this->items = []; |
133
|
12 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the number of items possible in this collection. |
137
|
|
|
* |
138
|
|
|
* @return int |
139
|
|
|
*/ |
140
|
75 |
|
public function getMaxItems() |
141
|
|
|
{ |
142
|
75 |
|
return $this->maxItems; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* As we use a field collection even if we have only one value, we use it that way. |
147
|
|
|
* |
148
|
|
|
* @return array|mixed|null |
149
|
|
|
*/ |
150
|
43 |
|
public function toArray() |
151
|
|
|
{ |
152
|
43 |
|
if ($this->maxItems != 1) { |
153
|
31 |
|
return parent::toArray(); |
154
|
|
|
} |
155
|
|
|
|
156
|
28 |
|
if (!array_key_exists(0, $this->items)) { |
157
|
16 |
|
return null; |
158
|
|
|
} |
159
|
|
|
|
160
|
12 |
|
return $this->get(0)->toArray(); |
161
|
|
|
} |
162
|
|
|
|
163
|
6 |
|
public function __toString() |
164
|
|
|
{ |
165
|
6 |
|
if ($this->maxItems == 1) { |
166
|
6 |
|
return $this->items[0]->value; |
167
|
|
|
} |
168
|
|
|
|
169
|
3 |
|
return 'Array'; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|