1 | <?php |
||
10 | class RestObject extends CoreRestObject |
||
11 | { |
||
12 | protected $primaryKey = '_id'; |
||
13 | protected $databaseClass = Database::class; |
||
14 | protected $indexNames = []; |
||
15 | |||
16 | 5 | public function validateUniqueFields() |
|
25 | |||
26 | 5 | protected function allUniqueFieldHasIndex($fields) |
|
27 | { |
||
28 | 5 | $indexes = array_keys($this->indexNames); |
|
29 | 5 | $existingFields = array_keys(get_object_vars($this->resource)); |
|
30 | 5 | $uniqueFields = array_diff( |
|
31 | array_intersect($fields, $existingFields), //Is unique and value is set |
||
32 | 5 | [$this->primaryKey] //Collect only secondary indexes |
|
33 | ); |
||
34 | 5 | $indexedFields = array_intersect($uniqueFields, $indexes); //Is unique and value is set and is indexed |
|
35 | 5 | sort($uniqueFields); |
|
36 | 5 | sort($indexedFields); |
|
37 | 5 | return $uniqueFields===$indexedFields; |
|
38 | } |
||
39 | |||
40 | 2 | protected function scanUniqueFields($fields) |
|
57 | |||
58 | 2 | protected function generateScan($fields) |
|
76 | |||
77 | 2 | protected function generateScanFilter($fields) |
|
97 | |||
98 | 3 | protected function queryUniqueFields($fields) |
|
99 | { |
||
100 | 3 | $queries = $this->generateQueries($fields); |
|
101 | 3 | $promises = $this->generatePromises($queries); |
|
102 | 3 | $results = PromiseFunctions\unwrap($promises); |
|
103 | 3 | foreach ($results as $result) { |
|
104 | 2 | if ($result !== null && $result['Count'] !== 0) { |
|
105 | 1 | throw new RestException( |
|
106 | 1 | 'Unique constraint violation', |
|
107 | [ |
||
108 | 1 | 'resourceName' => $this->resourceName, |
|
109 | 1 | 'confilct' => $result, |
|
110 | 2 | 'mode' => 'query', |
|
111 | ] |
||
112 | ); |
||
113 | } |
||
114 | } |
||
115 | 2 | } |
|
116 | |||
117 | 3 | protected function generateQueries($fields) |
|
118 | { |
||
119 | 3 | $queries = []; |
|
120 | 3 | foreach ($fields as $field) { |
|
121 | 2 | if (property_exists($this->resource, $field)) { |
|
122 | 2 | $queries[] = $this->generateQuery($field); |
|
123 | } |
||
124 | } |
||
125 | 3 | return $queries; |
|
126 | } |
||
127 | |||
128 | 2 | protected function generateQuery($field) |
|
149 | |||
150 | 3 | protected function generatePromises($queries) |
|
151 | { |
||
152 | 3 | $client = $this->getClient(); |
|
159 | |||
160 | 2 | protected function generatePromise($client, $request) |
|
176 | } |
||
177 |