Passed
Push — master ( 13fbc1...c5c3d4 )
by Anton
01:33
created

Entity   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 203
rs 10
c 0
b 0
f 0
wmc 22

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