FieldCollection   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 4
dl 0
loc 213
ccs 67
cts 67
cp 1
rs 9.92
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A initField() 0 16 4
B validateSet() 0 11 7
A getFieldInstance() 0 11 2
A offsetSet() 0 22 5
A offsetGet() 0 4 1
A offsetUnset() 0 7 2
A deleted() 0 4 1
A syncOriginal() 0 4 1
A clear() 0 8 2
A getMaxItems() 0 4 1
A toArray() 0 12 3
A __toString() 0 8 2
1
<?php
2
/**
3
 * A FieldCollection is a Collection that wraps all values in their Field models.
4
 *
5
 * It will also return the actual value but not the Field model.
6
 */
7
namespace Rocket\Entities;
8
9
use Illuminate\Support\Collection;
10
use Rocket\Entities\Exceptions\InvalidFieldTypeException;
11
use Rocket\Entities\Exceptions\ItemCountException;
12
use Rocket\Entities\Exceptions\NullValueException;
13
14
/**
15
 * Smart Collection for entities
16
 */
17
class FieldCollection extends \Illuminate\Support\Collection
18
{
19
    /**
20
     * @var int The max items this collection can hold
21
     */
22
    protected $maxItems = 1;
23
24
    /**
25
     * @var array The Collection configuration
26
     */
27
    protected $configuration = [];
28
29
    /**
30
     * @var string The type of this collection
31
     */
32
    protected $type;
33
34
    /**
35
     * @var array An array of fields that were deleted
36
     */
37
    protected $deleted = [];
38
39
    /**
40
     * Initialize a collection with the configuration
41
     *
42
     * @param array $configuration
43
     * @throws InvalidFieldTypeException
44
     * @return static
45
     */
46 159
    public static function initField($configuration = [])
47
    {
48 159
        if (!array_key_exists('type', $configuration) || !class_exists($configuration['type'])) {
49 3
            throw new InvalidFieldTypeException('You did not specify a type on this class.');
50
        }
51
52 156
        $collection = new static();
53 156
        $collection->configuration = $configuration;
54 156
        $collection->type = $configuration['type'];
55
56 156
        if (array_key_exists('max_items', $configuration)) {
57 141
            $collection->maxItems = $configuration['max_items'];
58 141
        }
59
60 156
        return $collection;
61
    }
62
63
    /**
64
     * Validate input of OffsetSet
65
     *
66
     * @param $key
67
     * @param $value
68
     * @throws ItemCountException
69
     * @throws NullValueException
70
     */
71 129
    protected function validateSet($key, $value)
72
    {
73 129
        $maxItems = $this->getMaxItems();
74 129
        if ((is_null($key) || !array_key_exists($key, $this->items)) && $maxItems != 0 && $this->count() >= $maxItems) {
75 3
            throw new ItemCountException('The maximum number of items has been reached on this field.');
76
        }
77
78 129
        if (is_null($key) && is_null($value)) {
79 3
            throw new NullValueException('You cannot add a null value');
80
        }
81 126
    }
82
83
    /**
84
     * Get a field from a value
85
     *
86
     * @param Field|mixed $value
87
     * @return Field
88
     */
89 126
    protected function getFieldInstance($value)
90
    {
91 126
        if ($value instanceof Field) {
92 39
            return $value;
93
        }
94
95 126
        $container = new $this->type();
96 126
        $container->value = $value;
97
98 126
        return $container;
99
    }
100
101
    /**
102
     * Set the item at a given offset.
103
     *
104
     * @param  mixed  $key
105
     * @param  mixed  $value
106
     * @return void
107
     */
108 129
    public function offsetSet($key, $value)
109
    {
110 129
        $this->validateSet($key, $value);
111
112 126
        if (is_null($value)) {
113 6
            $this->offsetUnset($key);
114
115 6
            return;
116
        }
117
118 126
        if (is_null($key)) {
119 96
            $this->items[] = $this->getFieldInstance($value);
120
121 96
            return;
122
        }
123
124 66
        if ($value instanceof Field && $this->has($key)) {
125 9
            $this->deleted[] = $this->items[$key];
126 9
        }
127
128 66
        $this->items[$key] = $this->getFieldInstance($value);
129 66
    }
130
131
    /**
132
     * Get an item at a given offset.
133
     *
134
     * @param  mixed  $key
135
     * @return mixed
136
     */
137 9
    public function offsetGet($key)
138
    {
139 9
        return parent::offsetGet($key)->value;
140
    }
141
142
    /**
143
     * Unset the item at a given offset.
144
     *
145
     * @param  string  $key
146
     * @return void
147
     */
148 33
    public function offsetUnset($key)
149
    {
150 33
        if ($this->offsetExists($key)) {
151 33
            $this->deleted[] = $this->items[$key];
152 33
        }
153 33
        parent::offsetUnset($key);
154 33
    }
155
156
    /**
157
     * Get all deleted fields
158
     *
159
     * @return Collection
160
     */
161 63
    public function deleted()
162
    {
163 63
        return new Collection(array_diff($this->deleted, $this->items));
164
    }
165
166
    /**
167
     * Mark the content as current and saved
168
     */
169 60
    public function syncOriginal()
170
    {
171 60
        $this->deleted = [];
172 60
    }
173
174
    /**
175
     * Remove all items in this collection
176
     *
177
     * @return void
178
     */
179 28
    public function clear()
180
    {
181 28
        foreach ($this->items as $item) {
182 13
            $this->deleted[] = $item;
183 28
        }
184
185 28
        $this->items = [];
186 28
    }
187
188
    /**
189
     * Get the number of items possible in this collection.
190
     *
191
     * @return int
192
     */
193 138
    public function getMaxItems()
194
    {
195 138
        return $this->maxItems;
196
    }
197
198
    /**
199
     * As we use a field collection even if we have only one value, we use it that way.
200
     *
201
     * @return array|mixed|null
202
     */
203 57
    public function toArray()
204
    {
205 57
        if ($this->getMaxItems() != 1) {
206 45
            return parent::toArray();
207
        }
208
209 39
        if (!array_key_exists(0, $this->items)) {
210 27
            return null;
211
        }
212
213 18
        return $this->get(0)->toArray();
214
    }
215
216
    /**
217
     * Convert the collection to its string representation.
218
     *
219
     * @return string
220
     */
221 3
    public function __toString()
222
    {
223 3
        if ($this->getMaxItems() == 1) {
224 3
            return $this->items[0]->value;
225
        }
226
227 3
        return 'Array';
228
    }
229
}
230