From bd11529b8b36857fdb59649944856261fea4fc65 Mon Sep 17 00:00:00 2001 From: Mohamed Safwan Date: Wed, 26 Oct 2022 16:58:08 +0530 Subject: [PATCH] Fix retrieval of incorrect keys - anlutro/laravel-settings#165 Fix retrieval of incorrect keys when numeric keys are stored and fetched using `SettingsStore::all()`. Usage of `array_merge` in `SettingsStore::load()` causes the numeric keys in the resultant array to be renumbered. Using `+` operator to merge two arrays with numeric keys should be a safer option. --- src/SettingStore.php | 2 +- tests/functional/AbstractFunctionalTest.php | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/SettingStore.php b/src/SettingStore.php index d5e8286..3375158 100644 --- a/src/SettingStore.php +++ b/src/SettingStore.php @@ -231,7 +231,7 @@ public function load($force = false) if (!$this->loaded || $force) { $this->data = $this->readData(); $this->persistedData = $this->data; - $this->data = array_merge($this->updatedData, $this->data); + $this->data = $this->updatedData + $this->data; $this->loaded = true; } } diff --git a/tests/functional/AbstractFunctionalTest.php b/tests/functional/AbstractFunctionalTest.php index 041707a..954446f 100644 --- a/tests/functional/AbstractFunctionalTest.php +++ b/tests/functional/AbstractFunctionalTest.php @@ -145,4 +145,15 @@ public function defaults_are_respected() $this->assertStoreEquals($store, ['foo' => 'bar']); $this->assertStoreKeyEquals($store, ['foo', 'bar'], ['foo' => 'bar', 'bar' => 'default']); } + + /** @test */ + public function numeric_keys_are_retrieved_correctly() + { + $store = $this->getStore(); + $store->set('1234', 'foo'); + $store->set('9876', 'bar'); + $store->load(true); + $this->assertStoreEquals($store, ['1234' => 'foo', '9876' => 'bar']); + $this->assertStoreKeyEquals($store, ['1234', '9876'], ['1234' => 'foo', '9876' => 'bar']); + } }