@@ -8,7 +8,7 @@ |
||
8 | 8 | * @param array $options |
9 | 9 | */ |
10 | 10 | public function __construct(array $keySchema, array $valueSchema, array $options = []) { |
11 | - if(!array_key_exists('dsn', $options)) { |
|
11 | + if (!array_key_exists('dsn', $options)) { |
|
12 | 12 | $options['dsn'] = 'sqlite::memory:'; |
13 | 13 | } |
14 | 14 | parent::__construct($keySchema, $valueSchema, $options); |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | * @param array $options |
10 | 10 | */ |
11 | 11 | public function __construct($filename = null, array $keySchema, array $valueSchema, array $options = []) { |
12 | - if($filename === null) { |
|
12 | + if ($filename === null) { |
|
13 | 13 | $filename = tempnam(sys_get_temp_dir(), 'data-diff-'); |
14 | 14 | } |
15 | 15 | $this->createFile($filename); |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | try { |
26 | 26 | $fp = fopen($filename, 'w+'); |
27 | 27 | } finally { |
28 | - if(is_resource($fp)) { |
|
28 | + if (is_resource($fp)) { |
|
29 | 29 | fclose($fp); |
30 | 30 | } |
31 | 31 | } |
@@ -19,9 +19,9 @@ discard block |
||
19 | 19 | * @param array $converter |
20 | 20 | */ |
21 | 21 | public function __construct(array $localData = null, array $foreignData = null, array $keys, array $valueKeys, array $converter) { |
22 | - if($localData !== null) { |
|
22 | + if ($localData !== null) { |
|
23 | 23 | $this->data = $localData; |
24 | - } elseif($foreignData !== null) { |
|
24 | + } elseif ($foreignData !== null) { |
|
25 | 25 | $this->data = $foreignData; |
26 | 26 | } |
27 | 27 | $localData = is_array($localData) ? $localData : []; |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | * @return mixed |
107 | 107 | */ |
108 | 108 | public function offsetGet($offset) { |
109 | - if($this->offsetExists($offset)) { |
|
109 | + if ($this->offsetExists($offset)) { |
|
110 | 110 | return $this->data[$offset]; |
111 | 111 | } |
112 | 112 | return null; |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | * @return void |
127 | 127 | */ |
128 | 128 | public function offsetUnset($offset) { |
129 | - if($this->offsetExists($offset)) { |
|
129 | + if ($this->offsetExists($offset)) { |
|
130 | 130 | unset($this->data[$offset]); |
131 | 131 | } |
132 | 132 | } |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | */ |
83 | 83 | private function buildSchema($schema) { |
84 | 84 | $def = []; |
85 | - foreach($schema as $name => $type) { |
|
85 | + foreach ($schema as $name => $type) { |
|
86 | 86 | switch ($type) { |
87 | 87 | case 'BOOL': |
88 | 88 | $def[] = sprintf('CASE WHEN CAST(:'.$name.' AS INT) = 0 THEN \'false\' ELSE \'true\' END'); |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | break; |
108 | 108 | } |
109 | 109 | } |
110 | - if(!count($def)) { |
|
110 | + if (!count($def)) { |
|
111 | 111 | throw new EmptySchemaException('Can\'t operate with empty schema'); |
112 | 112 | } |
113 | 113 | return join('||"|"||', $def); |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | */ |
121 | 121 | private function buildConverter($schema) { |
122 | 122 | $def = []; |
123 | - foreach($schema as $name => $type) { |
|
123 | + foreach ($schema as $name => $type) { |
|
124 | 124 | switch ($type) { |
125 | 125 | case 'BOOL': |
126 | 126 | $def[$name] = 'boolval'; |
@@ -129,19 +129,19 @@ discard block |
||
129 | 129 | $def[$name] = 'intval'; |
130 | 130 | break; |
131 | 131 | case 'FLOAT': |
132 | - $def[$name] = function ($value) { return number_format((float) $value, 6, '.', ''); }; |
|
132 | + $def[$name] = function($value) { return number_format((float) $value, 6, '.', ''); }; |
|
133 | 133 | break; |
134 | 134 | case 'DOUBLE': |
135 | - $def[$name] = function ($value) { return number_format((float) $value, 12, '.', ''); }; |
|
135 | + $def[$name] = function($value) { return number_format((float) $value, 12, '.', ''); }; |
|
136 | 136 | break; |
137 | 137 | case 'MONEY': |
138 | - $def[$name] = function ($value) { return number_format((float) $value, 2, '.', ''); }; |
|
138 | + $def[$name] = function($value) { return number_format((float) $value, 2, '.', ''); }; |
|
139 | 139 | break; |
140 | 140 | case 'STRING': |
141 | - $def[$name] = function ($value) { return (string) $value; }; |
|
141 | + $def[$name] = function($value) { return (string) $value; }; |
|
142 | 142 | break; |
143 | 143 | case 'MD5': |
144 | - $def[$name] = function ($value) { return md5((string) $value); }; |
|
144 | + $def[$name] = function($value) { return md5((string) $value); }; |
|
145 | 145 | break; |
146 | 146 | } |
147 | 147 | } |
@@ -151,11 +151,11 @@ discard block |
||
151 | 151 | /** |
152 | 152 | */ |
153 | 153 | private function compatibility() { |
154 | - if(!$this->testStatement('SELECT printf("%0.2f", 19.99999) AS res')) { |
|
154 | + if (!$this->testStatement('SELECT printf("%0.2f", 19.99999) AS res')) { |
|
155 | 155 | $this->registerUDFunction('printf', 'sprintf'); |
156 | 156 | } |
157 | 157 | |
158 | - if(!$this->testStatement('SELECT md5("aaa") AS md5res')) { |
|
158 | + if (!$this->testStatement('SELECT md5("aaa") AS md5res')) { |
|
159 | 159 | $this->registerUDFunction('md5', 'md5'); |
160 | 160 | } |
161 | 161 | } |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | * @throws Exception |
179 | 179 | */ |
180 | 180 | private function registerUDFunction($name, $callback) { |
181 | - if(!method_exists($this->pdo, 'sqliteCreateFunction')) { |
|
181 | + if (!method_exists($this->pdo, 'sqliteCreateFunction')) { |
|
182 | 182 | throw new Exception('It is not possible to create user defined functions for rkr/data-diff\'s sqlite instance'); |
183 | 183 | } |
184 | 184 | call_user_func([$this->pdo, 'sqliteCreateFunction'], $name, $callback); |
@@ -187,7 +187,7 @@ discard block |
||
187 | 187 | /** |
188 | 188 | */ |
189 | 189 | private function initSqlite() { |
190 | - $tryThis = function ($query) { |
|
190 | + $tryThis = function($query) { |
|
191 | 191 | try { |
192 | 192 | $this->pdo->exec($query); |
193 | 193 | } catch (Exception $e) { |
@@ -213,11 +213,11 @@ discard block |
||
213 | 213 | * @return array |
214 | 214 | */ |
215 | 215 | private function defineOptionDefaults($options) { |
216 | - if(!array_key_exists('dsn', $options)) { |
|
216 | + if (!array_key_exists('dsn', $options)) { |
|
217 | 217 | $options['dsn'] = 'sqlite::memory:'; |
218 | 218 | } |
219 | - if(!array_key_exists('duplicate_key_handler', $options)) { |
|
220 | - $options['duplicate_key_handler'] = function (array $newData = null, array $oldData = null) { |
|
219 | + if (!array_key_exists('duplicate_key_handler', $options)) { |
|
220 | + $options['duplicate_key_handler'] = function(array $newData = null, array $oldData = null) { |
|
221 | 221 | return array_merge($oldData, $newData); |
222 | 222 | }; |
223 | 223 | } |
@@ -64,10 +64,10 @@ discard block |
||
64 | 64 | */ |
65 | 65 | public function addRow(array $data, array $translation = null, $duplicateKeyHandler = null) { |
66 | 66 | $data = $this->translate($data, $translation); |
67 | - if($duplicateKeyHandler === null) { |
|
67 | + if ($duplicateKeyHandler === null) { |
|
68 | 68 | $duplicateKeyHandler = $this->duplicateKeyHandler; |
69 | 69 | } |
70 | - $buildMetaData = function (array $data, array $keys) { |
|
70 | + $buildMetaData = function(array $data, array $keys) { |
|
71 | 71 | $metaData = $data; |
72 | 72 | $metaData = array_diff_key($metaData, array_diff_key($metaData, $keys)); |
73 | 73 | $metaData['___data'] = json_encode($data); |
@@ -75,19 +75,19 @@ discard block |
||
75 | 75 | return $metaData; |
76 | 76 | }; |
77 | 77 | $metaData = $buildMetaData($data, $this->converter); |
78 | - if($duplicateKeyHandler === null) { |
|
78 | + if ($duplicateKeyHandler === null) { |
|
79 | 79 | $this->replaceStmt->execute($metaData); |
80 | 80 | } else { |
81 | 81 | try { |
82 | 82 | $this->insertStmt->execute($metaData); |
83 | 83 | } catch (\PDOException $e) { |
84 | - if(strpos($e->getMessage(), 'UNIQUE constraint failed') !== false) { |
|
84 | + if (strpos($e->getMessage(), 'UNIQUE constraint failed') !== false) { |
|
85 | 85 | $metaData = $buildMetaData($data, $this->converter); |
86 | 86 | unset($metaData['___data']); |
87 | 87 | unset($metaData['___sort']); |
88 | 88 | $this->selectStmt->execute($metaData); |
89 | 89 | $oldData = $this->selectStmt->fetch(PDO::FETCH_COLUMN, 0); |
90 | - if($oldData === null) { |
|
90 | + if ($oldData === null) { |
|
91 | 91 | $oldData = []; |
92 | 92 | } else { |
93 | 93 | $oldData = json_decode($oldData, true); |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | * @return $this |
111 | 111 | */ |
112 | 112 | public function addRows($rows, array $translation = null, $duplicateKeyHandler = null) { |
113 | - foreach($rows as $row) { |
|
113 | + foreach ($rows as $row) { |
|
114 | 114 | $this->addRow($row, $translation, $duplicateKeyHandler); |
115 | 115 | } |
116 | 116 | return $this; |
@@ -121,11 +121,11 @@ discard block |
||
121 | 121 | */ |
122 | 122 | public function hasAnyChanges() { |
123 | 123 | /** @noinspection PhpUnusedLocalVariableInspection */ |
124 | - foreach($this->getNewOrChanged() as $_) { |
|
124 | + foreach ($this->getNewOrChanged() as $_) { |
|
125 | 125 | return true; |
126 | 126 | } |
127 | 127 | /** @noinspection PhpUnusedLocalVariableInspection */ |
128 | - foreach($this->getMissing() as $_) { |
|
128 | + foreach ($this->getMissing() as $_) { |
|
129 | 129 | return true; |
130 | 130 | } |
131 | 131 | return false; |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | s2.s_ab IS NULL |
153 | 153 | ORDER BY |
154 | 154 | s1.s_sort |
155 | - ', function (DiffStorageStoreRowInterface $row) { |
|
155 | + ', function(DiffStorageStoreRowInterface $row) { |
|
156 | 156 | return $this->formatNewRow($row); |
157 | 157 | }); |
158 | 158 | } |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | s1.s_value != s2.s_value |
179 | 179 | ORDER BY |
180 | 180 | s1.s_sort |
181 | - ', function (DiffStorageStoreRowInterface $row) { |
|
181 | + ', function(DiffStorageStoreRowInterface $row) { |
|
182 | 182 | return $this->formatChangedRow($row); |
183 | 183 | }); |
184 | 184 | } |
@@ -202,8 +202,8 @@ discard block |
||
202 | 202 | ((s2.s_ab IS NULL) OR (s1.s_value != s2.s_value)) |
203 | 203 | ORDER BY |
204 | 204 | s1.s_sort |
205 | - ', function (DiffStorageStoreRowInterface $row) { |
|
206 | - if(count($row->getForeign()->getValueData())) { |
|
205 | + ', function(DiffStorageStoreRowInterface $row) { |
|
206 | + if (count($row->getForeign()->getValueData())) { |
|
207 | 207 | return $this->formatChangedRow($row); |
208 | 208 | } else { |
209 | 209 | return $this->formatNewRow($row); |
@@ -232,7 +232,7 @@ discard block |
||
232 | 232 | s2.s_ab IS NULL |
233 | 233 | ORDER BY |
234 | 234 | s1.s_sort |
235 | - ', function (DiffStorageStoreRowInterface $row) { |
|
235 | + ', function(DiffStorageStoreRowInterface $row) { |
|
236 | 236 | return $this->formatMissingRow($row); |
237 | 237 | }); |
238 | 238 | } |
@@ -254,7 +254,7 @@ discard block |
||
254 | 254 | private function query($query, $stringFormatter) { |
255 | 255 | $stmt = $this->pdo->query($query); |
256 | 256 | $stmt->execute(['sA' => $this->storeA, 'sB' => $this->storeB]); |
257 | - while($row = $stmt->fetch(PDO::FETCH_NUM)) { |
|
257 | + while ($row = $stmt->fetch(PDO::FETCH_NUM)) { |
|
258 | 258 | $d = json_decode($row[1], true); |
259 | 259 | $f = json_decode($row[2], true); |
260 | 260 | yield $this->instantiateRow($d, $f, $stringFormatter); |
@@ -278,9 +278,9 @@ discard block |
||
278 | 278 | '; |
279 | 279 | $stmt = $this->pdo->query($query); |
280 | 280 | $stmt->execute(['s' => $this->storeA]); |
281 | - while($row = $stmt->fetch(PDO::FETCH_NUM)) { |
|
281 | + while ($row = $stmt->fetch(PDO::FETCH_NUM)) { |
|
282 | 282 | $row = json_decode($row[0], true); |
283 | - $row = $this->instantiateRow($row, [], function (DiffStorageStoreRowInterface $row) { |
|
283 | + $row = $this->instantiateRow($row, [], function(DiffStorageStoreRowInterface $row) { |
|
284 | 284 | return $this->formatKeyValuePairs($row->getData()); |
285 | 285 | }); |
286 | 286 | yield $row->getData(); |
@@ -294,10 +294,10 @@ discard block |
||
294 | 294 | * @return array |
295 | 295 | */ |
296 | 296 | private function translate(array $data, array $translation = null) { |
297 | - if($translation !== null) { |
|
297 | + if ($translation !== null) { |
|
298 | 298 | $result = []; |
299 | - foreach($data as $key => $value) { |
|
300 | - if(array_key_exists($key, $translation)) { |
|
299 | + foreach ($data as $key => $value) { |
|
300 | + if (array_key_exists($key, $translation)) { |
|
301 | 301 | $key = $translation[$key]; |
302 | 302 | } |
303 | 303 | $result[$key] = $value; |
@@ -372,10 +372,10 @@ discard block |
||
372 | 372 | */ |
373 | 373 | private function formatKeyValuePairs($keyValues) { |
374 | 374 | $keyParts = []; |
375 | - foreach($keyValues as $key => $value) { |
|
375 | + foreach ($keyValues as $key => $value) { |
|
376 | 376 | $value = preg_replace('/\\s+/', ' ', $value); |
377 | - if(strlen($value) > 20) { |
|
378 | - $value = substr($value, 0, 16) . ' ...'; |
|
377 | + if (strlen($value) > 20) { |
|
378 | + $value = substr($value, 0, 16).' ...'; |
|
379 | 379 | } |
380 | 380 | $keyParts[] = sprintf("%s: %s", $key, json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); |
381 | 381 | } |
@@ -77,12 +77,12 @@ discard block |
||
77 | 77 | $keys = array_keys(array_merge($this->row, $this->foreignRow)); |
78 | 78 | $formattedLocalRow = $this->formatRow($localRow); |
79 | 79 | $formattedForeignRow = $this->formatRow($foreignRow); |
80 | - foreach($keys as $key) { |
|
81 | - $conv = function (array $row) use ($key) { |
|
80 | + foreach ($keys as $key) { |
|
81 | + $conv = function(array $row) use ($key) { |
|
82 | 82 | $value = null; |
83 | - if(array_key_exists($key, $row)) { |
|
83 | + if (array_key_exists($key, $row)) { |
|
84 | 84 | $value = $row[$key]; |
85 | - if(array_key_exists($key, $this->converter) && $value !== null) { |
|
85 | + if (array_key_exists($key, $this->converter) && $value !== null) { |
|
86 | 86 | $value = call_user_func($this->converter[$key], $value); |
87 | 87 | } |
88 | 88 | } |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | }; |
91 | 91 | $localValue = call_user_func($conv, $formattedLocalRow); |
92 | 92 | $foreignValue = call_user_func($conv, $formattedForeignRow); |
93 | - if(json_encode($localValue) !== json_encode($foreignValue)) { |
|
93 | + if (json_encode($localValue) !== json_encode($foreignValue)) { |
|
94 | 94 | $diff[$key] = ['local' => $localValue, 'foreign' => $foreignValue]; |
95 | 95 | } |
96 | 96 | } |
@@ -105,16 +105,16 @@ discard block |
||
105 | 105 | */ |
106 | 106 | public function getDiffFormatted(array $fields = null, $format = null) { |
107 | 107 | $diff = $this->getDiff($fields); |
108 | - if($format === null) { |
|
108 | + if ($format === null) { |
|
109 | 109 | $result = []; |
110 | - $formatVal = function ($value) { |
|
110 | + $formatVal = function($value) { |
|
111 | 111 | $value = preg_replace('/\\s+/', ' ', $value); |
112 | - if(strlen($value) > 20) { |
|
113 | - $value = substr($value, 0, 16) . ' ...'; |
|
112 | + if (strlen($value) > 20) { |
|
113 | + $value = substr($value, 0, 16).' ...'; |
|
114 | 114 | } |
115 | 115 | return $value; |
116 | 116 | }; |
117 | - foreach($diff as $fieldName => $values) { |
|
117 | + foreach ($diff as $fieldName => $values) { |
|
118 | 118 | $foreignValue = $formatVal($values['foreign']); |
119 | 119 | $localValue = $formatVal($values['local']); |
120 | 120 | $result[] = sprintf("%s: %s -> %s", $fieldName, $foreignValue, $localValue); |
@@ -130,13 +130,13 @@ discard block |
||
130 | 130 | * @return array |
131 | 131 | */ |
132 | 132 | private function applyOptions(array $row, array $options) { |
133 | - if(count($options) < 1) { |
|
133 | + if (count($options) < 1) { |
|
134 | 134 | return $row; |
135 | 135 | } |
136 | - if(array_key_exists('keys', $options) && is_array($options['keys'])) { |
|
136 | + if (array_key_exists('keys', $options) && is_array($options['keys'])) { |
|
137 | 137 | $row = array_intersect_key($row, array_combine($options['keys'], $options['keys'])); |
138 | 138 | } |
139 | - if(array_key_exists('ignore', $options) && is_array($options['ignore'])) { |
|
139 | + if (array_key_exists('ignore', $options) && is_array($options['ignore'])) { |
|
140 | 140 | $row = array_diff_key($row, array_combine($options['ignore'], $options['ignore'])); |
141 | 141 | } |
142 | 142 | return $row; |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | */ |
149 | 149 | private function formatRow($row) { |
150 | 150 | $schema = $this->converter; |
151 | - $schema = array_map(function () { return null; }, $schema); |
|
151 | + $schema = array_map(function() { return null; }, $schema); |
|
152 | 152 | $row = array_merge($schema, $row); |
153 | 153 | return $row; |
154 | 154 | } |