Completed
Push — master ( faa6bd...835ec7 )
by Jared
01:30
created

Collection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Pulsar;
4
5
use ArrayAccess;
6
use ArrayIterator;
7
use Countable;
8
use Exception;
9
use IteratorAggregate;
10
11
/**
12
 * Represents a collection of models.
13
 */
14
class Collection implements ArrayAccess, Countable, IteratorAggregate
15
{
16
    /**
17
     * @var Model[]
18
     */
19
    private $models;
20
21
    /**
22
     * @param Model[] $models
23
     */
24
    public function __construct(array $models)
25
    {
26
        $this->models = $models;
27
    }
28
29
    public function getIterator()
30
    {
31
        return new ArrayIterator($this->models);
32
    }
33
34
    public function offsetExists($offset)
35
    {
36
        return isset($this->models[$offset]);
37
    }
38
39
    public function offsetGet($offset)
40
    {
41
        return $this->models[$offset];
42
    }
43
44
    public function offsetSet($offset, $value)
45
    {
46
        // collections are immutable
47
        throw new Exception('Cannot perform set on immutable Collection');
48
    }
49
50
    public function offsetUnset($offset)
51
    {
52
        // collections are immutable
53
        throw new Exception('Cannot perform unset on immutable Collection');
54
    }
55
56
    public function count()
57
    {
58
        return count($this->models);
59
    }
60
}
61