Completed
Push — master ( a35735...e1f184 )
by Dominik
01:48
created

LazyModelCollection::rewind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Chubbyphp\Model\Collection;
4
5
use Chubbyphp\Model\ModelInterface;
6
7
class LazyModelCollection implements ModelCollectionInterface
8
{
9
    /**
10
     * @var \Closure
11
     */
12
    private $resolver;
13
14
    /**
15
     * @var ModelInterface[]|array
16
     */
17
    private $initialModels;
18
19
    /**
20
     * @var ModelInterface[]|array
21
     */
22
    private $models;
23
24
    /**
25
     * @var ModelInterface[]|array
26
     */
27
    private $toRemoveModels;
28
29
    /**
30
     * ResolverCollection constructor.
31
     * @param \Closure $resolver
32
     */
33
    public function __construct(\Closure $resolver)
34
    {
35
        $this->resolver = $resolver;
36
    }
37
38
    private function loadModels()
39
    {
40
        if (null !== $this->models) {
41
            return;
42
        }
43
44
        $resolver = $this->resolver;
45
        $this->initialModels = (array) $resolver();
46
        $this->models = $this->initialModels;
47
        $this->toRemoveModels = [];
48
    }
49
50
    /**
51
     * @return ModelInterface
52
     */
53
    public function current()
54
    {
55
        $this->loadModels();
56
57
        return current($this->models);
58
    }
59
60
    /**
61
     * @return ModelInterface|false
62
     */
63
    public function next()
64
    {
65
        $this->loadModels();
66
67
        return next($this->models);
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function key()
74
    {
75
        $this->loadModels();
76
77
        return key($this->models);
78
    }
79
80
    /**
81
     * @return bool
82
     */
83
    public function valid()
84
    {
85
        $this->loadModels();
86
87
        return (bool) current($this->models);
88
    }
89
90
    public function rewind()
91
    {
92
        $this->loadModels();
93
94
        reset($this->models);
95
    }
96
97
    /**
98
     * @param ModelInterface $model
99
     * @return ModelCollectionInterface
100
     */
101
    public function add(ModelInterface $model): ModelCollectionInterface
102
    {
103
        $this->loadModels();
104
105
        $this->models[$model->getId()] = $model;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @param ModelInterface $model
112
     * @return ModelCollectionInterface
113
     */
114
    public function remove(ModelInterface $model): ModelCollectionInterface
115
    {
116
        $this->loadModels();
117
118
        if (isset($this->models[$model->getId()])) {
119
            unset($this->models[$model->getId()]);
120
        }
121
122
        if (isset($this->initialModels[$model->getId()])) {
123
            $this->toRemoveModels[$model->getId()] = $model;
124
        }
125
126
        return $this;
127
    }
128
129
    /**
130
     * @return ModelInterface[]|array
131
     */
132
    public function toPersist(): array
133
    {
134
        $this->loadModels();
135
136
        return $this->models;
137
    }
138
139
    /**
140
     * @return ModelInterface[]|array
141
     */
142
    public function toRemove(): array
143
    {
144
        $this->loadModels();
145
146
        return $this->toRemoveModels;
147
    }
148
149
    /**
150
     * @return array
151
     */
152
    public function jsonSerialize(): array
153
    {
154
        $this->loadModels();
155
156
        $serialzedModels = [];
157
        foreach ($this->models as $model) {
158
            $serialzedModels[] = $model->jsonSerialize();
159
        }
160
161
        return $serialzedModels;
162
    }
163
}
164