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: * @template TReplace of TEntity[]|static|null
49: *
50: * @param SyncProviderInterface $provider The provider servicing the entity.
51: * @param SyncContextInterface|null $context The context within which the
52: * provider is servicing the entity.
53: * @param class-string<TEntity> $entity The entity to instantiate.
54: * @param class-string<SyncEntityInterface> $forEntity The entity for which
55: * the relationship is deferred.
56: * @param string $forEntityProperty The entity property for which the
57: * relationship is deferred.
58: * @param int|string $forEntityId The identifier of the entity for which the
59: * relationship is deferred.
60: * @param array<string,mixed>|null $filter Overrides the default filter
61: * passed to the provider when requesting entities.
62: * @param TReplace $replace Refers to the variable or property to replace
63: * when the relationship is resolved.
64: * @param (Closure(TEntity[]): void)|null $callback If given, `$replace` is
65: * ignored and the resolved entities are passed to the callback.
66: * @param-out ($callback is null ? TEntity[]|static : TReplace) $replace
67: */
68: public static function defer(
69: SyncProviderInterface $provider,
70: ?SyncContextInterface $context,
71: string $entity,
72: string $forEntity,
73: string $forEntityProperty,
74: $forEntityId,
75: ?array $filter = null,
76: &$replace = null,
77: ?Closure $callback = null
78: ): void;
79: }
80: