Completed
Push — master ( 096673...3f861c )
by Stéphane
11:15
created

FieldCollection::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

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