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