From 8a927a039b2e927c249f479afb6c56bc1396ad59 Mon Sep 17 00:00:00 2001 From: BMTmohammedtaha Date: Tue, 26 Sep 2023 21:13:03 +0200 Subject: [PATCH] replace json method by json_ encode/decode --- src/Data/DataOptimizer.php | 19 +++++++++---------- src/Data/DataRules.php | 17 ++++++++++++++--- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/Data/DataOptimizer.php b/src/Data/DataOptimizer.php index dd07536..c0d6b33 100644 --- a/src/Data/DataOptimizer.php +++ b/src/Data/DataOptimizer.php @@ -16,11 +16,6 @@ class DataOptimizer */ private $data; - /** - * @var array $errors The error messages. - */ - private array $errors = []; - /** * @var DataRules $data_rule The data validation rules. */ @@ -93,10 +88,14 @@ public function textToSlug($text, $delimiter = '-') public function applyRule(string $rule, string $key, $value) { - if ($rule === 'json' && $this->isJson($value)) { + if ($rule === 'json_decode' && $this->isJson($value)) { $value = json_decode($value); } + if ($rule === 'json_encode') { + $value = json_encode($value); + } + if ($rule === 'integer' && is_numeric($value)) { $value = (int) $value; } @@ -137,11 +136,11 @@ public function applyRule(string $rule, string $key, $value) } if ($rule === 'strip_tags' && is_string($value)) { - $value = strip_tags($value,$this->data_rule->getAttribute('allowed_tags')); + $value = strip_tags($value, $this->data_rule->getAttribute('allowed_tags')); } if ($rule === 'replace_value') { - $value = $this->data_rule->getAttribute('replace_new_value') ; + $value = $this->data_rule->getAttribute('replace_new_value'); } if ($rule === 'replace_value_by_new') { @@ -193,8 +192,8 @@ public function optimize($rules): array $key = $result->key; } $item = array_merge($item, [$key => $value]); - if(isset($result->remove)){ - unset( $item[$result->remove]); + if (isset($result->remove)) { + unset($item[$result->remove]); } } $data[] = $item; diff --git a/src/Data/DataRules.php b/src/Data/DataRules.php index 3b1eec4..16c21a7 100644 --- a/src/Data/DataRules.php +++ b/src/Data/DataRules.php @@ -123,14 +123,25 @@ public function setRule(string $key, string $rule): self } /** - * Set a rule for a key to validate as JSON. + * Set a rule for a key to validate as JSON encoded. * * @param string $key The key to validate as JSON. * @return self */ - public function json(string $key): self + public function json_encode(string $key): self { - return $this->setRule($key, 'json'); + return $this->setRule($key, 'json_encode'); + } + + /** + * Set a rule for a key to validate as JSON decoded. + * + * @param string $key The key to validate as JSON. + * @return self + */ + public function json_decode(string $key): self + { + return $this->setRule($key, 'json_decode'); } /**