Skip to content

Commit

Permalink
helpers for creation
Browse files Browse the repository at this point in the history
has method
  • Loading branch information
fantismic committed Jul 22, 2024
1 parent 5b41df3 commit e4c75cb
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 38 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ notificactions.alwayscc.emails

- [Comparing setting](#comparing-setting)
- [Boolean comparition](#boolean-comparition)
- [Array search](#array-search)


### Get all settings
Expand Down Expand Up @@ -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', '[email protected]')
= true
> DynSettings::has('notifications.alwayscc.email', '[email protected]')
= false
> DynSettings::has('notifications.alwayscc.email', '[email protected]', false)
= true
> DynSettings::has('notifications.alwayscc.email', '[email protected]')
= false
```
---

[![Image description](https://i.postimg.cc/SxB7b1T0/fantismic-no-background.png)](https://github.com/fantismic)
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
2 changes: 1 addition & 1 deletion src/Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
| 'blade': always use normal blade
|
*/
'component_blade' => 'wireui',
'component_blade' => 'auto',

/*
|--------------------------------------------------------------------------
Expand Down
191 changes: 155 additions & 36 deletions src/DynSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

0 comments on commit e4c75cb

Please sign in to comment.