summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/orm/src/Relation.php
blob: 19593636a289ce5f0fc4d2d8f3d8f66d67a75b0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php

namespace ipl\Orm;

use function ipl\Stdlib\get_php_type;

/**
 * Relations represent the connection between models, i.e. the association between rows in one or more tables
 * on the basis of matching key columns. The relationships are defined using candidate key-foreign key constructs.
 */
class Relation
{
    /** @var string Name of the relation */
    protected $name;

    /** @var Model Source model */
    protected $source;

    /** @var string|array Column name(s) of the foreign key found in the target table */
    protected $foreignKey;

    /** @var string|array Column name(s) of the candidate key in the source table which references the foreign key */
    protected $candidateKey;

    /** @var string Target model class */
    protected $targetClass;

    /** @var Model Target model */
    protected $target;

    /** @var string Type of the JOIN used in the query */
    protected $joinType = 'INNER';

    /** @var bool Whether this is the inverse of a relationship */
    protected $inverse;

    /** @var bool Whether this is a to-one relationship */
    protected $isOne = true;

    /**
     * Get the default column name(s) in the source table used to match the foreign key
     *
     * The default candidate key is the primary key column name(s) of the given model.
     *
     * @param Model $source
     *
     * @return array
     */
    public static function getDefaultCandidateKey(Model $source)
    {
        return (array) $source->getKeyName();
    }

    /**
     * Get the default column name(s) of the foreign key found in the target table
     *
     * The default foreign key is the given model's primary key column name(s) prefixed with its table name.
     *
     * @param Model $source
     *
     * @return array
     */
    public static function getDefaultForeignKey(Model $source)
    {
        $tableName = $source->getTableName();

        return array_map(
            function ($key) use ($tableName) {
                return "{$tableName}_{$key}";
            },
            (array) $source->getKeyName()
        );
    }

    /**
     * Get whether this is a to-one relationship
     *
     * @return bool
     */
    public function isOne()
    {
        return $this->isOne;
    }

    /**
     * Get the name of the relation
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set the name of the relation
     *
     * @param string $name
     *
     * @return $this
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get the source model of the relation
     *
     * @return Model
     */
    public function getSource()
    {
        return $this->source;
    }

    /**
     * Set the source model of the relation
     *
     * @param Model $source
     *
     * @return $this
     */
    public function setSource(Model $source)
    {
        $this->source = $source;

        return $this;
    }

    /**
     * Get the column name(s) of the foreign key found in the target table
     *
     * @return string|array Array if the foreign key is compound, string otherwise
     */
    public function getForeignKey()
    {
        return $this->foreignKey;
    }

    /**
     * Set the column name(s) of the foreign key found in the target table
     *
     * @param string|array $foreignKey Array if the foreign key is compound, string otherwise
     *
     * @return $this
     */
    public function setForeignKey($foreignKey)
    {
        $this->foreignKey = $foreignKey;

        return $this;
    }

    /**
     * Get the column name(s) of the candidate key in the source table which references the foreign key
     *
     * @return string|array Array if the candidate key is compound, string otherwise
     */
    public function getCandidateKey()
    {
        return $this->candidateKey;
    }

    /**
     * Set the column name(s) of the candidate key in the source table which references the foreign key
     *
     * @param string|array $candidateKey Array if the candidate key is compound, string otherwise
     *
     * @return $this
     */
    public function setCandidateKey($candidateKey)
    {
        $this->candidateKey = $candidateKey;

        return $this;
    }

    /**
     * Get the target model class
     *
     * @return string
     */
    public function getTargetClass()
    {
        return $this->targetClass;
    }

    /**
     * Set the target model class
     *
     * @param string $targetClass
     *
     * @return $this
     *
     * @throws \InvalidArgumentException If the target model class is not of type string
     */
    public function setTargetClass($targetClass)
    {
        if (! is_string($targetClass)) {
            // Require a class name here instead of a concrete model in oder to prevent circular references when
            // constructing relations
            throw new \InvalidArgumentException(sprintf(
                '%s() expects parameter 1 to be string, %s given',
                __METHOD__,
                get_php_type($targetClass)
            ));
        }

        $this->targetClass = $targetClass;

        return $this;
    }

    /**
     * Get the target model
     *
     * Returns the model from {@link setTarget()} or an instance of {@link getTargetClass()}.
     * Note that multiple calls to this method always returns the very same model instance.
     *
     * @return Model
     */
    public function getTarget()
    {
        if ($this->target === null) {
            $targetClass = $this->getTargetClass();
            $this->target = new $targetClass();
        }

        return $this->target;
    }

    /**
     * Set the the target model
     *
     * @param Model $target
     *
     * @return $this
     */
    public function setTarget(Model $target)
    {
        $this->target = $target;

        return $this;
    }

    /**
     * Get the type of the JOIN used in the query
     *
     * @return string
     */
    public function getJoinType()
    {
        return $this->joinType;
    }

    /**
     * Set the type of the JOIN used in the query
     *
     * @param string $joinType
     *
     * @return Relation
     */
    public function setJoinType($joinType)
    {
        $this->joinType = $joinType;

        return $this;
    }

    /**
     * Determine the candidate key-foreign key construct of the relation
     *
     * @param Model $source
     *
     * @return array Candidate key-foreign key column name pairs
     *
     * @throws \UnexpectedValueException If there's no candidate key to be found
     *                                   or the foreign key count does not match the candidate key count
     */
    public function determineKeys(Model $source)
    {
        $candidateKey = (array) $this->getCandidateKey();

        if (empty($candidateKey)) {
            $candidateKey = $this->inverse
                ? static::getDefaultForeignKey($this->getTarget())
                : static::getDefaultCandidateKey($source);
        }

        if (empty($candidateKey)) {
            throw new \UnexpectedValueException(sprintf(
                "Can't join relation '%s' in model '%s'. No candidate key found.",
                $this->getName(),
                get_class($source)
            ));
        }

        $foreignKey = (array) $this->getForeignKey();

        if (empty($foreignKey)) {
            $foreignKey = $this->inverse
                ? static::getDefaultCandidateKey($this->getTarget())
                : static::getDefaultForeignKey($source);
        }

        if (count($foreignKey) !== count($candidateKey)) {
            throw new \UnexpectedValueException(sprintf(
                "Can't join relation '%s' in model '%s'."
                . " Foreign key count (%s) does not match candidate key count (%s).",
                $this->getName(),
                get_class($source),
                implode(', ', $foreignKey),
                implode(', ', $candidateKey)
            ));
        }

        return array_combine($foreignKey, $candidateKey);
    }

    /**
     * Resolve the relation
     *
     * Yields a three-element array consisting of the source model, target model and the join keys.
     *
     * @return \Generator
     */
    public function resolve()
    {
        $source = $this->getSource();

        yield [$source, $this->getTarget(), $this->determineKeys($source)];
    }
}