Completed
Push — master ( b031f2...59e749 )
by Dominik
02:40
created

ModelCollection::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Model\Collection;
6
7
use Chubbyphp\Model\ModelInterface;
8
use Chubbyphp\Model\ModelSortTrait;
9
10
final class ModelCollection implements ModelCollectionInterface
11
{
12
    use ModelSortTrait;
13
14
    /**
15
     * @var string
16
     */
17
    private $modelClass;
18
19
    /**
20
     * @var string
21
     */
22
    private $foreignField;
23
24
    /**
25
     * @var string
26
     */
27
    private $foreignId;
28
29
    /**
30
     * @var array|null
31
     */
32
    private $orderBy;
33
34
    /**
35
     * @var ModelInterface[]|array
36
     */
37
    private $models;
38
39
    /**
40
     * @var \ReflectionProperty
41
     */
42
    private $propertyReflection;
43
44
    /**
45
     * @param string $modelClass
46
     * @param string $foreignField
47
     * @param string $foreignId
48
     * @param array|null $orderBy
49
     */
50 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
        string $modelClass,
52
        string $foreignField,
53
        string $foreignId,
54
        array $orderBy = null
55
    ) {
56
        $this->modelClass = $modelClass;
57
        $this->foreignField = $foreignField;
58
        $this->foreignId = $foreignId;
59
        $this->orderBy = $orderBy;
60
61
        $this->models = [];
62
63
        $this->propertyReflection = new \ReflectionProperty($this->modelClass, $this->foreignField);
64
        $this->propertyReflection->setAccessible(true);
65
    }
66
67
    /**
68
     * @param ModelInterface $model
69
     *
70
     * @return ModelCollectionInterface
71
     */
72 View Code Duplication
    public function addModel(ModelInterface $model): ModelCollectionInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        $this->propertyReflection->setValue($model, $this->foreignId);
75
76
        $this->models[$model->getId()] = $model;
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param ModelInterface $model
83
     *
84
     * @return ModelCollectionInterface
85
     */
86 View Code Duplication
    public function removeModel(ModelInterface $model): ModelCollectionInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88
        if (isset($this->models[$model->getId()])) {
89
            $this->propertyReflection->setValue($model, null);
90
91
            unset($this->models[$model->getId()]);
92
        }
93
94
        return $this;
95
    }
96
97
    /**
98
     * @param ModelInterface[]|array $models
99
     *
100
     * @return ModelCollectionInterface
101
     */
102
    public function setModels(array $models): ModelCollectionInterface
103
    {
104
        $this->models = [];
105
        foreach ($models as $model) {
106
            $this->addModel($model);
107
        }
108
109
        return $this;
110
    }
111
112
    /**
113
     * @return ModelInterface[]|array
114
     */
115
    public function getModels(): array
116
    {
117
        return $this->sort($this->modelClass, array_values($this->models), $this->orderBy);
118
    }
119
120
    /**
121
     * @return ModelInterface[]|array
122
     */
123
    public function getInitialModels(): array
124
    {
125
        return [];
126
    }
127
128
    /**
129
     * @return \ArrayIterator
130
     */
131
    public function getIterator()
132
    {
133
        return new \ArrayIterator($this->getModels());
134
    }
135
136
    /**
137
     * @return int
138
     */
139
    public function count()
140
    {
141
        return count($this->getModels());
142
    }
143
144
    /**
145
     * @return array
146
     */
147
    public function jsonSerialize(): array
148
    {
149
        $serializedModels = [];
150
        foreach ($this->getModels() as $model) {
151
            $serializedModels[] = $model->jsonSerialize();
152
        }
153
154
        return $serializedModels;
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    public function getForeignField(): string
161
    {
162
        return $this->foreignField;
163
    }
164
165
    /**
166
     * @return string
167
     */
168
    public function getForeignId(): string
169
    {
170
        return $this->foreignId;
171
    }
172
}
173