1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Contract\Sync; |
4: | |
5: | use Salient\Contract\Container\ContainerInterface; |
6: | use Salient\Contract\Core\Entity\Relatable; |
7: | use Salient\Contract\Core\Provider\ProvidableEntityInterface; |
8: | use Salient\Contract\Core\HasId; |
9: | use Salient\Contract\Core\HasName; |
10: | use Salient\Contract\Sync\Exception\SyncEntityNotFoundExceptionInterface; |
11: | use JsonSerializable; |
12: | |
13: | /** |
14: | * Represents the state of an entity in an external system |
15: | * |
16: | * @extends ProvidableEntityInterface<SyncProviderInterface,SyncContextInterface> |
17: | */ |
18: | interface SyncEntityInterface extends |
19: | HasId, |
20: | HasName, |
21: | ProvidableEntityInterface, |
22: | Relatable, |
23: | JsonSerializable |
24: | { |
25: | /** |
26: | * Get the entity's default provider |
27: | */ |
28: | public static function getDefaultProvider(ContainerInterface $container): SyncProviderInterface; |
29: | |
30: | /** |
31: | * Perform sync operations on the entity using its default provider |
32: | * |
33: | * @return SyncEntityProviderInterface<static> |
34: | */ |
35: | public static function withDefaultProvider(ContainerInterface $container, ?SyncContextInterface $context = null): SyncEntityProviderInterface; |
36: | |
37: | /** |
38: | * Get the entity's serialization rules |
39: | * |
40: | * @return SyncSerializeRulesInterface<static> |
41: | */ |
42: | public static function getSerializeRules(): SyncSerializeRulesInterface; |
43: | |
44: | /** |
45: | * Get the plural form of the entity's short name |
46: | * |
47: | * If this method returns a value other than `null` or the unqualified name |
48: | * of the entity, it may be used to identify provider methods that implement |
49: | * sync operations on the entity. |
50: | * |
51: | * For example, if `Faculty::getPlural()` returns `null`, a provider may |
52: | * implement `Faculty` sync operations via one or more of the following: |
53: | * |
54: | * - `create_faculty()` |
55: | * - `get_faculty()` |
56: | * - `update_faculty()` |
57: | * - `delete_faculty()` |
58: | * - `createList_faculty()` |
59: | * - `getList_faculty()` |
60: | * - `updateList_faculty()` |
61: | * - `deleteList_faculty()` |
62: | * |
63: | * If the same method returns `"Faculties"`, these are also recognised as |
64: | * `Faculty` sync implementations: |
65: | * |
66: | * - `createFaculty()` |
67: | * - `getFaculty()` |
68: | * - `updateFaculty()` |
69: | * - `deleteFaculty()` |
70: | * - `createFaculties()` |
71: | * - `getFaculties()` |
72: | * - `updateFaculties()` |
73: | * - `deleteFaculties()` |
74: | */ |
75: | public static function getPlural(): ?string; |
76: | |
77: | /** |
78: | * Get the unique identifier assigned to the entity by its provider |
79: | */ |
80: | public function getId(); |
81: | |
82: | /** |
83: | * Get the unique identifier assigned to the entity by its canonical backend |
84: | * |
85: | * If a provider is bound to the service container as the default |
86: | * implementation of the entity's underlying provider interface, it is |
87: | * regarded as its canonical backend. |
88: | * |
89: | * To improve the accuracy and performance of sync operations, providers |
90: | * should propagate this value to and from backends capable of storing it. |
91: | * |
92: | * @return int|string|null |
93: | */ |
94: | public function getCanonicalId(); |
95: | |
96: | /** |
97: | * Get the name of the entity |
98: | */ |
99: | public function getName(): string; |
100: | |
101: | /** |
102: | * Resolve a name or entity ID to the entity ID of one matching entity |
103: | * |
104: | * Returns: |
105: | * |
106: | * - `null` if `$nameOrId` is `null` |
107: | * - `$nameOrId` if it is a valid identifier for the entity in the given |
108: | * provider (see {@see SyncProviderInterface::isValidIdentifier()}), or |
109: | * - the entity ID of the entity to which `$nameOrId` resolves |
110: | * |
111: | * A {@see SyncEntityNotFoundExceptionInterface} is thrown if: |
112: | * |
113: | * - there are no matching entities, or |
114: | * - there are multiple matching entities |
115: | * |
116: | * @param int|string|null $nameOrId |
117: | * @param SyncProviderInterface|SyncContextInterface $providerOrContext |
118: | * @return int|string|null |
119: | */ |
120: | public static function idFromNameOrId( |
121: | $nameOrId, |
122: | $providerOrContext, |
123: | ?float $uncertaintyThreshold = null, |
124: | ?string $nameProperty = null, |
125: | ?float &$uncertainty = null |
126: | ); |
127: | |
128: | /** |
129: | * Serialize the entity and any nested entities |
130: | * |
131: | * Rules returned by {@see SyncEntityInterface::getSerializeRules()} are |
132: | * used. |
133: | * |
134: | * @return array<string,mixed> |
135: | */ |
136: | public function toArray(?SyncStoreInterface $store = null): array; |
137: | |
138: | /** |
139: | * Use the given serialization rules to serialize the entity and any nested |
140: | * entities |
141: | * |
142: | * @param SyncSerializeRulesInterface<static> $rules |
143: | * @return array<string,mixed> |
144: | */ |
145: | public function toArrayWith(SyncSerializeRulesInterface $rules, ?SyncStoreInterface $store = null): array; |
146: | |
147: | /** |
148: | * Get the entity's canonical location in the form of an array |
149: | * |
150: | * Inspired by JSON-LD. |
151: | * |
152: | * @param LinkType::* $type |
153: | * @return array<string,int|string> |
154: | */ |
155: | public function toLink(?SyncStoreInterface $store = null, int $type = LinkType::DEFAULT, bool $compact = true): array; |
156: | |
157: | /** |
158: | * Get the entity's canonical location in the form of a URI |
159: | * |
160: | * Inspired by OData. |
161: | */ |
162: | public function getUri(?SyncStoreInterface $store = null, bool $compact = true): string; |
163: | } |
164: |