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: * @param SyncProviderInterface $provider The provider servicing the entity.
60: * @param SyncContextInterface|null $context The context within which the
61: * provider is servicing the entity.
62: * @param class-string<TEntity> $entity The entity to instantiate.
63: * @param int|string $entityId The identifier of the deferred entity.
64: * @param TEntity|static|null $replace Refers to the variable or property to
65: * replace when the entity is resolved.
66: * @param (Closure(TEntity): void)|null $callback If given, `$replace` is
67: * ignored and the resolved entity is passed to the callback.
68: */
69: public static function defer(
70: SyncProviderInterface $provider,
71: ?SyncContextInterface $context,
72: string $entity,
73: $entityId,
74: &$replace = null,
75: ?Closure $callback = null
76: ): void;
77:
78: /**
79: * Defer retrieval of a list of sync entities
80: *
81: * @param SyncProviderInterface $provider The provider servicing the entity.
82: * @param SyncContextInterface|null $context The context within which the
83: * provider is servicing the entity.
84: * @param class-string<TEntity> $entity The entity to instantiate.
85: * @param array<int|string> $entityIds A list of deferred entity
86: * identifiers.
87: * @param array<TEntity|static>|null $replace Refers to the variable or
88: * property to replace when the entities are resolved.
89: * @param (Closure(TEntity): void)|null $callback If given, `$replace` is
90: * ignored and each resolved entity is passed to the callback.
91: */
92: public static function deferList(
93: SyncProviderInterface $provider,
94: ?SyncContextInterface $context,
95: string $entity,
96: array $entityIds,
97: &$replace = null,
98: ?Closure $callback = null
99: ): void;
100: }
101: