Skip to content

Commit

Permalink
replace json method by json_ encode/decode
Browse files Browse the repository at this point in the history
  • Loading branch information
BMTmohammedtaha committed Sep 26, 2023
1 parent 2cb6e3b commit 8a927a0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
19 changes: 9 additions & 10 deletions src/Data/DataOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 14 additions & 3 deletions src/Data/DataRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down

0 comments on commit 8a927a0

Please sign in to comment.