Completed
Push — 3.x ( e32815...7c4255 )
by Ryota
09:26 queued 05:13
created

AbstractCollection   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 49
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add() 0 6 1
A get() 0 8 2
A getIterator() 0 6 2
A count() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Polidog\Chatwork\Entity\Collection;
6
7
use Polidog\Chatwork\Entity\EntityInterface;
8
use Polidog\Chatwork\Exception\OutOfBoundsException;
9
10
abstract class AbstractCollection implements \IteratorAggregate, \Countable, CollectionInterface
11
{
12
    protected $entities = [];
13
14
    /**
15
     * @param array $entities
16
     */
17
    public function __construct(array $entities = [])
18
    {
19
        $this->entities = $entities;
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function add(EntityInterface $entity)
26
    {
27
        $this->entities[] = $entity;
28
29
        return $this;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function get($idx)
36
    {
37
        if (!array_key_exists($idx, $this->entities)) {
38
            throw new OutOfBoundsException('index not found, index:'.$idx);
39
        }
40
41
        return $this->entities[$idx];
42
    }
43
44
    /**
45
     * @return \ArrayIterator
46
     */
47
    public function getIterator()
48
    {
49
        foreach ($this->entities as $key => $entity) {
50
            yield $key => $entity;
51
        }
52
    }
53
54
    public function count()
55
    {
56
        return count($this->entities);
57
    }
58
}
59