Skip to content

Commit

Permalink
Refactor and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bramato committed Nov 21, 2021
1 parent a4083b6 commit 6304e2a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 82 deletions.
25 changes: 1 addition & 24 deletions src/CastSettings.php
Expand Up @@ -12,30 +12,6 @@ public static function boolean($value)
return $cast->apply($value, ['boolean']);
}

public static function booleanFromString($value)
{
switch ($value) {
case 'true':
return true;
case 'false':
return false;
default:
throw new \Exception('Cast Boolean from String is not possible. Value is not correct.');
}
}

public static function booleanFromInt($value)
{
switch ($value) {
case '1':
return 1;
case '0':
return 0;
default:
throw new \Exception('Cast Boolean from String is not possible. Value is not correct.');
}
}

public static function string($value)
{
return $value.'';
Expand All @@ -45,6 +21,7 @@ public static function integer($value)
{
return (int)$value;
}

public static function numeric($value)
{
return (int)$value;
Expand Down
73 changes: 16 additions & 57 deletions src/Settings.php
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Padosoft\Laravel\CmsAdmin\Presenters\PresenterBase;
Expand Down Expand Up @@ -79,60 +80,34 @@ protected function validate($value)
return $value;
}

$rule = $this->validation_rules;
$validation_rules = $this->validation_rules;
$type = typeOfValueFromValidationRule($validation_rules);
//recupera la validazione dal config se presente
//il valore validate non è obbligatorio può essere un valore utilizzabile con Validate o un regex
if (config('padosoft-settings.cast.' . $rule . '.validate') !== null) {
$rule = config('padosoft-settings.cast.' . $rule . '.validate');
if (config('padosoft-settings.cast.' . $type . '.validate') !== null) {
$ruleString = config('padosoft-settings.cast.' . $type . '.validate');
}else{
$ruleString = $validation_rules;
}
//Se regex trasforma in array altrimenti crea un array esplodendo sul carattere pipe
if (str_contains($rule, 'regex:')) {
$rule = array($rule);
if (str_contains($ruleString, 'regex:')) {
$rule = array($ruleString);
} else {
$rule = explode('|', $rule);
$rule = explode('|', $ruleString);
}
if (!str_contains($ruleString, 'regex:') && !method_exists(Validator::class, 'validate'.$type)) {
Log::error('Validation method does not exists for settings key: "'. $this->key. '". Miss Method "validate'.$type. '" or config value: "cast.'.$type.'.validate".');
return ($value);
}
try {
Validator::make(['value' => $value], ['value' => $rule])->validate();
//Effettua un cast dinamico del valore
return $this->cast($value);
return cast($value, $type);
} catch (ValidationException $e) {
throw new \Exception($value . ' is not a valid value.' . 'line:' . $e->getLine());
}
}

/**
* Effettua un cast dinamico di value
* Il tipo di cast da utilizzare viene recuperato se presente da config
* Se non trova il valore da config cerca nei tipi di cast base più comuni
* I cast possono essere sovrascritti da config
* @return bool|float|\Illuminate\Support\Collection|int|mixed|object|string
* @throws \Exception
*/
protected function cast($value)
{
$cast = config('padosoft-settings.cast.' . $this->validation_rules);
//Se esiste la classe e il metodo indicati per il cast in config li utilizza
//Altrimenti prosegue.
$class = $cast['class'] ?? CastSettings::class;
$method = $cast['method'] ?? 'execute';
if ($cast !== null && class_exists($class) && method_exists($class, $method)) {
return $class::$method($value);
}
switch ($this->typeOfValue) {
case 'boolean':
return CastSettings::boolean($value);
case 'booleanFromString':
return CastSettings::booleanFromString($value);
case 'booleanFromInt':
return CastSettings::booleanFromInt($value);
case 'numeric':
return CastSettings::numeric($value);
default:
//Se non trova niente effettua un cast in string
return CastSettings::string($value);
}
}



/**
Expand Down Expand Up @@ -170,22 +145,6 @@ public function getIsValidAttribute(): bool
*/
public function getTypeOfValueAttribute()
{
if (str_contains($this->validation_rules, 'regex')) {
return 'custom';
}
$validation_base = 'string';
$typeCheck = ['boolean','integer','numeric','string'];
if (config('padosoft-settings.cast') !== null && is_array(config('padosoft-settings.cast'))) {
$keys = array_keys(config('padosoft-settings.cast'));
$typeCheck = array_merge($keys, $typeCheck);
}
$arrayValidate = explode('|', $this->validation_rules);
foreach ($typeCheck as $type) {
if (in_array($type, $arrayValidate)) {
$validation_base = $type;
break;
};
}
return $validation_base;
return typeOfValueFromValidationRule($this->validation_rules);
}
}
3 changes: 3 additions & 0 deletions src/SettingsManager.php
Expand Up @@ -64,6 +64,9 @@ protected function getMemoryValue($key)
)
) {
return settings()->settings[$key]['value'];
//Ritorn valore con cast automatico
//return cast(settings()->settings[$key]['value'], settings()->settings[$key]['validation_rule'], typeOfValueFromValidationRule(settings()->settings[$key]['validation_rule']));

}
try {
return Crypt::decrypt(settings()->settings[$key]['value']);
Expand Down
58 changes: 57 additions & 1 deletion src/helpers.php
@@ -1,8 +1,10 @@
<?php

/**
* Copyright (c) Padosoft.com 2018.
*/

use Padosoft\Laravel\Settings\CastSettings;

if (!function_exists('settings')) {
/**
Expand All @@ -23,7 +25,6 @@ function settings($key = null, $default = null)

return app('Padosoft\Laravel\Settings\SettingsManager')->get($key, $default);
}

}


Expand All @@ -43,4 +44,59 @@ function hasDbSettingsTable()
return \Schema::hasTable('settings');
}


if (!function_exists('cast')) {
/**
* Effettua un cast dinamico di value
* Il tipo di cast da utilizzare viene recuperato se presente da config
* Se non trova il valore da config cerca nei tipi di cast base più comuni
* I cast possono essere sovrascritti da config
* @return bool|float|\Illuminate\Support\Collection|int|mixed|object|string
* @throws \Exception
*/
function cast($value, $type_of_value)
{

$cast = config('padosoft-settings.cast.' . $type_of_value);
//Se esiste la classe e il metodo indicati per il cast in config li utilizza
//Altrimenti prosegue.
$class = $cast['class'] ?? CastSettings::class;
$method = $cast['method'] ?? 'execute';
if ($cast !== null && class_exists($class) && method_exists($class, $method)) {
return $class::$method($value);
}
switch ($type_of_value) {
case 'boolean':
return CastSettings::boolean($value);
case 'numeric':
return CastSettings::numeric($value);
default:
//Se non trova niente effettua un cast in string
return CastSettings::string($value);
}
}
}

if (!function_exists('typeOfValue')) {
function typeOfValueFromValidationRule($validation_rules)
{
if (str_contains($validation_rules, 'regex')) {
return 'custom';
}
$validation_base = 'string';
$typeCheck = ['boolean','integer','numeric','string'];
if (config('padosoft-settings.cast') !== null && is_array(config('padosoft-settings.cast'))) {
$keys = array_keys(config('padosoft-settings.cast'));
$typeCheck = array_merge($keys, $typeCheck);
}
$arrayValidate = explode('|', $validation_rules);
foreach ($typeCheck as $type) {
if (in_array($type, $arrayValidate)) {
$validation_base = $type;
break;
};
}
return $validation_base;
}
}
}

0 comments on commit 6304e2a

Please sign in to comment.