1: <?php declare(strict_types=1);
2:
3: namespace Salient\Contract\Sync;
4:
5: use Closure;
6: use IteratorAggregate;
7:
8: /**
9: * @template TEntity of SyncEntityInterface
10: *
11: * @extends IteratorAggregate<array-key,TEntity>
12: */
13: interface DeferredRelationshipInterface extends IteratorAggregate
14: {
15: /**
16: * Get the context within which the provider is servicing the entity
17: */
18: public function getContext(): ?SyncContextInterface;
19:
20: /**
21: * Resolve the deferred relationship from the provider or the local entity
22: * store
23: *
24: * Calling this method has the same effect as passing the resolved entities
25: * to {@see DeferredRelationshipInterface::replace()}.
26: *
27: * @return TEntity[]
28: */
29: public function resolve(): array;
30:
31: /**
32: * Resolve the deferred relationship with a list of entity instances
33: *
34: * If `$callback` was given when the relationship was deferred, it is called
35: * with the given entities, otherwise the variable or property given via
36: * `$replace` is replaced.
37: *
38: * Subsequent calls to {@see DeferredRelationshipInterface::resolve()}
39: * return the same instances.
40: *
41: * @param TEntity[] $entities
42: */
43: public function replace(array $entities): void;
44:
45: /**
46: * Defer retrieval of a sync entity relationship
47: *
48: * @param SyncProviderInterface $provider The provider servicing the entity.
49: * @param SyncContextInterface|null $context The context within which the
50: * provider is servicing the entity.
51: * @param class-string<TEntity> $entity The entity to instantiate.
52: * @param class-string<SyncEntityInterface> $forEntity The entity for which
53: * the relationship is deferred.
54: * @param string $forEntityProperty The entity property for which the
55: * relationship is deferred.
56: * @param int|string $forEntityId The identifier of the entity for which the
57: * relationship is deferred.
58: * @param array<string,mixed>|null $filter Overrides the default filter
59: * passed to the provider when requesting entities.
60: * @param TEntity[]|static|null $replace Refers to the variable or property
61: * to replace when the relationship is resolved.
62: * @param (Closure(TEntity[]): void)|null $callback If given, `$replace` is
63: * ignored and the resolved entities are passed to the callback.
64: */
65: public static function defer(
66: SyncProviderInterface $provider,
67: ?SyncContextInterface $context,
68: string $entity,
69: string $forEntity,
70: string $forEntityProperty,
71: $forEntityId,
72: ?array $filter = null,
73: &$replace = null,
74: ?Closure $callback = null
75: ): void;
76: }
77: