AbstractObjectCollection::addItem()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 14
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 2
nop 1
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/skeleton/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/skeleton
7
 */
8
9
namespace Flipbox\Skeleton\Collections;
10
11
use Flipbox\Skeleton\Exceptions\InvalidCollectionItemException;
12
use Flipbox\Skeleton\Helpers\ArrayHelper;
13
use Flipbox\Skeleton\Object\AbstractObject;
14
use Flipbox\Skeleton\Object\ObjectInterface;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 2.0.0
19
 */
20
abstract class AbstractObjectCollection extends AbstractObject implements ObjectCollectionInterface
21
{
22
    /**
23
     * The item instance class
24
     */
25
    const ITEM_CLASS_INSTANCE = ObjectInterface::class;
26
27
    /**
28
     * A collection of items.
29
     *
30
     * @var array|\ArrayIterator
31
     */
32
    protected $items = [];
33
34
    /*******************************************
35
     * ITEMS
36
     *******************************************/
37
38
    /**
39
     * Add an item to a collection
40
     *
41
     * @param $item
42
     * @return static
43
     * @throws InvalidCollectionItemException
44
     */
45
    public function addItem($item)
46
    {
47
        // Item class instance
48
        $itemInstance = static::ITEM_CLASS_INSTANCE;
49
50
        // Validate instance
51
        if ($itemInstance && !$item instanceof $itemInstance) {
52
            throw new InvalidCollectionItemException(
53
                sprintf(
54
                    "Unable to add item to collection because it must be an instance of '%s'",
55
                    static::ITEM_CLASS_INSTANCE
56
                )
57
            );
58
        }
59
60
        $this->items[] = $item;
61
62
        return $this;
63
    }
64
65
    /**
66
     * @param array $items
67
     * @return static
68
     */
69
    public function setItems($items = [])
70
    {
71
        $this->items = [];
72
73
        // Make sure we can iterate over it
74
        if (!is_array($items) && !$items instanceof \Traversable) {
75
            $items = [$items];
76
        }
77
78
        foreach ($items as $item) {
79
            $this->addItem($item);
80
        }
81
82
        return $this;
83
    }
84
85
    /**
86
     * @param null $indexBy
87
     * @return ObjectInterface[]
88
     */
89
    public function getItems($indexBy = null)
90
    {
91
        return $this->items;
92
    }
93
94
    /**
95
     * @return \ArrayIterator
96
     */
97
    public function getIterator(): \ArrayIterator
98
    {
99
        $items = $this->getItems();
100
101
        if ($items instanceof \ArrayIterator) {
102
            return $items;
103
        }
104
105
        return new \ArrayIterator($items);
106
    }
107
108
    /**
109
     * @return mixed|null
110
     */
111
    public function getFirstItem()
112
    {
113
        if ($items = $this->getItems()) {
114
            return ArrayHelper::getFirstValue($items);
115
        }
116
117
        return null;
118
    }
119
120
121
    /*******************************************
122
     * MERGE
123
     *******************************************/
124
125
    /**
126
     * Merge one collection into another
127
     *
128
     * @param ObjectCollectionInterface $collection
129
     * @return static
130
     */
131
    public function merge(ObjectCollectionInterface $collection)
132
    {
133
        $this->items = array_merge(
134
            $this->getItems(),
135
            $collection->getItems()
136
        );
137
138
        return $this;
139
    }
140
}
141