1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Sync; |
4: | |
5: | use Salient\Contract\Container\ContainerInterface; |
6: | use Salient\Contract\Sync\SyncEntityInterface; |
7: | use Salient\Contract\Sync\SyncOperation; |
8: | use Salient\Contract\Sync\SyncProviderInterface; |
9: | use Salient\Contract\Sync\SyncStoreInterface; |
10: | use Salient\Core\Facade\Sync; |
11: | use Salient\Utility\AbstractUtility; |
12: | use Salient\Utility\Arr; |
13: | use Salient\Utility\Get; |
14: | use Salient\Utility\Regex; |
15: | |
16: | final class SyncUtil extends AbstractUtility |
17: | { |
18: | |
19: | |
20: | |
21: | |
22: | |
23: | |
24: | |
25: | public static function isListOperation(int $operation): bool |
26: | { |
27: | return [ |
28: | SyncOperation::CREATE_LIST => true, |
29: | SyncOperation::READ_LIST => true, |
30: | SyncOperation::UPDATE_LIST => true, |
31: | SyncOperation::DELETE_LIST => true, |
32: | ][$operation] ?? false; |
33: | } |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | public static function isWriteOperation(int $operation): bool |
43: | { |
44: | return [ |
45: | SyncOperation::CREATE => true, |
46: | SyncOperation::UPDATE => true, |
47: | SyncOperation::DELETE => true, |
48: | SyncOperation::CREATE_LIST => true, |
49: | SyncOperation::UPDATE_LIST => true, |
50: | SyncOperation::DELETE_LIST => true, |
51: | ][$operation] ?? false; |
52: | } |
53: | |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | |
60: | public static function getEntityTypeProvider( |
61: | string $entityType, |
62: | ?SyncStoreInterface $store = null |
63: | ): string { |
64: | if ($store) { |
65: | $helper = $store->getNamespaceHelper($entityType); |
66: | if ($helper) { |
67: | return $helper->getEntityTypeProvider($entityType); |
68: | } |
69: | } |
70: | |
71: | |
72: | return Arr::implode('\\', [ |
73: | Get::namespace($entityType), |
74: | 'Provider', |
75: | Get::basename($entityType) . 'Provider', |
76: | ]); |
77: | } |
78: | |
79: | |
80: | |
81: | |
82: | |
83: | |
84: | |
85: | public static function getProviderEntityTypes( |
86: | string $provider, |
87: | ?SyncStoreInterface $store = null |
88: | ): array { |
89: | if ($store) { |
90: | $helper = $store->getNamespaceHelper($provider); |
91: | if ($helper) { |
92: | return $helper->getProviderEntityTypes($provider); |
93: | } |
94: | } |
95: | |
96: | if (Regex::match( |
97: | '/^\\\\?(?<namespace>(?:[^\\\\]++\\\\)*)Provider\\\\(?<class>[^\\\\]+)Provider$/', |
98: | $provider, |
99: | $matches, |
100: | )) { |
101: | |
102: | return [$matches['namespace'] . $matches['class']]; |
103: | } |
104: | |
105: | return []; |
106: | } |
107: | |
108: | |
109: | |
110: | |
111: | |
112: | |
113: | public static function getEntityTypeUri( |
114: | string $entityType, |
115: | bool $compact = true, |
116: | ?SyncStoreInterface $store = null |
117: | ): string { |
118: | if ($store) { |
119: | return $store->getEntityTypeUri($entityType, $compact); |
120: | } |
121: | return '/' . str_replace('\\', '/', ltrim($entityType, '\\')); |
122: | } |
123: | |
124: | |
125: | |
126: | |
127: | |
128: | |
129: | |
130: | |
131: | public static function getStore(?ContainerInterface $container = null): SyncStoreInterface |
132: | { |
133: | if ($container && $container->hasInstance(SyncStoreInterface::class)) { |
134: | return $container->get(SyncStoreInterface::class); |
135: | } |
136: | if (Sync::isLoaded()) { |
137: | return Sync::getInstance(); |
138: | } |
139: | return new SyncStore(); |
140: | } |
141: | } |
142: | |