From e4c75cb0ca7595aec6f3a339757f9beae424523a Mon Sep 17 00:00:00 2001 From: fantismic Date: Mon, 22 Jul 2024 13:08:10 -0300 Subject: [PATCH] helpers for creation has method --- README.md | 16 ++++ composer.json | 2 +- src/Config/config.php | 2 +- src/DynSettings.php | 191 ++++++++++++++++++++++++++++++++++-------- 4 files changed, 173 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index ff0d65e..2636f2b 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,7 @@ notificactions.alwayscc.emails - [Comparing setting](#comparing-setting) - [Boolean comparition](#boolean-comparition) + - [Array search](#array-search) ### Get all settings @@ -408,6 +409,21 @@ notificactions.alwayscc.emails = false ``` +### Array search +**DynSettings::has(** *(string)* **$key**, *(array)* **$value,** [(bool) strict = false]**)**: (bool) +``` +> DynSettings::has('notifications.alwayscc.email', 'admin@app.com') += true + +> DynSettings::has('notifications.alwayscc.email', 'admin@APP.com') += false + +> DynSettings::has('notifications.alwayscc.email', 'admin@APP.com', false) += true + +> DynSettings::has('notifications.alwayscc.email', 'not-in-list-mail@app.com') += false +``` --- [![Image description](https://i.postimg.cc/SxB7b1T0/fantismic-no-background.png)](https://github.com/fantismic) diff --git a/composer.json b/composer.json index 6868f1f..1b51828 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "fantismic/dynamic-settings", - "version": "1.1.0", + "version": "1.1.1", "description": "This package creates custom dynamic settings controlled by the user.", "license": "MIT", "keywords": [ diff --git a/src/Config/config.php b/src/Config/config.php index 44ea7f3..44787a0 100644 --- a/src/Config/config.php +++ b/src/Config/config.php @@ -13,7 +13,7 @@ | 'blade': always use normal blade | */ - 'component_blade' => 'wireui', + 'component_blade' => 'auto', /* |-------------------------------------------------------------------------- diff --git a/src/DynSettings.php b/src/DynSettings.php index 93195f0..e839ff8 100644 --- a/src/DynSettings.php +++ b/src/DynSettings.php @@ -52,12 +52,101 @@ public function getTypes(): array { * @param string $group * @param string $association * @param string $description - * @return bool + * @return mixed */ - public function add(string $key, mixed $value, string $type, string $name, string $group='General', string $association='Misc',string $description=null): bool { + public function add(string $key, mixed $value, string $type, string $name, string $group='General', string $association='Misc',string $description=null): mixed { return DynamicSetting::add($key,$value,$type,$name,$group,$association,$description); } + /** + * addBool + * + * @param string $key + * @param bool $value + * @param string $name + * @param string $group + * @param string $association + * @param string $description + * @return mixed + */ + public function addBool(string $key, string $name, bool $value, string $group, string $association, string $description = null): mixed { + return DynamicSetting::add($key,$value,'boolean',$group,$association,$description); + } + + /** + * addString + * + * @param string $key + * @param string $value + * @param string $name + * @param string $group + * @param string $association + * @param string $description + * @return mixed + */ + public function addString(string $key, string $name, string $value, string $group, string $association, string $description = null): mixed { + return DynamicSetting::add($key,$value,'string',$group,$association,$description); + } + + /** + * addInt + * + * @param string $key + * @param int $value + * @param string $name + * @param string $group + * @param string $association + * @param string $description + * @return mixed + */ + public function addInt(string $key, string $name, int $value, string $group, string $association, string $description = null): mixed { + return DynamicSetting::add($key,$value,'integer',$group,$association,$description); + } + + /** + * addFloat + * + * @param string $key + * @param float $value + * @param string $name + * @param string $group + * @param string $association + * @param string $description + * @return mixed + */ + public function addFloat(string $key, string $name, float $value, string $group, string $association, string $description = null): mixed { + return $this->addDouble($key,$value,'double',$group,$association,$description); + } + + /** + * addDouble + * + * @param string $key + * @param double $value + * @param string $name + * @param string $group + * @param string $association + * @param string $description + * @return mixed + */ + public function addDouble(string $key, string $name, float $value, string $group, string $association, string $description = null): mixed { + return DynamicSetting::add($key,$value,'double',$group,$association,$description); + } + + /** + * addArray + * + * @param string $key + * @param array $value + * @param string $name + * @param string $group + * @param string $association + * @param string $description + * @return mixed + */ + public function addArray(string $key, string $name, array $value, string $group, string $association, string $description = null): mixed { + return DynamicSetting::add($key,$value,'array',$group,$association,$description); + } /** * set @@ -125,40 +214,6 @@ public function all() { return DynamicSetting::all()->toArray(); } - public function should($key) { - $data = $this->getKeyData($key); - $keyValue = json_decode($data->value,true); - if ($data->type != 'boolean') { - throw new Exception("This method only works with boolean type settings."); - } - - return $keyValue; - } - - public function is($key,$value) - { - $data = $this->getKeyData($key); - $keyValue = json_decode($data->value,true); - - switch ($data->type) { - case 'boolean': - return $value == $keyValue; - break; - - case 'string': - return strtolower($value) == strtolower($keyValue); - break; - - case 'array': - case 'integer': - case 'double': - return $value == $keyValue; - break; - } - - return $data; - } - public function saveArray($arr) { $settings_all = $this->getModel(); $form_settings_dot = Arr::dot($arr); @@ -228,4 +283,68 @@ public function getByGroup($group) { return DynamicSetting::getByGroup($group); } + + /** + * should + * + * get value of boolean setting to assert if action should be done based on the site config + * + * @param string $key + * @return mixed + */ + public function should(string $key): mixed { + $data = $this->getKeyData($key); + if ($data->type != 'boolean') { + throw new Exception("This method only works with boolean type settings."); + } + + return $this->get($key); + } + + /** + * is + * + * @param string $key + * @param mixed $value + * @return bool + */ + public function is($key,$value): bool + { + $data = $this->getKeyData($key); + $keyValue = $this->get($key); + + switch ($data->type) { + case 'boolean': + return $value == $keyValue; + break; + + case 'string': + return strtolower($value) == strtolower($keyValue); + break; + + case 'array': + case 'integer': + case 'double': + return $value == $keyValue; + break; + } + + return false; + } + + public function has($key, $needle, $strict = true): bool { + $data = $this->getKeyData($key); + if ($data->type != "array") { + throw new Exception("This method only works with array type settings."); + } + $value = $this->get($key); + + if (!$strict) { + $value = array_map('strtolower',$value); + $needle = strtolower($needle); + } + + + return in_array($needle,$value); + } } \ No newline at end of file