Completed
Push — master ( 50173f...31cc74 )
by Dominik
01:46
created

ModelCollection::getModels()   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
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
     * @return ModelCollectionInterface
33
     */
34
    public function addModel(ModelInterface $model): ModelCollectionInterface
35
    {
36
        $this->models[$model->getId()] = $model;
37
38
        return $this;
39
    }
40
41
    /**
42
     * @param ModelInterface $model
43
     * @return ModelCollectionInterface
44
     */
45 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...
46
    {
47
        if (isset($this->models[$model->getId()])) {
48
            unset($this->models[$model->getId()]);
49
        }
50
51
        return $this;
52
    }
53
54
    /**
55
     * @param ModelInterface[]|array $models
56
     * @return ModelCollectionInterface
57
     */
58
    public function setModels(array $models): ModelCollectionInterface
59
    {
60
        $this->models = [];
61
        foreach ($models as $model) {
62
            $this->addModel($model);
63
        }
64
65
        return $this;
66
    }
67
68
    /**
69
     * @return ModelInterface[]|array
70
     */
71
    public function getModels(): array
72
    {
73
        return $this->models;
74
    }
75
76
    /**
77
     * @return ModelInterface[]|array
78
     */
79
    public function getInitialModels(): array
80
    {
81
        return $this->initialModels;
82
    }
83
84
    /**
85
     * @return array
86
     */
87
    public function jsonSerialize(): array
88
    {
89
        $serializedModels = [];
90
        foreach ($this->models as $model) {
91
            $serializedModels[] = $model->jsonSerialize();
92
        }
93
94
        return $serializedModels;
95
    }
96
}
97