diff --git a/src/Core/Setting.php b/src/Core/Setting.php index fe3a5c4..49b8d47 100644 --- a/src/Core/Setting.php +++ b/src/Core/Setting.php @@ -1,5 +1,6 @@ count() > 0) { $data = $data->first()->toArray(); } else { - return null; + $data = $model->find()->select(['name', 'value'])->where(['name LIKE' => $key . '.%']); + + if ($data->count() > 0) { + $data = $data->toArray(); + foreach ($data as $dataSet) { + if (self::_serialized($dataSet->value)) { + $dataSet->value = unserialize($dataSet->value); + } + static::$_values = Hash::insert(static::$_values, $dataSet->name, $dataSet->value); + } + + $data['value'] = static::$_values; + } else { + return null; + } } + if (self::_serialized($data['value'])) { + $data['value'] = unserialize($data['value']); + } self::_store($key, $data['value']); $value = $data['value']; @@ -100,6 +132,7 @@ public static function read($key = null, $type = null) } /** + * * write * * Method to write data to database. @@ -118,9 +151,9 @@ public static function read($key = null, $type = null) * 'editable' => 0, * ] * - * @param string $key Key of the value. Must contain an prefix. - * @param mixed $value The value of the key. - * @param array $options Options array. + * @param string $key Key of the value. Must contain an prefix. + * @param mixed $value The value of the key. + * @param array $options Options array. * @return void|bool */ public static function write($key, $value = null, $options = []) @@ -139,6 +172,10 @@ public static function write($key, $value = null, $options = []) $options = Hash::merge($_options, $options); $model = self::model(); + + if (is_array($value) && !empty($value)) { + $value = serialize($value); + } if (self::check($key)) { if ($options['overrule']) { @@ -165,6 +202,7 @@ public static function write($key, $value = null, $options = []) } /** + * * check * * Checks if an specific key exists. @@ -196,6 +234,7 @@ public static function check($key) } /** + * * model * * Returns an instance of the Configurations-model (Table). @@ -218,13 +257,14 @@ public static function model($model = null) } /** + * * register * * Registers a setting and its default values. * - * @param string $key The key. - * @param mixed $value The default value. - * @param array $data Custom data. + * @param string $key The key. + * @param mixed $value The default value. + * @param array $data Custom data. * @return void */ public static function register($key, $value, $data = []) @@ -234,6 +274,10 @@ public static function register($key, $value, $data = []) } self::autoLoad(); + + if (is_array($value)) { + $value = seralize($value); + } $_data = [ 'value' => $value, @@ -254,10 +298,11 @@ public static function register($key, $value, $data = []) } /** + * * options * - * @param string $key Key for options. - * @param array $value Options to use. + * @param string $key Key for options. + * @param array $value Options to use. * @return mixed */ public static function options($key, $value = null) @@ -278,6 +323,7 @@ public static function options($key, $value = null) } /** + * * autoLoad * * AutoLoad method. @@ -305,6 +351,7 @@ public static function autoLoad() } /** + * * clear * * Clears all settings out of the class. Settings @@ -320,12 +367,13 @@ public static function clear($reload = false) } /** + * * _store * * Stores recent data in the $_data-variable. * - * @param string $key The key. - * @param mixed $value The value. + * @param string $key The key. + * @param mixed $value The value. * @return void */ protected static function _store($key, $value) @@ -334,6 +382,7 @@ protected static function _store($key, $value) } /** + * * _tableExists * * @return bool @@ -348,4 +397,85 @@ protected static function _tableExists() } return false; } + + /** + * _serialized + * + * @param string $value The value. + * @param mixed $result The result (null default). + * @return bool + */ + protected static function _serialized($value, $result = null) + { + if (! is_string($value)) { + return false; + } + + if ('b:0;' === $value) { + $result = false; + return true; + } + $length = strlen($value); + $end = ''; + + if (isset($value[0])) { + switch ($value[0]) { + case 's': + if ('"' !== $value[$length - 2]) { + return false; + } + // no break + case 'b': + // no break + case 'i': + // no break + case 'd': + $end .= ';'; + // no break + case 'a': + // no break + case 'O': + $end .= '}'; + + if (':' !== $value[1]) { + return false; + } + + switch ($value[2]) { + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: + case 8: + case 9: + break; + + default: + return false; + } + // break appled in embedded switch + case 'N': + $end .= ';'; + + if ($value[$length - 1] !== $end[0]) { + return false; + } + break; + + default: + return false; + } + } + + if (( $result = unserialize($value) ) === false) { + $result = null; + return false; + } + + return true; + } } diff --git a/tests/TestCase/Core/SettingTest.php b/tests/TestCase/Core/SettingTest.php index 855c322..cdb69c3 100644 --- a/tests/TestCase/Core/SettingTest.php +++ b/tests/TestCase/Core/SettingTest.php @@ -148,6 +148,16 @@ public function testRead() $this->assertEquals(1, Setting::read('App.UniqueReadvalue')); $this->assertEquals(1, Setting::read('App.UniqueReadvalue', 'integer')); $this->assertEquals('1', Setting::read('App.UniqueReadvalue', 'string')); + + $data = [ + 'name' => 'App.UniqueArray', + 'value' => 'a:4:{i:0;i:1;i:2;i:3;i:3;s:3:"one";s:3:"two";s:5:"three";}' + ]; + + $this->Settings->save($this->Settings->newEntity($data)); + $read = Setting::read('App.UniqueArray'); + $this->assertGreaterThan(0, count($read)); + $this->assertEquals([1, 2 => 3, 'one', 'two' => 'three'], Setting::read('App.UniqueArray')); } /** @@ -197,6 +207,30 @@ public function testWrite() $this->assertEquals(1, $value->editable); $this->assertEquals(20, $value->weight); $this->assertEquals(1, $value->autoload); + + Setting::write('App.WriteArray', [1, 2 => 3, 'one', 'two' => 'three'], [ + 'description' => 'Short description', + 'type' => 'array', + 'editable' => true, + 'options' => [ + 1 => 'One', + 2 => 'Two' + ], + 'weight' => 20, + 'autoload' => true, + ]); + + $this->assertEquals(3, $this->Settings->find('all')->count()); + + $value = $this->Settings->get(3); + $this->assertEquals('App.WriteArray', $value->name); + $this->assertEquals('App.WriteArray', $value->key); + $this->assertEquals('a:4:{i:0;i:1;i:2;i:3;i:3;s:3:"one";s:3:"two";s:5:"three";}', $value->value); + $this->assertEquals('Short description', $value->description); + $this->assertEquals('array', $value->type); + $this->assertEquals(1, $value->editable); + $this->assertEquals(20, $value->weight); + $this->assertEquals(1, $value->autoload); } /**