Completed
Push — master ( 2102da...c6b695 )
by Lars
02:18
created

Collection   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getType() 0 4 1
A toBase() 0 4 1
1
<?php
2
3
namespace Arrayy\Collection;
4
5
/**
6
 * A collection represents a group of objects.
7
 *
8
 * Each object in the collection is of a specific, defined type.
9
 *
10
 * This is a direct implementation of `CollectionSetTypeInterface`,
11
 * which provides a simple api for your collections.
12
 *
13
 * Example usage:
14
 *
15
 * ``` php
16
 * $collection = new \Arrayy\Collection\Collection(\My\FooInterface::class);
17
 * $collection->add(new \My\Foo());
18
 * $collection->add(new \My\Foo());
19
 *
20
 * foreach ($collection as $foo) {
21
 *     if ($foo instanceof \My\FooInterface) {
22
 *         // Do something with $foo
23
 *     }
24
 * }
25
 * ```
26
 *
27
 * It is preferable to subclass `AbstractCollection` to create your own typed
28
 * collections. For example:
29
 *
30
 * ``` php
31
 * namespace My;
32
 *
33
 * class FooCollection extends \Arrayy\Collection\AbstractCollection
34
 * {
35
 *     public function getType(): string
36
 *     {
37
 *         return FooInterface::class;
38
 *     }
39
 * }
40
 * ```
41
 *
42
 * And then use it similarly to the earlier example:
43
 *
44
 * ``` php
45
 * namespace My;
46
 *
47
 * $fooCollection = new \My\FooCollection();
48
 * $fooCollection->add(new \My\Foo());
49
 * $fooCollection->add(new \My\Foo());
50
 *
51
 * foreach ($fooCollection as $foo) {
52
 *     if ($foo instanceof \My\FooInterface) {
53
 *         // Do something with $foo
54
 *     }
55
 * }
56
 * ```
57
 *
58
 * INFO: this collection thingy is inspired by https://github.com/ramsey/collection/
59
 */
60
class Collection extends AbstractCollection
61
{
62
    /**
63
     * The type of elements stored in this collection.
64
     *
65
     * @var string
66
     */
67
    private $collectionTypeTmp;
68
69
    /**
70
     * Constructs a collection object of the specified type, optionally with the
71
     * specified data.
72
     *
73
     * @param string $type
74
     * @param mixed  $data
75
     *                     <p>
76
     *                     The initial items to store in the collection.
77
     *                     </p>
78
     */
79 3
    public function __construct(string $type, $data = [])
80
    {
81 3
        $this->collectionTypeTmp = $type;
82
83 3
        parent::__construct($data);
84 2
    }
85
86
    /**
87
     * The type (FQCN) associated with this collection.
88
     *
89
     * @return string
90
     */
91 3
    public function getType(): string
92
    {
93 3
        return $this->collectionTypeTmp;
94
    }
95
96
    /**
97
     * Get a base Collection instance from this Collection.
98
     *
99
     * @return self
100
     */
101 1
    public function toBase(): self
102
    {
103 1
        return new self($this);
104
    }
105
}
106