Passed
Push — master ( 09a6b1...7a7f8a )
by Anton
02:05
created

Entity   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 215
rs 10
c 0
b 0
f 0
wmc 23

19 Methods

Rating   Name   Duplication   Size   Complexity  
A setRole() 0 5 1
A setConstrain() 0 5 1
A setSource() 0 5 1
A getConstrain() 0 3 1
A merge() 0 11 5
A getFields() 0 3 1
A getClass() 0 3 1
A setSchema() 0 5 1
A setClass() 0 5 1
A getRole() 0 3 1
A getOptions() 0 3 1
A setMapper() 0 5 1
A getRepository() 0 3 1
A getSource() 0 3 1
A getRelations() 0 3 1
A getMapper() 0 3 1
A getSchema() 0 3 1
A setRepository() 0 5 1
A __construct() 0 5 1
1
<?php declare(strict_types=1);
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Cycle\Schema\Definition;
10
11
use Cycle\ORM\Mapper\Mapper;
12
use Cycle\ORM\Select\Repository;
13
use Cycle\ORM\Select\Source;
14
use Cycle\Schema\Definition\Map\FieldMap;
15
use Cycle\Schema\Definition\Map\OptionMap;
16
use Cycle\Schema\Definition\Map\RelationMap;
17
18
/**
19
 * Contains information about specific entity definition.
20
 */
21
final class Entity
22
{
23
    /** @var OptionMap */
24
    private $options;
25
26
    /** @var string */
27
    private $role;
28
29
    /** @var string|null */
30
    private $class;
31
32
    /** @var string|null */
33
    private $mapper;
34
35
    /** @var string|null */
36
    private $source;
37
38
    /** @var string|null */
39
    private $constrain;
40
41
    /** @var string|null */
42
    private $repository;
43
44
    /** @var FieldMap */
45
    private $fields;
46
47
    /** @var RelationMap */
48
    private $relations;
49
50
    /** @var array */
51
    private $schema = [];
52
53
    /**
54
     * Entity constructor.
55
     */
56
    public function __construct()
57
    {
58
        $this->options = new OptionMap();
59
        $this->fields = new FieldMap();
60
        $this->relations = new RelationMap();
61
    }
62
63
    /**
64
     * @return OptionMap
65
     */
66
    public function getOptions(): OptionMap
67
    {
68
        return $this->options;
69
    }
70
71
    /**
72
     * @param string $role
73
     * @return Entity
74
     */
75
    public function setRole(string $role): self
76
    {
77
        $this->role = $role;
78
79
        return $this;
80
    }
81
82
    /**
83
     * @return string|null
84
     */
85
    public function getRole(): ?string
86
    {
87
        return $this->role;
88
    }
89
90
    /***
91
     * @param string $class
92
     * @return Entity
93
     */
94
    public function setClass(string $class): self
95
    {
96
        $this->class = $class;
97
98
        return $this;
99
    }
100
101
    /**
102
     * @return string|null
103
     */
104
    public function getClass(): ?string
105
    {
106
        return $this->class;
107
    }
108
109
    /**
110
     * @param string|null $mapper
111
     * @return Entity
112
     */
113
    public function setMapper(?string $mapper): self
114
    {
115
        $this->mapper = $mapper;
116
117
        return $this;
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public function getMapper(): string
124
    {
125
        return $this->mapper ?? Mapper::class;
126
    }
127
128
    /**
129
     * @param string|null $source
130
     * @return Entity
131
     */
132
    public function setSource(?string $source): self
133
    {
134
        $this->source = $source;
135
136
        return $this;
137
    }
138
139
    /**
140
     * @return string
141
     */
142
    public function getSource(): string
143
    {
144
        return $this->source ?? Source::class;
145
    }
146
147
    /**
148
     * @param string|null $constrain
149
     * @return Entity
150
     */
151
    public function setConstrain(?string $constrain): self
152
    {
153
        $this->constrain = $constrain;
154
155
        return $this;
156
    }
157
158
    /**
159
     * @return string|null
160
     */
161
    public function getConstrain(): ?string
162
    {
163
        return $this->constrain;
164
    }
165
166
    /**
167
     * @param string|null $repository
168
     * @return Entity
169
     */
170
    public function setRepository(?string $repository): self
171
    {
172
        $this->repository = $repository;
173
174
        return $this;
175
    }
176
177
    /**
178
     * @return string
179
     */
180
    public function getRepository(): string
181
    {
182
        return $this->repository ?? Repository::class;
183
    }
184
185
    /**
186
     * @return FieldMap
187
     */
188
    public function getFields(): FieldMap
189
    {
190
        return $this->fields;
191
    }
192
193
    /**
194
     * @return RelationMap
195
     */
196
    public function getRelations(): RelationMap
197
    {
198
        return $this->relations;
199
    }
200
201
    /**
202
     * @param array $schema
203
     * @return Entity
204
     */
205
    public function setSchema(array $schema): Entity
206
    {
207
        $this->schema = $schema;
208
209
        return $this;
210
    }
211
212
    /**
213
     * @return array
214
     */
215
    public function getSchema(): array
216
    {
217
        return $this->schema;
218
    }
219
220
    /**
221
     * Merge entity relations and fields.
222
     *
223
     * @param Entity $entity
224
     */
225
    public function merge(Entity $entity)
226
    {
227
        foreach ($entity->getRelations() as $name => $relation) {
228
            if (!$this->relations->has($name)) {
229
                $this->relations->set($name, $relation);
230
            }
231
        }
232
233
        foreach ($entity->getFields() as $name => $field) {
234
            if (!$this->fields->has($name)) {
235
                $this->fields->set($name, $field);
236
            }
237
        }
238
    }
239
}