Passed
Push — master ( 61b2f8...584670 )
by Chubarov
05:03
created

ProductCollection::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Fns;
5
6
class ProductCollection implements \Countable, \Iterator, \ArrayAccess
7
{
8
    private $products;
9
    private $position;
10
11
    public function __construct(array $productCandidates = [])
12
    {
13
        foreach ($productCandidates as $candidate) {
14
            $this->offsetSet('', new Product(json_decode(json_encode($candidate), true)));
15
        }
16
    }
17
18
    public function count()
19
    {
20
        return count($this->products);
21
    }
22
23
    public function current()
24
    {
25
        return $this->products[$this->position];
26
    }
27
28
    public function next()
29
    {
30
        $this->position++;
31
    }
32
33
    public function key()
34
    {
35
        return $this->position;
36
    }
37
38
    public function rewind() : void
39
    {
40
        $this->position = 0;
41
    }
42
43
    public function valid()
44
    {
45
        return isset($this->products[$this->position]);
46
    }
47
48
    public function offsetExists($offset) : bool
49
    {
50
        return isset($this->products[$offset]);
51
    }
52
53
    public function offsetGet($offset) : mixed
54
    {
55
        return $this->products[$offset];
56
    }
57
58
    public function offsetSet($offset, $product)
59
    {
60
        if (empty($offset)) {
61
            $this->products[] = $product;
62
        } else {
63
            $this->products[$offset] = $product;
64
        }
65
    }
66
67
    public function offsetUnset($offset)
68
    {
69
        unset($this->products[$offset]);
70
    }
71
}
72