1: <?php declare(strict_types=1);
2:
3: namespace Salient\Contract\Sync;
4:
5: use Salient\Contract\Core\Provider\HasProvider;
6: use Salient\Contract\Iterator\FluentIteratorInterface;
7: use Salient\Contract\HasTextComparisonFlag;
8: use Closure;
9:
10: /**
11: * Provides an entity-agnostic interface to a SyncProviderInterface's
12: * implementation of sync operations for an entity
13: *
14: * @template TEntity of SyncEntityInterface
15: *
16: * @extends HasProvider<SyncProviderInterface>
17: */
18: interface SyncEntityProviderInterface extends HasProvider, HasTextComparisonFlag
19: {
20: /**
21: * Get the sync entity being serviced
22: *
23: * @return class-string<TEntity>
24: */
25: public function entity(): string;
26:
27: /**
28: * Perform an arbitrary sync operation on one or more backend entities
29: *
30: * @internal
31: *
32: * @param SyncOperation::* $operation
33: * @param mixed ...$args
34: * @return FluentIteratorInterface<array-key,TEntity>|TEntity
35: * @phpstan-return (
36: * $operation is SyncOperation::*_LIST
37: * ? FluentIteratorInterface<array-key,TEntity>
38: * : TEntity
39: * )
40: */
41: public function run(int $operation, ...$args);
42:
43: /**
44: * Add an entity to the backend
45: *
46: * @param TEntity $entity
47: * @param mixed ...$args
48: * @return TEntity
49: */
50: public function create($entity, ...$args): SyncEntityInterface;
51:
52: /**
53: * Get an entity from the backend
54: *
55: * @param int|string|null $id
56: * @param mixed ...$args
57: * @return TEntity
58: */
59: public function get($id, ...$args): SyncEntityInterface;
60:
61: /**
62: * Update an entity in the backend
63: *
64: * @param TEntity $entity
65: * @param mixed ...$args
66: * @return TEntity
67: */
68: public function update($entity, ...$args): SyncEntityInterface;
69:
70: /**
71: * Delete an entity from the backend
72: *
73: * @param TEntity $entity
74: * @param mixed ...$args
75: * @return TEntity
76: */
77: public function delete($entity, ...$args): SyncEntityInterface;
78:
79: /**
80: * Add a list of entities to the backend
81: *
82: * @param iterable<TEntity> $entities
83: * @param mixed ...$args
84: * @return FluentIteratorInterface<array-key,TEntity>
85: */
86: public function createList(iterable $entities, ...$args): FluentIteratorInterface;
87:
88: /**
89: * Get a list of entities from the backend
90: *
91: * @param mixed ...$args
92: * @return FluentIteratorInterface<array-key,TEntity>
93: */
94: public function getList(...$args): FluentIteratorInterface;
95:
96: /**
97: * Update a list of entities in the backend
98: *
99: * @param iterable<TEntity> $entities
100: * @param mixed ...$args
101: * @return FluentIteratorInterface<array-key,TEntity>
102: */
103: public function updateList(iterable $entities, ...$args): FluentIteratorInterface;
104:
105: /**
106: * Delete a list of entities from the backend
107: *
108: * @param iterable<TEntity> $entities
109: * @param mixed ...$args
110: * @return FluentIteratorInterface<array-key,TEntity>
111: */
112: public function deleteList(iterable $entities, ...$args): FluentIteratorInterface;
113:
114: /**
115: * Perform an arbitrary sync operation on a list of backend entities and
116: * return an array
117: *
118: * @internal
119: *
120: * @param SyncOperation::*_LIST $operation
121: * @param mixed ...$args
122: * @return array<TEntity>
123: */
124: public function runA(int $operation, ...$args): array;
125:
126: /**
127: * Add a list of entities to the backend and return an array
128: *
129: * @param iterable<TEntity> $entities
130: * @param mixed ...$args
131: * @return array<TEntity>
132: */
133: public function createListA(iterable $entities, ...$args): array;
134:
135: /**
136: * Get a list of entities from the backend as an array
137: *
138: * @param mixed ...$args
139: * @return array<TEntity>
140: */
141: public function getListA(...$args): array;
142:
143: /**
144: * Update a list of entities in the backend and return an array
145: *
146: * @param iterable<TEntity> $entities
147: * @param mixed ...$args
148: * @return array<TEntity>
149: */
150: public function updateListA(iterable $entities, ...$args): array;
151:
152: /**
153: * Delete a list of entities from the backend and return an array
154: *
155: * @param iterable<TEntity> $entities
156: * @param mixed ...$args
157: * @return array<TEntity>
158: */
159: public function deleteListA(iterable $entities, ...$args): array;
160:
161: /**
162: * Use a property of the entity class to resolve names to entities
163: *
164: * @param (Closure(TEntity): string)|string|null $nameProperty If `null`,
165: * entity names are taken from {@see SyncEntityInterface::getName()}.
166: * @param int-mask-of<SyncEntityProviderInterface::*> $flags
167: * @param array<SyncEntityProviderInterface::ALGORITHM_*,float>|float|null $uncertaintyThreshold If
168: * the uncertainty of a match for a given name is greater than or equal to
169: * this value (between `0.0` and `1.0`), the entity is not returned.
170: * @param (Closure(TEntity): (int|float))|string|null $weightProperty If
171: * multiple entities are equally similar to a given name, the one with the
172: * greatest weight (highest value) is preferred.
173: * @return SyncEntityResolverInterface<TEntity>
174: */
175: public function getResolver(
176: $nameProperty = null,
177: int $flags = SyncEntityProviderInterface::ALGORITHM_SAME,
178: $uncertaintyThreshold = null,
179: $weightProperty = null,
180: bool $requireOneMatch = false
181: ): SyncEntityResolverInterface;
182:
183: /**
184: * Perform sync operations on the backend directly, ignoring any entities in
185: * the entity store
186: *
187: * @return $this
188: */
189: public function online();
190:
191: /**
192: * Perform "get" operations on the entity store, throwing an exception if
193: * entities have never been synced with the backend
194: *
195: * @return $this
196: */
197: public function offline();
198:
199: /**
200: * Retrieve entities directly from the backend unless it cannot be reached
201: * or the entity store's copies are sufficiently fresh
202: *
203: * @return $this
204: */
205: public function offlineFirst();
206:
207: /**
208: * Do not resolve deferred entities or relationships
209: *
210: * @return $this
211: */
212: public function doNotResolve();
213:
214: /**
215: * Resolve deferred entities and relationships immediately
216: *
217: * @return $this
218: */
219: public function resolveEarly();
220:
221: /**
222: * Resolve deferred entities and relationships after reaching the end of
223: * each stream of entity data
224: *
225: * @return $this
226: */
227: public function resolveLate();
228:
229: /**
230: * Do not hydrate entities returned by the backend
231: *
232: * @return $this
233: */
234: public function doNotHydrate();
235:
236: /**
237: * Apply the given hydration policy to entities returned by the backend
238: *
239: * @param HydrationPolicy::* $policy
240: * @param class-string<SyncEntityInterface>|null $entity
241: * @param array<int<1,max>>|int<1,max>|null $depth
242: * @return $this
243: */
244: public function hydrate(
245: int $policy = HydrationPolicy::EAGER,
246: ?string $entity = null,
247: $depth = null
248: );
249: }
250: