Completed
Push — master ( 99ca87...95904e )
by Andrey
23:44
created

Entity::getAssociations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/jms-serializer-module
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\JmsSerializerModule\DataContainer;
7
8
use Doctrine\Common\Inflector\Inflector;
9
10
/**
11
 * Class Entity
12
 *
13
 * @package Nnx\JmsSerializerModule\DataContainer
14
 */
15
class Entity implements EntityInterface
16
{
17
    /**
18
     * Внутренний идендификатор контейнера с данынми для фикстуры
19
     *
20
     * @var integer
21
     */
22
    protected $id;
23
24
    /**
25
     * Поле используемое для генерации id
26
     *
27
     * @var int
28
     */
29
    protected static $idGenerator = 0;
30
31
    /**
32
     * Уровень на котором расположенна сущность
33
     *
34
     * @var integer
35
     */
36
    protected $level;
37
38
    /**
39
     * Свойства сущности
40
     *
41
     * @var Property[]
42
     */
43
    protected $properties = [];
44
45
    /**
46
     * Связи с другими сущностями
47
     *
48
     * @var Association[]
49
     */
50
    protected $associations = [];
51
52
    /**
53
     * Родительская сущность
54
     *
55
     * @var EntityInterface|null
56
     */
57
    protected $parentEntity;
58
59
    /**
60
     * Возвращает уровень, на котором расположена сущность
61
     *
62
     * @return int
63
     */
64
    public function getLevel()
65
    {
66
        return $this->level;
67
    }
68
69
    /**
70
     * Устанавливает уровень на котором расположена сущность
71
     *
72
     * @param int $level
73
     *
74
     * @return $this
75
     */
76
    public function setLevel($level)
77
    {
78
        $this->level = $level;
79
80
        return $this;
81
    }
82
83
    /**
84
     * Возвращает родительскую сущность
85
     *
86
     * @return EntityInterface|null
87
     */
88
    public function getParentEntity()
89
    {
90
        return $this->parentEntity;
91
    }
92
93
    /**
94
     * Устанавливает родительскую сущность
95
     *
96
     * @param EntityInterface $parentEntity
97
     *
98
     * @return $this
99
     */
100
    public function setParentEntity(EntityInterface $parentEntity = null)
101
    {
102
        $this->parentEntity = $parentEntity;
103
104
        return $this;
105
    }
106
107
    /**
108
     * Добавляет связь на вложенную сущность
109
     *
110
     * @param Association $association
111
     *
112
     * @return $this
113
     */
114
    public function addAssociation(Association $association)
115
    {
116
        $this->associations[$association->getName()] = $association;
117
118
        return $this;
119
    }
120
121
    /**
122
     * Возвращает связи
123
     *
124
     * @return Association[]
125
     */
126
    public function getAssociations()
127
    {
128
        return $this->associations;
129
    }
130
131
    /**
132
     * Добавляет поле
133
     *
134
     * @param Property $property
135
     *
136
     * @return $this
137
     */
138
    public function addProperty(Property $property)
139
    {
140
        $normalizeName = Inflector::camelize($property->getName());
141
142
        $this->properties[$normalizeName] = $property;
143
144
        return $this;
145
    }
146
147
    /**
148
     * Возвращает набор полей сущности
149
     *
150
     * @return Property[]
151
     */
152
    public function getProperties()
153
    {
154
        return $this->properties;
155
    }
156
157
    /**
158
     * Определяет, есть ли связь, с заданным именем
159
     *
160
     * @param $name
161
     *
162
     * @return bool
163
     */
164
    public function hasAssociation($name)
165
    {
166
        return array_key_exists($name, $this->associations);
167
    }
168
169
170
    /**
171
     * Определяет, есть ли связь, с заданным именем
172
     *
173
     * @param $name
174
     *
175
     * @return Association
176
     * @throws \Nnx\JmsSerializerModule\DataContainer\Exception\InvalidArgumentException
177
     */
178
    public function getAssociation($name)
179
    {
180
        if (!array_key_exists($name, $this->associations)) {
181
            $errMsg = sprintf('Association %s not found', $name);
182
            throw new Exception\InvalidArgumentException($errMsg);
183
        }
184
185
        return $this->associations[$name];
186
    }
187
188
    /**
189
     * Возвращает внутренний идендификатор контейнера с данынми для фикстуры
190
     *
191
     * @return int
192
     */
193 View Code Duplication
    public function getId()
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...
194
    {
195
        if (null === $this->id) {
196
            static::$idGenerator += 1;
197
            $this->id = static::$idGenerator;
198
        }
199
        return $this->id;
200
    }
201
}
202