Completed
Push — master ( 24c826...e3a2d5 )
by Stéphane
13:49
created

FieldCollection::deleted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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
     * Validate input of OffsetSet
56
     *
57
     * @param $key
58
     * @param $value
59
     * @throws ItemCountException
60
     * @throws NullValueException
61
     */
62 81
    protected function validateSet($key, $value) {
63 81
        if ((is_null($key) || !array_key_exists($key, $this->items)) && $this->count() >= $this->getMaxItems()) {
64 3
            throw new ItemCountException('The maximum number of items has been reached on this field.');
65
        }
66
67 81
        if (is_null($key) && is_null($value)) {
68 3
            throw new NullValueException('You cannot add a null value');
69
        }
70 78
    }
71
72
    /**
73
     * Get a field from a value
74
     *
75
     * @param Field|mixed $value
76
     * @return Field
77
     */
78 78
    protected function getFieldInstance($value) {
79 78
        if ($value instanceof Field) {
80 18
            return $value;
81
        }
82
83 78
        $container = new $this->type();
84 78
        $container->value = $value;
85
86 78
        return $container;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 81
    public function offsetSet($key, $value)
93
    {
94 81
        $this->validateSet($key, $value);
95
96 78
        if (is_null($value)) {
97 6
            $this->offsetUnset($key);
98
99 6
            return;
100
        }
101
102 78
        if (is_null($key)) {
103 54
            $this->items[] = $this->getFieldInstance($value);
104
105 54
            return;
106
        }
107
108 45
        if ($value instanceof Field && $this->has($key)) {
109 6
            $this->deleted[] = $this->items[$key];
110 6
        }
111
112 45
        $this->items[$key] = $this->getFieldInstance($value);
113 45
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 6
    public function offsetGet($key)
119
    {
120 6
        return parent::offsetGet($key)->value;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 24
    public function offsetUnset($key)
127
    {
128 24
        if ($this->offsetExists($key)) {
129 24
            $this->deleted[] = $this->items[$key];
130 24
        }
131 24
        parent::offsetUnset($key);
132 24
    }
133
134
    /**
135
     * Get all deleted fields
136
     *
137
     * @return Collection
138
     */
139 27
    public function deleted()
140
    {
141 27
        return new Collection(array_diff($this->deleted, $this->items));
142
    }
143
144
    /**
145
     * Mark the content as current and saved
146
     */
147 15
    public function syncOriginal()
148
    {
149 15
        $this->deleted = [];
150 15
    }
151
152
    /**
153
     * Remove all items in this collection
154
     *
155
     * @return void
156
     */
157 15
    public function clear()
158
    {
159 15
        foreach ($this->items as $item) {
160 6
            $this->deleted[] = $item;
161 15
        }
162
163 15
        $this->items = [];
164 15
    }
165
166
    /**
167
     * Get the number of items possible in this collection.
168
     *
169
     * @return int
170
     */
171 84
    public function getMaxItems()
172
    {
173 84
        return $this->maxItems;
174
    }
175
176
    /**
177
     * As we use a field collection even if we have only one value, we use it that way.
178
     *
179
     * @return array|mixed|null
180
     */
181 43
    public function toArray()
182
    {
183 43
        if ($this->maxItems != 1) {
184 31
            return parent::toArray();
185
        }
186
187 28
        if (!array_key_exists(0, $this->items)) {
188 16
            return null;
189
        }
190
191 12
        return $this->get(0)->toArray();
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197 6
    public function __toString()
198
    {
199 6
        if ($this->maxItems == 1) {
200 6
            return $this->items[0]->value;
201
        }
202
203 3
        return 'Array';
204
    }
205
}
206