Completed
Push — master ( 2b894b...836484 )
by Gaetano
05:34
created

AbstractCollection   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 0
dl 0
loc 81
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A append() 0 8 2
A exchangeArray() 0 10 3
A offsetSet() 0 8 2
A isValidElement() 0 4 1
A throwInvalid() 0 4 2
A __set_state() 0 4 1
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\API\Collection;
4
5
/**
6
 * Implements a 'typed array' structure
7
 */
8
class AbstractCollection extends \ArrayObject
9
{
10
    protected $allowedClass;
11
12
    /**
13
     * AbstractCollection constructor.
14
     * @param array $input
15
     * @param int $flags
16
     * @param string $iterator_class
17
     */
18
    public function __construct($input = array(), $flags = 0, $iterator_class = "ArrayIterator")
19
    {
20
        foreach ($input as $value) {
21
            if (!$this->isValidElement($value)) {
22
                $this->throwInvalid($value);
23
            }
24
        }
25
26
        parent::__construct($input, $flags, $iterator_class);
27
    }
28
29
    /**
30
     * @param mixed $value
31
     */
32
    public function append($value)
33
    {
34
        if (!$this->isValidElement($value)) {
35
            $this->throwInvalid($value);
36
        }
37
38
        parent::append($value);
39
    }
40
41
    /**
42
     * @param mixed $input
43
     * @return array the old array
44
     */
45
    public function exchangeArray($input)
46
    {
47
        foreach ($input as $value) {
48
            if (!$this->isValidElement($value)) {
49
                $this->throwInvalid($value);
50
            }
51
        }
52
53
        return parent::exchangeArray($input);
54
    }
55
56
    /**
57
     * @param mixed $index
58
     * @param mixed $value
59
     */
60
    public function offsetSet($index, $value)
61
    {
62
        if (!$this->isValidElement($value)) {
63
            $this->throwInvalid($value);
64
        }
65
66
        parent::offsetSet($index, $value);
67
    }
68
69
    protected function isValidElement($value)
70
    {
71
        return is_a($value, $this->allowedClass);
72
    }
73
74
    protected function throwInvalid($value)
75
    {
76
        throw new \InvalidArgumentException("Can not add element of type '" . (is_object($value) ? get_class($value) : gettype($value)) . "' to Collection of type '" . get_class($this) . "'");
77
    }
78
79
    /**
80
     * Allow the class to be serialized to php using var_export
81
     * @param array $data
82
     * @return static
83
     */
84
    public static function __set_state(array $data)
85
    {
86
        return new static($data);
87
    }
88
}
89