Completed
Push — master ( 4d9ca3...1d0b3b )
by Peter
22:33
created

BaseCollection::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Foo\Grid\Collection;
6
7
use ArrayAccess;
8
use Countable;
9
use Foo\Grid\Component\Component;
10
use Foo\Grid\Component\IComponent;
11
use Foo\Grid\Helper\StringHelper;
12
use Foo\Translate\ITranslator;
13
use InvalidArgumentException;
14
use Iterator;
15
use LogicException;
16
17
class BaseCollection extends Component implements ArrayAccess, Countable, Iterator, IComponent
18
{
19
    const ERROR_INVALID_TYPE_ARG     = 'Provided value must be an object instance of "%s", type "%s" is found';
20
    const ERROR_INVALID_INSTANCE_ARG = 'Provided value must be an instance of "%s", not an instance of "%s"';
21
    const ERROR_INVALID_TYPE_RETURN  = 'Retrieved value is not an instance of "%s"';
22
23
    /** @var int */
24
    protected $position = 0;
25
26
    /** @var IComponent[] */
27
    protected $components = [];
28
29
    /** @var null|string */
30
    protected $tag = null;
31
32
    /** @var array */
33
    protected $attributes = [];
34
35
    /** @var string */
36
    protected $indentation = '';
37
38
    /**
39
     * Collection constructor.
40
     *
41
     * @param string|null $tag
42
     * @param array       $attributes
43
     */
44 38
    public function __construct(string $tag = null, $attributes = [])
45
    {
46 38
        $this->position = 0;
47
48 38
        parent::__construct('', $tag, $attributes);
49 38
    }
50
51
    public function rewind()
52
    {
53
        $this->position = 0;
54
    }
55
56
    /**
57
     * @return object
58
     */
59
    public function current()
60
    {
61
        $component = $this->components[$this->position];
62
63
        return $component;
64
    }
65
66
    /**
67
     * @return int
68
     */
69
    public function key()
70
    {
71
        return $this->position;
72
    }
73
74
    public function next()
75
    {
76
        ++$this->position;
77
    }
78
79
    /**
80
     * @return bool
81
     */
82
    public function valid()
83
    {
84
        return isset($this->components[$this->position]);
85
    }
86
87
    /**
88
     * @param int|null $offset
89
     * @param object   $value
90
     */
91 15
    public function offsetSet($offset, $value)
92
    {
93 15
        if (is_null($offset)) {
94 15
            $this->components[] = $value;
95
        } else {
96
            $this->components[$offset] = $value;
97
        }
98 15
    }
99
100
    /**
101
     * @param int $offset
102
     *
103
     * @return bool
104
     */
105
    public function offsetExists($offset)
106
    {
107
        return isset($this->components[$offset]);
108
    }
109
110
    /**
111
     * @param int $offset
112
     */
113
    public function offsetUnset($offset)
114
    {
115
        unset($this->components[$offset]);
116
    }
117
118
    /**
119
     * @param int $offset
120
     *
121
     * @return object|null
122
     */
123
    public function offsetGet($offset)
124
    {
125
        return isset($this->components[$offset]) ? $this->components[$offset] : null;
126
    }
127
128
    /**
129
     * @return int
130
     */
131
    public function count()
132
    {
133
        return count($this->components);
134
    }
135
136
    /**
137
     * @param int    $num
138
     * @param string $whitespace
139
     */
140
    public function setIndentation(int $num, string $whitespace = '    ')
141
    {
142
        foreach ($this->components as $component) {
143
            $component->setIndentation($num + 1, $whitespace);
144
        }
145
146
        $this->indentation = str_repeat($num, $whitespace);
147
    }
148
149
    /**
150
     * @param ITranslator $translator
151
     */
152
    public function setTranslator(ITranslator $translator)
153
    {
154
        foreach ($this->components as $component) {
155
            $component->setTranslator($translator);
156
        }
157
    }
158
159
    /**
160
     * @return string
161
     */
162 15
    public function __toString(): string
163
    {
164 15
        $list = [];
165 15
        foreach ($this->components as $stringer) {
166 15
            $list[] = (string)$stringer;
167
        }
168
169 15
        $content = implode("\n" . $this->indentation, $list);
170
171 15
        $result = StringHelper::wrapInTag($content, $this->tag, $this->attributes);
172
173 15
        return $result;
174
    }
175
176
    /**
177
     * @param object $object
178
     * @param string $className
179
     *
180
     * @throws InvalidArgumentException
181
     */
182 18
    protected function verifyArgument($object, $className)
183
    {
184 18
        if ($object instanceof $className) {
185 13
            return;
186
        }
187
188 5
        $type = gettype($object);
189 5
        if (gettype($object) !== 'object') {
190 5
            throw new InvalidArgumentException(sprintf(static::ERROR_INVALID_TYPE_ARG, $className, $type));
191
        }
192
193
194
        throw new InvalidArgumentException(
195
            sprintf(static::ERROR_INVALID_INSTANCE_ARG, $className, get_class($object))
196
        );
197
    }
198
199
    /**
200
     * @param object $object
201
     * @param string $className
202
     *
203
     * @throws LogicException
204
     */
205
    protected function verifyReturn($object, $className)
206
    {
207
        if ($object instanceof $className) {
208
            return;
209
        }
210
211
        throw new LogicException(sprintf(static::ERROR_INVALID_TYPE_RETURN, $className));
212
    }
213
}
214