1: <?php declare(strict_types=1);
2:
3: namespace Salient\Contract\Sync;
4:
5: use Closure;
6: use LogicException;
7:
8: /**
9: * @template TEntity of SyncEntityInterface
10: */
11: interface DeferredEntityInterface
12: {
13: /**
14: * Get the context within which the provider is servicing the entity
15: */
16: public function getContext(): ?SyncContextInterface;
17:
18: /**
19: * Get the deferred entity's canonical location in the form of an array
20: *
21: * @param LinkType::* $type
22: * @return array<string,int|string>
23: */
24: public function toLink(int $type = LinkType::DEFAULT, bool $compact = true): array;
25:
26: /**
27: * Get the deferred entity's canonical location in the form of a URI
28: */
29: public function getUri(bool $compact = true): string;
30:
31: /**
32: * Resolve the deferred entity from the provider or the local entity store
33: *
34: * This method returns the resolved entity without taking further action.
35: * {@see DeferredEntityInterface::replace()} is called separately.
36: *
37: * @return TEntity
38: */
39: public function resolve(): SyncEntityInterface;
40:
41: /**
42: * Resolve the deferred entity with an instance
43: *
44: * If `$callback` was given when the entity was deferred, it is called with
45: * the given entity, otherwise the variable or property given via `$replace`
46: * is replaced.
47: *
48: * Subsequent calls to {@see DeferredEntityInterface::resolve()} return the
49: * same instance.
50: *
51: * @param TEntity $entity
52: * @throws LogicException if the entity has already been resolved.
53: */
54: public function replace(SyncEntityInterface $entity): void;
55:
56: /**
57: * Defer retrieval of a sync entity
58: *
59: * @template TReplace of TEntity|static|null
60: *
61: * @param SyncProviderInterface $provider The provider servicing the entity.
62: * @param SyncContextInterface|null $context The context within which the
63: * provider is servicing the entity.
64: * @param class-string<TEntity> $entity The entity to instantiate.
65: * @param int|string $entityId The identifier of the deferred entity.
66: * @param TReplace $replace Refers to the variable or property to replace
67: * when the entity is resolved.
68: * @param (Closure(TEntity): void)|null $callback If given, `$replace` is
69: * ignored and the resolved entity is passed to the callback.
70: * @param-out ($callback is null ? TEntity|static : TReplace) $replace
71: */
72: public static function defer(
73: SyncProviderInterface $provider,
74: ?SyncContextInterface $context,
75: string $entity,
76: $entityId,
77: &$replace = null,
78: ?Closure $callback = null
79: ): void;
80:
81: /**
82: * Defer retrieval of a list of sync entities
83: *
84: * @param SyncProviderInterface $provider The provider servicing the entity.
85: * @param SyncContextInterface|null $context The context within which the
86: * provider is servicing the entity.
87: * @param class-string<TEntity> $entity The entity to instantiate.
88: * @param array<int|string> $entityIds A list of deferred entity
89: * identifiers.
90: * @param array<TEntity|static>|null $replace Refers to the variable or
91: * property to replace when the entities are resolved.
92: * @param (Closure(TEntity): void)|null $callback If given, `$replace` is
93: * ignored and each resolved entity is passed to the callback.
94: */
95: public static function deferList(
96: SyncProviderInterface $provider,
97: ?SyncContextInterface $context,
98: string $entity,
99: array $entityIds,
100: &$replace = null,
101: ?Closure $callback = null
102: ): void;
103: }
104: