Passed
Pull Request — master (#11)
by Carlos C
01:36
created

AbstractCollection::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\Finkok\Services;
6
7
use ArrayIterator;
8
use ArrayObject;
9
use Countable;
10
use IteratorAggregate;
11
use stdClass;
12
13
abstract class AbstractCollection implements Countable, IteratorAggregate
14
{
15
    abstract protected function createItemFromStdClass(stdClass $content): object;
16
17
    /** @var ArrayObject */
18
    protected $collection;
19
20 36
    public function __construct(array $collection)
21
    {
22 36
        $this->collection = new ArrayObject();
23 36
        foreach ($collection as $content) {
24 31
            $this->collection->append($this->createItemFromStdClass($content));
25
        }
26 36
    }
27
28 11
    public function get(int $index): object
29
    {
30 11
        if (! isset($this->collection[$index])) {
31 2
            return $this->createItemFromStdClass((object) []);
32
        }
33 9
        return $this->collection[$index];
34
    }
35
36 18
    public function count(): int
37
    {
38 18
        return $this->collection->count();
39
    }
40
41 5
    public function first(): object
42
    {
43 5
        return $this->get(0);
44
    }
45
46 8
    public function getIterator(): ArrayIterator
47
    {
48 8
        return $this->collection->getIterator();
49
    }
50
}
51