1
|
|
|
<?php namespace Rocket\Entities; |
2
|
|
|
|
3
|
|
|
use Rocket\Entities\Exceptions\InvalidFieldTypeException; |
4
|
|
|
use Rocket\Entities\Exceptions\ItemCountException; |
5
|
|
|
|
6
|
|
|
class FieldCollection extends \Illuminate\Support\Collection |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @var int The max items this collection can hold |
10
|
|
|
*/ |
11
|
|
|
protected $maxItems = 1; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var array The Collection configuration |
15
|
|
|
*/ |
16
|
|
|
protected $configuration = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string The type of this collection |
20
|
|
|
*/ |
21
|
|
|
protected $type; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Initialize a collection with the configuration |
25
|
|
|
* |
26
|
|
|
* @param array $configuration |
27
|
|
|
* @return static |
28
|
|
|
*/ |
29
|
66 |
|
public static function initField($configuration = []) |
30
|
|
|
{ |
31
|
66 |
|
if (!array_key_exists('type', $configuration) || !class_exists($configuration['type'])) { |
32
|
|
|
throw new InvalidFieldTypeException('You did not specify a type on this class.'); |
33
|
|
|
} |
34
|
|
|
|
35
|
66 |
|
$collection = new static(); |
36
|
66 |
|
$collection->configuration = $configuration; |
37
|
66 |
|
$collection->type = $configuration['type']; |
38
|
|
|
|
39
|
66 |
|
if (array_key_exists('max_items', $configuration)) { |
40
|
54 |
|
$collection->maxItems = $configuration['max_items']; |
41
|
54 |
|
} |
42
|
|
|
|
43
|
66 |
|
return $collection; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
51 |
|
public function offsetSet($key, $value) |
50
|
|
|
{ |
51
|
51 |
|
if ((is_null($key) || !array_key_exists($key, $this->items)) && $this->count() >= $this->getMaxItems()) { |
52
|
3 |
|
throw new ItemCountException('The maximum number of items has been reached on this field.'); |
53
|
|
|
} |
54
|
|
|
|
55
|
51 |
|
if ($value instanceof Field) { |
56
|
3 |
|
$container = $value; |
57
|
3 |
|
} else { |
58
|
51 |
|
$container = new $this->type(); |
59
|
51 |
|
$container->value = $value; |
60
|
|
|
} |
61
|
|
|
|
62
|
51 |
|
if (is_null($key)) { |
63
|
42 |
|
$this->items[] = $container; |
64
|
42 |
|
} else { |
65
|
21 |
|
$this->items[$key] = $container; |
66
|
|
|
} |
67
|
51 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get an item at a given offset. |
71
|
|
|
* |
72
|
|
|
* @param mixed $key |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
6 |
|
public function offsetGet($key) |
76
|
|
|
{ |
77
|
6 |
|
$value = parent::offsetGet($key); |
78
|
|
|
|
79
|
6 |
|
if (!$value) { |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
6 |
|
return $value->value; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Remove all items in this collection |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
9 |
|
public function clear() |
92
|
|
|
{ |
93
|
9 |
|
$this->items = []; |
94
|
9 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the number of items possible in this collection. |
98
|
|
|
* |
99
|
|
|
* @return int |
100
|
|
|
*/ |
101
|
54 |
|
public function getMaxItems() |
102
|
|
|
{ |
103
|
54 |
|
return $this->maxItems; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* As we use a field collection even if we have only one value, we use it that way. |
108
|
|
|
* |
109
|
|
|
* @return array|mixed|null |
110
|
|
|
*/ |
111
|
30 |
|
public function toArray() |
112
|
|
|
{ |
113
|
30 |
|
if ($this->maxItems != 1) { |
114
|
18 |
|
return parent::toArray(); |
115
|
|
|
} |
116
|
|
|
|
117
|
18 |
|
if (!array_key_exists(0, $this->items)) { |
118
|
9 |
|
return null; |
119
|
|
|
} |
120
|
|
|
|
121
|
9 |
|
return $this->get(0)->toArray(); |
122
|
|
|
} |
123
|
|
|
|
124
|
6 |
|
public function __toString() |
125
|
|
|
{ |
126
|
6 |
|
if ($this->maxItems == 1) { |
127
|
6 |
|
return $this->items[0]->value; |
128
|
|
|
} |
129
|
|
|
|
130
|
3 |
|
return 'Array'; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|