1: <?php declare(strict_types=1);
2:
3: namespace Salient\Contract\Sync;
4:
5: use Salient\Contract\Core\Provider\HasProvider;
6: use Salient\Contract\Core\TextComparisonAlgorithm;
7: use Salient\Contract\Core\TextComparisonFlag;
8: use Salient\Contract\Iterator\FluentIteratorInterface;
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
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 int-mask-of<TextComparisonAlgorithm::*|TextComparisonFlag::*> $algorithm
165: * @param array<TextComparisonAlgorithm::*,float>|float|null $uncertaintyThreshold
166: * @param string|null $weightProperty If multiple entities are equally
167: * similar to a given name, the one with the highest weight is preferred.
168: * @return SyncEntityResolverInterface<TEntity>
169: */
170: public function getResolver(
171: ?string $nameProperty = null,
172: int $algorithm = TextComparisonAlgorithm::SAME,
173: $uncertaintyThreshold = null,
174: ?string $weightProperty = null,
175: bool $requireOneMatch = false
176: ): SyncEntityResolverInterface;
177:
178: /**
179: * Perform sync operations on the backend directly, ignoring any entities in
180: * the entity store
181: *
182: * @return $this
183: */
184: public function online();
185:
186: /**
187: * Perform "get" operations on the entity store, throwing an exception if
188: * entities have never been synced with the backend
189: *
190: * @return $this
191: */
192: public function offline();
193:
194: /**
195: * Retrieve entities directly from the backend unless it cannot be reached
196: * or the entity store's copies are sufficiently fresh
197: *
198: * @return $this
199: */
200: public function offlineFirst();
201:
202: /**
203: * Do not resolve deferred entities or relationships
204: *
205: * @return $this
206: */
207: public function doNotResolve();
208:
209: /**
210: * Resolve deferred entities and relationships immediately
211: *
212: * @return $this
213: */
214: public function resolveEarly();
215:
216: /**
217: * Resolve deferred entities and relationships after reaching the end of
218: * each stream of entity data
219: *
220: * @return $this
221: */
222: public function resolveLate();
223:
224: /**
225: * Do not hydrate entities returned by the backend
226: *
227: * @return $this
228: */
229: public function doNotHydrate();
230:
231: /**
232: * Apply the given hydration policy to entities returned by the backend
233: *
234: * @param HydrationPolicy::* $policy
235: * @param class-string<SyncEntityInterface>|null $entity
236: * @param array<int<1,max>>|int<1,max>|null $depth
237: * @return $this
238: */
239: public function hydrate(
240: int $policy = HydrationPolicy::EAGER,
241: ?string $entity = null,
242: $depth = null
243: );
244: }
245: