Collection   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getType() 0 4 1
1
<?php
2
/**
3
 * This file is part of the ramsey/collection library
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright Copyright (c) Ben Ramsey <[email protected]>
9
 * @license http://opensource.org/licenses/MIT MIT
10
 * @link https://benramsey.com/projects/ramsey-collection/ Documentation
11
 * @link https://packagist.org/packages/ramsey/collection Packagist
12
 * @link https://github.com/ramsey/collection GitHub
13
 */
14
15
namespace Ramsey\Collection;
16
17
/**
18
 * A collection represents a group of objects. Each object in the collection
19
 * is of a specific, defined type.
20
 *
21
 * This is a direct implementation of CollectionInterface, provided for
22
 * the sake of convenience.
23
 *
24
 * Example usage:
25
 *
26
 * ``` php
27
 * $collection = new \Ramsey\Collection\Collection('My\\Foo');
28
 * $collection->add(new \My\Foo());
29
 * $collection->add(new \My\Foo());
30
 *
31
 * foreach ($collection as $foo) {
32
 *     // Do something with $foo
33
 * }
34
 * ```
35
 *
36
 * It is preferable to subclass AbstractCollection to create your own typed
37
 * collections. For example:
38
 *
39
 * ``` php
40
 * namespace My\Foo;
41
 *
42
 * class FooCollection extends \Ramsey\Collection\AbstractCollection
43
 * {
44
 *     public function getType()
45
 *     {
46
 *         return 'My\\Foo';
47
 *     }
48
 * }
49
 * ```
50
 *
51
 * And then use it similarly to the earlier example:
52
 *
53
 * ``` php
54
 * $fooCollection = new \My\Foo\FooCollection();
55
 * $fooCollection->add(new \My\Foo());
56
 * $fooCollection->add(new \My\Foo());
57
 *
58
 * foreach ($fooCollection as $foo) {
59
 *     // Do something with $foo
60
 * }
61
 * ```
62
 *
63
 * The benefit with this approach is that you may do type-checking on the
64
 * collection object:
65
 *
66
 * ``` php
67
 * if ($collection instanceof \My\Foo\FooCollection) {
68
 *     // the collection is a collection of My\Foo objects
69
 * }
70
 * ```
71
 */
72
class Collection extends AbstractCollection
73
{
74
    /**
75
     * The type of elements stored in this collection
76
     *
77
     * A collection's type is immutable once it is set. For this reason, this
78
     * property is set private
79
     *
80
     * @var string
81
     */
82
    private $collectionType;
83
84
    /**
85
     * Constructs a collection object of the specified type,
86
     * optionally with the specified data
87
     *
88
     * @param string $collectionType
89
     * @param array $data
90
     */
91
    public function __construct($collectionType, array $data = [])
92
    {
93
        $this->collectionType = $collectionType;
94
        parent::__construct($data);
95
    }
96
97
    /**
98
     * Returns the type associated with this collection
99
     *
100
     * @return string
101
     */
102
    public function getType()
103
    {
104
        return $this->collectionType;
105
    }
106
}
107