Completed
Push — master ( 12bc5f...b06c34 )
by Dominik
01:52
created

src/Collection/ModelCollection.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Model\Collection;
6
7
use Chubbyphp\Model\ModelInterface;
8
9
final class ModelCollection implements ModelCollectionInterface
10
{
11
    /**
12
     * @var ModelInterface[]|array
13
     */
14
    private $initialModels;
15
16
    /**
17
     * @var ModelInterface[]|array
18
     */
19
    private $models;
20
21
    /**
22
     * @param ModelInterface[]|array $models
23
     */
24
    public function __construct(array $models = [])
25
    {
26
        $this->setModels($models);
27
        $this->initialModels = $this->models;
28
    }
29
30
    /**
31
     * @param ModelInterface $model
32
     *
33
     * @return ModelCollectionInterface
34
     */
35
    public function addModel(ModelInterface $model): ModelCollectionInterface
36
    {
37
        $this->models[$model->getId()] = $model;
38
39
        return $this;
40
    }
41
42
    /**
43
     * @param ModelInterface $model
44
     *
45
     * @return ModelCollectionInterface
46
     */
47 View Code Duplication
    public function removeModel(ModelInterface $model): ModelCollectionInterface
0 ignored issues
show
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...
48
    {
49
        if (isset($this->models[$model->getId()])) {
50
            unset($this->models[$model->getId()]);
51
        }
52
53
        return $this;
54
    }
55
56
    /**
57
     * @param ModelInterface[]|array $models
58
     *
59
     * @return ModelCollectionInterface
60
     */
61
    public function setModels(array $models): ModelCollectionInterface
62
    {
63
        $this->models = [];
64
        foreach ($models as $model) {
65
            $this->addModel($model);
66
        }
67
68
        return $this;
69
    }
70
71
    /**
72
     * @return ModelInterface[]|array
73
     */
74
    public function getModels(): array
75
    {
76
        return $this->models;
77
    }
78
79
    /**
80
     * @return ModelInterface[]|array
81
     */
82
    public function getInitialModels(): array
83
    {
84
        return $this->initialModels;
85
    }
86
87
    /**
88
     * @return \ArrayIterator
89
     */
90
    public function getIterator()
91
    {
92
        return new \ArrayIterator($this->models);
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    public function jsonSerialize(): array
99
    {
100
        $serializedModels = [];
101
        foreach ($this->models as $model) {
102
            $serializedModels[] = $model->jsonSerialize();
103
        }
104
105
        return $serializedModels;
106
    }
107
}
108