Completed
Push — master ( 8507e2...2d694e )
by Lars
01:39
created

Collection::construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 1
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arrayy\Collection;
6
7
use Arrayy\ArrayyIterator;
8
use Arrayy\TypeCheck\TypeCheckArray;
9
use Arrayy\TypeCheck\TypeCheckInterface;
10
11
/**
12
 * A collection represents a group of objects.
13
 *
14
 * Each object in the collection is of a specific, defined type.
15
 *
16
 * This is a direct implementation of `CollectionSetTypeInterface`,
17
 * which provides a simple api for your collections.
18
 *
19
 * Example usage:
20
 *
21
 * ``` php
22
 * $collection = new \Arrayy\Collection\Collection(\My\FooInterface::class);
23
 * $collection->add(new \My\Foo());
24
 * $collection->add(new \My\Foo());
25
 *
26
 * foreach ($collection as $foo) {
27
 *     if ($foo instanceof \My\FooInterface) {
28
 *         // Do something with $foo
29
 *     }
30
 * }
31
 * ```
32
 *
33
 * It is preferable to subclass `AbstractCollection` to create your own typed
34
 * collections. For example:
35
 *
36
 * ``` php
37
 * namespace My;
38
 *
39
 * class FooCollection extends \Arrayy\Collection\AbstractCollection
40
 * {
41
 *     public function getType()
42
 *     {
43
 *         return FooInterface::class;
44
 *     }
45
 * }
46
 * ```
47
 *
48
 * And then use it similarly to the earlier example:
49
 *
50
 * ``` php
51
 * namespace My;
52
 *
53
 * $fooCollection = new \My\FooCollection();
54
 * $fooCollection->add(new \My\Foo());
55
 * $fooCollection->add(new \My\Foo());
56
 *
57
 * foreach ($fooCollection as $foo) {
58
 *     if ($foo instanceof \My\FooInterface) {
59
 *         // Do something with $foo
60
 *     }
61
 * }
62
 * ```
63
 *
64
 * INFO: this collection thingy is inspired by https://github.com/ramsey/collection/
65
 */
66
class Collection extends AbstractCollection
67
{
68
    /**
69
     * Constructs a collection object of the specified type, optionally with the
70
     * specified data.
71
     *
72
     * @param mixed                                    $data
73
     *                                                                               <p>
74
     *                                                                               The initial items to store in the collection.
75
     *                                                                               </p>
76
     * @param string                                   $iteratorClass                optional <p>
77
     *                                                                               You can overwrite the ArrayyIterator, but mostly you don't
78
     *                                                                               need this option.
79
     *                                                                               </p>
80
     * @param bool                                     $checkPropertiesInConstructor optional <p>
81
     *                                                                               You need to extend the "Arrayy"-class and you need to set
82
     *                                                                               the $checkPropertiesMismatchInConstructor class property
83
     *                                                                               to
84
     *                                                                               true, otherwise this option didn't not work anyway.
85
     *                                                                               </p>
86
     * @param TypeCheckArray|TypeCheckInterface[]|null $type
87
     */
88 55
    public function __construct(
89
        $data = [],
90
        string $iteratorClass = null,
91
        bool $checkPropertiesInConstructor = null,
92
        TypeCheckArray $type = null
93
    ) {
94
        // fallback
95 55
        if ($iteratorClass === null) {
96 45
            $iteratorClass = ArrayyIterator::class;
97
        }
98 55
        if ($checkPropertiesInConstructor === null) {
99 45
            $checkPropertiesInConstructor = true;
100
        }
101
102 55
        if ($type !== null) {
103 5
            $this->properties = $type;
104
        }
105
106 55
        parent::__construct(
107 55
            $data,
108 55
            $iteratorClass,
109 55
            $checkPropertiesInConstructor
110
        );
111 44
    }
112
113
    /**
114
     * @param string|TypeCheckArray|TypeCheckInterface[] $type
115
     * @param array                                      $data
116
     * @param bool                                       $checkPropertiesInConstructorAndType
117
     *
118
     * @return static
119
     */
120 5
    public static function construct(
121
        $type,
122
        $data = [],
123
        bool $checkPropertiesInConstructorAndType = true
124
    ): self {
125 5
        $type = self::convertIntoTypeCheckArray($type);
126
127 5
        return new static(
128 5
            $data,
129 5
            ArrayyIterator::class,
130 5
            $checkPropertiesInConstructorAndType,
131 5
            $type
132
        );
133
    }
134
135
    /**
136
     * The type (FQCN) associated with this collection.
137
     *
138
     * @return TypeCheckArray|TypeCheckInterface[]
139
     */
140 5
    public function getType()
141
    {
142 5
        return $this->properties;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->properties; of type array|Arrayy\TypeCheck\TypeCheckArray adds the type array to the return on line 142 which is incompatible with the return type declared by the interface Arrayy\Collection\CollectionInterface::getType of type string|string[]|Arrayy\T...ck\TypeCheckInterface[].
Loading history...
143
    }
144
145
    /**
146
     * Get a base Collection instance from this Collection.
147
     *
148
     * @return self
149
     */
150 1
    public function toBase(): self
151
    {
152
        /** @noinspection SelfClassReferencingInspection */
153 1
        return Collection::construct($this->getType(), $this->getArray());
154
    }
155
}
156