Completed
Branch master (cbd196)
by Rémi
08:54
created

EmbeddedRelationship::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Analogue\ORM\Relationships;
4
5
use Analogue\ORM\System\Manager;
6
use Analogue\ORM\System\ResultBuilder;
7
use Analogue\ORM\System\Wrappers\Factory;
8
use Analogue\ORM\System\Wrappers\Wrapper;
9
10
abstract class EmbeddedRelationship
11
{
12
    /**
13
     * The class that embeds the current relation.
14
     *
15
     * @var string
16
     */
17
    protected $parentClass;
18
19
    /**
20
     * The class of the embedded relation.
21
     *
22
     * @var string
23
     */
24
    protected $relatedClass;
25
26
    /**
27
     * The relation attribute on the parent object.
28
     *
29
     * @var string
30
     */
31
    protected $relation;
32
33
    /**
34
     * If set to true, embedded Object's attributes will
35
     * be stored as a serialized array in a JSON Column.
36
     *
37
     * @var bool
38
     */
39
    protected $asArray = false;
40
41
    /**
42
     * Prefix on which the object's attributes are saved into
43
     * the parent's table. defaults to "<relatedClass>_".
44
     *
45
     * @var string
46
     */
47
    protected $prefix;
48
49
    /**
50
     * Attributes Map allow the calling EntityMap to overrides attributes
51
     * on the embedded relation.
52
     *
53
     * @var array
54
     */
55
    protected $columnMap = [];
56
57
    /**
58
     * Wrapper factory.
59
     *
60
     * @var \Analogue\ORM\System\Wrappers\Factory
61
     */
62
    protected $factory;
63
64
    public function __construct($parent, string $relatedClass, string $relation)
65
    {
66
        $this->parentClass = get_class($parent);
67
        $this->relatedClass = $relatedClass;
68
        $this->relation = $relation;
69
        $this->prefix = $relation.'_';
70
        $this->factory = new Factory();
71
    }
72
73
    /**
74
     * Switch the 'store as array' feature.
75
     *
76
     * @param bool $storeAsArray
77
     *
78
     * @return static
79
     */
80
    public function asArray(bool $storeAsArray = true)
81
    {
82
        $this->asArray = $storeAsArray;
83
84
        return $this;
85
    }
86
87
    /**
88
     * Set the column map for the embedded relation.
89
     *
90
     * @param array $columns
91
     *
92
     * @return static
93
     */
94
    public function setColumnMap(array $columns)
95
    {
96
        $this->columnMap = $columns;
97
98
        return $this;
99
    }
100
101
    /**
102
     * Set parent's attribute prefix.
103
     *
104
     * @param string $prefix
105
     *
106
     * @return static
107
     */
108
    public function setPrefix(string $prefix)
109
    {
110
        $this->prefix = $prefix;
111
112
        return $this;
113
    }
114
115
    /**
116
     * Return parent's attribute prefix.
117
     *
118
     * @return string
119
     */
120
    public function getPrefix() : string
121
    {
122
        return $this->prefix;
123
    }
124
125
    /**
126
     * Get the embedded object's attributes that will be
127
     * hydrated using parent's entity attributes.
128
     *
129
     * @return array
130
     */
131
    protected function getEmbeddedObjectAttributes() : array
132
    {
133
        $entityMap = $this->getRelatedMapper()->getEntityMap();
134
135
        $attributes = $entityMap->getAttributes();
136
        $properties = $entityMap->getProperties();
137
138
        return array_merge($attributes, $properties);
139
    }
140
141
    /**
142
     * Get the corresponding attribute on parent's attributes.
143
     *
144
     * @param string $key
145
     *
146
     * @return string
147
     */
148
    protected function getParentAttributeKey($key) : string
149
    {
150
        return $this->getPrefixedAttributeKey($this->getMappedParentAttribute($key));
151
    }
152
153
    /**
154
     * Get attribute name from the parent, if a map has been
155
     * defined.
156
     *
157
     * @param srring $attributeKey
0 ignored issues
show
Bug introduced by
There is no parameter named $attributeKey. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
158
     *
159
     * @return string
160
     */
161
    protected function getMappedParentAttribute(string $key) : string
162
    {
163
        if (array_key_exists($key, $this->columnMap)) {
164
            return $this->columnMap[$key];
165
        } else {
166
            return $key;
167
        }
168
    }
169
170
    /**
171
     * Return the name of the attribute with key.
172
     *
173
     * @param string $attributeKey
174
     *
175
     * @return string
176
     */
177
    protected function getPrefixedAttributeKey(string $attributeKey) : string
178
    {
179
        return $this->prefix.$attributeKey;
180
    }
181
182
    /**
183
     * Transform attributes into embedded object(s), and
184
     * match it into the given resultset.
185
     *
186
     * @return array
187
     */
188
    abstract public function match(array $results) : array;
189
190
    /**
191
     * Build an embedded object instance.
192
     *
193
     * @param array $attributes
194
     *
195
     * @return mixed
196
     */
197
    protected function buildEmbeddedObject(array $attributes)
198
    {
199
        $resultBuilder = new ResultBuilder($this->getRelatedMapper());
200
201
        // TODO : find a way to support eager load within an embedded
202
        // object.
203
        $eagerLoads = [];
204
205
        return $resultBuilder->build([$attributes], $eagerLoads)[0];
206
    }
207
208
    /**
209
     * Transform embedded object into db column(s).
210
     *
211
     * @param mixed $object
212
     *
213
     * @return array $columns
214
     */
215
    abstract public function normalize($object) : array;
216
217
    /**
218
     * Return parent mapper.
219
     *
220
     * @return Analogue\ORM\System\Mapper
221
     */
222
    protected function getParentMapper()
223
    {
224
        return Manager::getInstance()->mapper($this->parentClass);
225
    }
226
227
    /**
228
     * Return embedded relationship mapper.
229
     *
230
     * @return Analogue\ORM\System\Mapper
231
     */
232
    protected function getRelatedMapper()
233
    {
234
        return Manager::getInstance()->mapper($this->relatedClass);
235
    }
236
}
237