Completed
Push — master ( dfa02b...09a151 )
by Zoltán
04:46
created

AbstractCollection::isStrict()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php namespace BuildR\Collection\Collection;
2
3
use BuildR\Collection\Exception\CollectionException;
4
5
/**
6
 * Abstract collection implementation
7
 *
8
 * BuildR PHP Framework
9
 *
10
 * @author Zoltán Borsos <[email protected]>
11
 * @package Collection
12
 * @subpackage Collection
13
 *
14
 * @copyright    Copyright 2015, Zoltán Borsos.
15
 * @license      https://github.com/Zolli/BuildR/blob/master/LICENSE.md
16
 * @link         https://github.com/Zolli/BuildR
17
 */
18
abstract class AbstractCollection implements CollectionInterface {
19
20
    /**
21
     * @type array
22
     */
23
    protected $data = [];
24
25
    /**
26
     * @type NULL|callable
27
     */
28
    protected $typeChecker;
29
30
    /**
31
     * @type NULL|string
32
     */
33
    protected $typeCheckFailMessage;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function setStrictType(callable $typeCheck, $message = NULL) {
39
        $this->typeChecker = $typeCheck;
40
        $this->typeCheckFailMessage = $message;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function isStrict() {
47
        return is_callable($this->typeChecker);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 39
    public function toArray() {
54 39
        return (array) $this->data;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 2
    public function clear() {
61 2
        $this->data = [];
62 2
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    public function isEmpty() {
68 1
        return empty($this->data);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 59
    public function size() {
75 59
        return (is_array($this->data)) ? count($this->data) : 0;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     *
81
     * @codeCoverageIgnore
82
     */
83
    public function current() {
84
        return current($this->data);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     *
90
     * @codeCoverageIgnore
91
     */
92
    public function next() {
93
        return next($this->data);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     *
99
     * @codeCoverageIgnore
100
     */
101
    public function key() {
102
        return key($this->data);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     *
108
     * @codeCoverageIgnore
109
     */
110
    public function valid() {
111
        return key($this->data) !== NULL;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     *
117
     * @codeCoverageIgnore
118
     */
119
    public function rewind() {
120
        return reset($this->data);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 40
    public function count() {
127 40
        return $this->size();
128
    }
129
130
    /**
131
     * Executes the type check if the collection is strict. Always
132
     * returns true, when the collection is not strictly typed
133
     *
134
     * @param mixed $value
135
     *
136
     * @return bool
137
     *
138
     * @throws \BuildR\Collection\Exception\CollectionException
139
     */
140
    protected function doTypeCheck($value) {
141
        if($this->isStrict()) {
142
            $result = (bool) $this->typeChecker($value);
0 ignored issues
show
Bug introduced by
The method typeChecker() does not seem to exist on object<BuildR\Collection...ion\AbstractCollection>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
143
144
            if($result === FALSE) {
145
                $message = ($this->typeCheckFailMessage === NULL) ? gettype($value) : $this->typeCheckFailMessage;
146
147
                throw CollectionException::typeException($message);
148
            }
149
150
            return TRUE;
151
        }
152
    }
153
154
    /**
155
     * @param array|\BuildR\Collection\Collection\CollectionInterface $elements
156
     *
157
     * @return array
158
     */
159 36
    protected function collectionToArray($elements) {
160 36
        if($elements instanceof CollectionInterface) {
161 1
            $elements = $elements->toArray();
162 1
        }
163
164 36
        return $elements;
165
    }
166
167
}
168