Issues (590)

src/Relations/RelationInterface.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Bdf\Prime\Relations;
4
5
use Bdf\Prime\Collection\Indexer\EntityIndexerInterface;
6
use Bdf\Prime\Exception\PrimeException;
7
use Bdf\Prime\Query\Contract\EntityJoinable;
8
use Bdf\Prime\Query\Contract\ReadOperation;
9
use Bdf\Prime\Query\Contract\WriteOperation;
10
use Bdf\Prime\Query\ReadCommandInterface;
11
use Bdf\Prime\Repository\RepositoryInterface;
12
13
/**
14
 * RelationInterface
15
 *
16
 * @template L as object
17
 * @template R as object
18
 */
19
interface RelationInterface
20
{
21
    public const BELONGS_TO        = 'belongsTo';
22
    public const HAS_ONE           = 'hasOne';
23
    public const HAS_MANY          = 'hasMany';
24
    public const BELONGS_TO_MANY   = 'belongsToMany';
25
    public const BY_INHERITANCE    = 'byInheritance';
26
    public const MORPH_TO          = 'morphTo';
27
    public const CUSTOM            = 'custom';
28
//    const MORPH_TO_MANY     = 'morphToMany';
29
30
    /**
31
     * Get the relation repository.
32
     * For standard relation, this is the distant repository (i.e. related entity)
33
     * For polymorphic relation, this is the local repository (i.e. declarer)
34
     *
35
     * @return RepositoryInterface<R>
36
     */
37
    public function relationRepository(): RepositoryInterface;
38
39
    /**
40
     * Get the local (owner) repository
41
     *
42
     * @return RepositoryInterface<L>
43
     */
44
    public function localRepository(): RepositoryInterface;
45
46
    /**
47
     * Set the alias for to use for the joined table
48
     *
49
     * @param string|null $localAlias
50
     *
51
     * @return $this
52
     */
53
    public function setLocalAlias(?string $localAlias);
54
55
    /**
56
     * Set the relation options
57
     *
58
     * @param array $options
59
     *
60
     * @return $this
61
     */
62
    public function setOptions(array $options);
63
64
    /**
65
     * Load relation and inject into given entities
66
     *
67
     * Ex:
68
     * <code>
69
     * $users = EntityIndexer::formArray($mapper, User::all());
70
     * $relation = User::relation('customer');
71
     *
72
     * $relation->load($users); // Simply load the customer into users
73
     * $relation->load($users, ['packs.pack', 'parent']); // Load customer, customer packs, packs and parent customer
74
     * $relation->load($users, [], ['name :not' => 'John']); // Load only customers which as not the name 'John'
75
     * $relation->load($users, [], [], ['packs']); // Do not load packs (if marked as eager)
76
     * </code>
77
     *
78
     * @param EntityIndexerInterface<L> $collection Relation owners entities
79
     * @param string[] $with The distant relations to load. The array is in form : [ 'subrelation', 'other.subsubrelation', ... ]
80
     * @param mixed $constraints The distant constraints. Should be a criteria array, using relation attributes
81
     * @param string[] $without The distant relations to unload (in case of eager load). Format is same as $with, expects that only leaf relation are unloaded
82
     *
83
     * @return void
84
     * @throws PrimeException
85
     */
86
    #[ReadOperation]
87
    public function load(EntityIndexerInterface $collection, array $with = [], $constraints = [], array $without = []): void;
88
89
    /**
90
     * Load relation if not yet loaded
91
     *
92
     * - If the relation is already loaded, but sub-relations is requested ($with parameter), only sub-relations will be loaded
93
     * - If any constraints is found, the loading is forced
94
     * - If at least one of the relations is not loaded, all relations will be loaded
95
     *
96
     * Ex:
97
     * <code>
98
     * $users = EntityIndexer::formArray($mapper, User::all());
99
     * $relation = User::relation('customer');
100
     *
101
     * $relation->loadIfNotLoaded($users); // Simply load the customer into users
102
     * $relation->loadIfNotLoaded($users); // The second call will do nothing
103
     * $relation->loadIfNotLoaded($users, ['packs']); // Loading customers packs into customers, but customers will not be reloaded
104
     * </code>
105
     *
106
     * @param EntityIndexerInterface<L> $collection Relation owners entities
107
     * @param string[] $with The distant relations to load. The array is in form : [ 'subrelation', 'other.subsubrelation', ... ]
108
     * @param mixed $constraints The distant constraints. Should be a criteria array, using relation attributes
109
     * @param string[] $without The distant relations to unload (in case of eager load). Format is same as $with, expects that only leaf relation are unloaded
110
     *
111
     * @return void
112
     * @throws PrimeException
113
     *
114
     * @see RelationInterface::load() The base loading method
115
     */
116
    #[ReadOperation]
117
    public function loadIfNotLoaded(EntityIndexerInterface $collection, array $with = [], $constraints = [], array $without = []): void;
118
119
    /**
120
     * Get the distant query linked to the entity
121
     * The result query can be used to requests related entities
122
     *
123
     * @param L|L[] $owner The relation owner, or collection of owners
124
     *
125
     * @return ReadCommandInterface<\Bdf\Prime\Connection\ConnectionInterface, R>
126
     */
127
    public function link($owner): ReadCommandInterface;
128
129
    /**
130
     * Add join expression on query builder
131
     *
132
     * @param EntityJoinable&ReadCommandInterface $query
133
     * @param string $alias
134
     */
135
    public function join(EntityJoinable $query, string $alias): void;
136
137
    /**
138
     * Get the repositories to register for a JOIN query
139
     *
140
     * @see RelationInterface::join()
141
     * @see AliasResolver::registerMetadata()
142
     *
143
     * @param EntityJoinable $query
144
     * @param string $alias
145
     * @param string|int|null $discriminator
146
     *
147
     * @return array<string, RepositoryInterface> Repositories, indexed by alias
148
     */
149
    public function joinRepositories(EntityJoinable $query, string $alias, $discriminator = null): array;
150
151
    /**
152
     * Associate an entity to the owner entity
153
     *
154
     * This method will set the foreign key value on the owner entity and attach the entity
155
     * If the relation is detached, only the foreign key will be updated
156
     *
157
     * Note: This method will not perform any write operation on database :
158
     *       the related entity id must be generated before, and the owner must be updated manually
159
     *
160
     * <code>
161
     * $relation->associate($entity, $relatedEntity);
162
     * $entity->getRelatedEntity() === $relatedEntity; // should be true
163
     * </code>
164
     *
165
     * Only foreign key barrier can associate an entity
166
     *
167
     * @param L $owner  The relation owner
0 ignored issues
show
The type Bdf\Prime\Relations\L was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
168
     * @param R $entity The related entity data
169
     *
170
     * @return L Returns the owner entity instance
171
     *
172
     * @throws \InvalidArgumentException If the owner is not a foreign key barrier
173
     */
174
    public function associate($owner, $entity);
175
176
    /**
177
     * Remove the relation from owner entity
178
     * This is the reverse operation of associate : will detach the related entity and set the foreign key to null
179
     *
180
     * Note: This method will not perform any write operation on database :
181
     *       the related entity id must be generated before, and the owner must be updated manually
182
     *
183
     * Only foreign key barrier can dissociate an entity
184
     *
185
     * @param L $owner  The relation owner
186
     *
187
     * @return L Returns the owner entity instance
188
     *
189
     * @throws \InvalidArgumentException If the owner is not a foreign key barrier
190
     */
191
    public function dissociate($owner);
192
193
    /**
194
     * Add a relation entity on the given entity owner
195
     * This method will set the foreign key value on the related entity but not attach the related entity to the owner
196
     *
197
     * Only non foreign key barrier can add an entity
198
     *
199
     * <code>
200
     * $relation->add($entity, $related);
201
     *
202
     * $related->getOwnerId() === $entity->id(); // Should be true
203
     * </code>
204
     *
205
     * @param L $owner
206
     * @param R $related
207
     *
208
     * @return int
209
     *
210
     * @throws \InvalidArgumentException     If the owner is the foreign key barrier
211
     * @throws PrimeException When cannot save entity
212
     */
213
    #[WriteOperation]
214
    public function add($owner, $related): int;
215
216
    /**
217
     * Create the relation entity and set its foreign key value
218
     * This method will not attach the created entity to the owner
219
     *
220
     * Note: This method will not perform any write operation on database, it will only instantiate the entity
221
     *
222
     * <code>
223
     * $related = $relation->create($entity);
224
     * $related->getOwnerId() === $entity->id(); // Should be true
225
     * </code>
226
     *
227
     * Only non foreign key barrier can create an entity
228
     *
229
     * @param L $owner The relation owner
230
     * @param array $data The related entity data
231
     *
232
     * @return R Returns the related entity instance
233
     *
234
     * @throws \InvalidArgumentException If the owner is the foreign key barrier
235
     */
236
    public function create($owner, array $data = []);
237
238
    /**
239
     * Save the relation from an entity
240
     *
241
     * Note: This method can only works with attached entities
242
     *
243
     * @param L $owner
244
     * @param array $relations sub-relation names to save
245
     *
246
     * @return int Number of updated / inserted entities
247
     * @throws PrimeException When cannot save entity
248
     */
249
    #[WriteOperation]
250
    public function saveAll($owner, array $relations = []): int;
251
252
    /**
253
     * Remove the relation from an entity
254
     *
255
     * Note: This method can only works with attached entities
256
     *
257
     * @param L $owner
258
     * @param array $relations sub-relation names to delete
259
     *
260
     * @return int Number of deleted entities
261
     * @throws PrimeException When cannot delete entity
262
     */
263
    #[WriteOperation]
264
    public function deleteAll($owner, array $relations = []): int;
265
266
    /**
267
     * Check if the relation is loaded into the given entity
268
     *
269
     * @param L $entity
270
     *
271
     * @return boolean
272
     */
273
    public function isLoaded($entity): bool;
274
275
    /**
276
     * Clear relation entity data of the entity
277
     *
278
     * @param L $entity
279
     *
280
     * @internal Will be removed in 3.0
281
     */
282
    public function clearInfo($entity): void;
283
}
284