@@ -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 | } |
@@ -59,10 +59,10 @@ discard block |
||
59 | 59 | */ |
60 | 60 | public function addRow(array $data, array $translation = null, $duplicateKeyHandler = null) { |
61 | 61 | $data = $this->translate($data, $translation); |
62 | - if($duplicateKeyHandler === null) { |
|
62 | + if ($duplicateKeyHandler === null) { |
|
63 | 63 | $duplicateKeyHandler = $this->duplicateKeyHandler; |
64 | 64 | } |
65 | - $buildMetaData = function (array $data, array $keys) { |
|
65 | + $buildMetaData = function(array $data, array $keys) { |
|
66 | 66 | $metaData = $data; |
67 | 67 | $metaData = array_diff_key($metaData, array_diff_key($metaData, $keys)); |
68 | 68 | $metaData['___data'] = json_encode($data); |
@@ -73,13 +73,13 @@ discard block |
||
73 | 73 | $metaData = $buildMetaData($data, $this->converter); |
74 | 74 | $this->insertStmt->execute($metaData); |
75 | 75 | } catch (\PDOException $e) { |
76 | - if(strpos($e->getMessage(), 'UNIQUE constraint failed') !== false) { |
|
76 | + if (strpos($e->getMessage(), 'UNIQUE constraint failed') !== false) { |
|
77 | 77 | $metaData = $buildMetaData($data, $this->converter); |
78 | 78 | unset($metaData['___data']); |
79 | 79 | unset($metaData['___sort']); |
80 | 80 | $this->selectStmt->execute($metaData); |
81 | 81 | $oldData = $this->selectStmt->fetch(PDO::FETCH_COLUMN, 0); |
82 | - if($oldData === null) { |
|
82 | + if ($oldData === null) { |
|
83 | 83 | $oldData = []; |
84 | 84 | } else { |
85 | 85 | $oldData = json_decode($oldData, true); |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | * @return $this |
102 | 102 | */ |
103 | 103 | public function addRows($rows, array $translation = null, $duplicateKeyHandler = null) { |
104 | - foreach($rows as $row) { |
|
104 | + foreach ($rows as $row) { |
|
105 | 105 | $this->addRow($row, $translation, $duplicateKeyHandler); |
106 | 106 | } |
107 | 107 | return $this; |
@@ -112,11 +112,11 @@ discard block |
||
112 | 112 | */ |
113 | 113 | public function hasAnyChanges() { |
114 | 114 | /** @noinspection PhpUnusedLocalVariableInspection */ |
115 | - foreach($this->getNewOrChanged() as $_) { |
|
115 | + foreach ($this->getNewOrChanged() as $_) { |
|
116 | 116 | return true; |
117 | 117 | } |
118 | 118 | /** @noinspection PhpUnusedLocalVariableInspection */ |
119 | - foreach($this->getMissing() as $_) { |
|
119 | + foreach ($this->getMissing() as $_) { |
|
120 | 120 | return true; |
121 | 121 | } |
122 | 122 | return false; |
@@ -232,7 +232,7 @@ discard block |
||
232 | 232 | private function query($query) { |
233 | 233 | $stmt = $this->pdo->query($query); |
234 | 234 | $stmt->execute(['sA' => $this->storeA, 'sB' => $this->storeB]); |
235 | - while($row = $stmt->fetch(PDO::FETCH_NUM)) { |
|
235 | + while ($row = $stmt->fetch(PDO::FETCH_NUM)) { |
|
236 | 236 | $d = json_decode($row[1], true); |
237 | 237 | $f = json_decode($row[2], true); |
238 | 238 | yield new DiffStorageStoreRow($d, $f, $this->converter); |
@@ -256,7 +256,7 @@ discard block |
||
256 | 256 | '; |
257 | 257 | $stmt = $this->pdo->query($query); |
258 | 258 | $stmt->execute(['s' => $this->storeA]); |
259 | - while($row = $stmt->fetch(PDO::FETCH_NUM)) { |
|
259 | + while ($row = $stmt->fetch(PDO::FETCH_NUM)) { |
|
260 | 260 | $row = json_decode($row[0], true); |
261 | 261 | $row = new DiffStorageStoreRow($row, [], $this->converter); |
262 | 262 | yield $row->getData(); |
@@ -270,10 +270,10 @@ discard block |
||
270 | 270 | * @return array |
271 | 271 | */ |
272 | 272 | private function translate(array $data, array $translation = null) { |
273 | - if($translation !== null) { |
|
273 | + if ($translation !== null) { |
|
274 | 274 | $result = []; |
275 | - foreach($data as $key => $value) { |
|
276 | - if(array_key_exists($key, $translation)) { |
|
275 | + foreach ($data as $key => $value) { |
|
276 | + if (array_key_exists($key, $translation)) { |
|
277 | 277 | $key = $translation[$key]; |
278 | 278 | } |
279 | 279 | $result[$key] = $value; |
@@ -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 | } |
@@ -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)) { |
|
85 | + if (array_key_exists($key, $this->converter)) { |
|
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,9 +105,9 @@ 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 | - foreach($diff as $fieldName => $values) { |
|
110 | + foreach ($diff as $fieldName => $values) { |
|
111 | 111 | $result[] = sprintf("%s: %s -> %s", $fieldName, $values['foreign'], $values['local']); |
112 | 112 | } |
113 | 113 | return join(', ', $result); |
@@ -121,13 +121,13 @@ discard block |
||
121 | 121 | * @return array |
122 | 122 | */ |
123 | 123 | private function applyOptions(array $row, array $options) { |
124 | - if(count($options) < 1) { |
|
124 | + if (count($options) < 1) { |
|
125 | 125 | return $row; |
126 | 126 | } |
127 | - if(array_key_exists('keys', $options)) { |
|
127 | + if (array_key_exists('keys', $options)) { |
|
128 | 128 | $row = array_intersect_key($row, array_combine($options['keys'], $options['keys'])); |
129 | 129 | } |
130 | - if(array_key_exists('ignore', $options)) { |
|
130 | + if (array_key_exists('ignore', $options)) { |
|
131 | 131 | $row = array_diff_key($row, array_combine($options['ignore'], $options['ignore'])); |
132 | 132 | } |
133 | 133 | return $row; |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | */ |
140 | 140 | private function formatRow($row) { |
141 | 141 | $schema = $this->converter; |
142 | - $schema = array_map(function () { return null; }, $schema); |
|
142 | + $schema = array_map(function() { return null; }, $schema); |
|
143 | 143 | $row = array_merge($schema, $row); |
144 | 144 | return $row; |
145 | 145 | } |
@@ -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($value, 6, '.', ''); }; |
|
132 | + $def[$name] = function($value) { return number_format($value, 6, '.', ''); }; |
|
133 | 133 | break; |
134 | 134 | case 'DOUBLE': |
135 | - $def[$name] = function ($value) { return number_format($value, 12, '.', ''); }; |
|
135 | + $def[$name] = function($value) { return number_format($value, 12, '.', ''); }; |
|
136 | 136 | break; |
137 | 137 | case 'MONEY': |
138 | - $def[$name] = function ($value) { return number_format($value, 2, '.', ''); }; |
|
138 | + $def[$name] = function($value) { return number_format($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 | } |