Skip to content

Commit

Permalink
auto parse boolean to int for attribute is boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
alvin0 committed May 26, 2023
1 parent 299dc01 commit 5f3eb7d
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/Model.php
Expand Up @@ -516,8 +516,8 @@ protected function performUpdate(Builder $build)
return (string) $item;
})->toArray();

$keyOrigin = $build->compileHashByFields($this->getOriginal());
$keyNew = $build->compileHashByFields($attributes);
$keyOrigin = $build->compileHashByFields($this->parseAttributeKeyBooleanToInt(($this->getOriginal())));
$keyNew = $build->compileHashByFields($this->parseAttributeKeyBooleanToInt($attributes));
$build->getRepository()->updateRedisHashes($keyOrigin, $attributes, $keyNew);

$this->exists = true;
Expand Down Expand Up @@ -580,7 +580,7 @@ protected function performInsert(Builder $build)
return (string) $item;
})->toArray();

$keyInsert = $build->compileHashByFields($attributes);
$keyInsert = $build->compileHashByFields($this->parseAttributeKeyBooleanToInt($attributes));
$build->getRepository()->insertRedisHashes($keyInsert, $attributes);
} else {
throw new RedisModelException("Primary key and sub key values are required");
Expand Down Expand Up @@ -1182,12 +1182,30 @@ protected function isKeyExist()
protected function isValidationKeyAndSubKeys($attributes)
{
$listKey = array_merge([$this->getKeyName()], $this->getSubKeys());

foreach ($listKey as $key) {
if (!isset($attributes[$key]) || empty($attributes[$key])) {
if (!isset($attributes[$key]) ||
(isset($this->getCasts()[$key]) && $this->getCasts()[$key] != 'boolean' && empty($attributes[$key]))) {
return false;
}
}

return true;
}

/**
* @param array $value
*
* @return int
*/
private function parseAttributeKeyBooleanToInt($attributes)
{
foreach ($attributes as $key => $value) {
if (isset($this->getCasts()[$key]) && $this->getCasts()[$key] == 'boolean') {
$attributes[$key] = (int) filter_var($value, FILTER_VALIDATE_BOOLEAN);
}
}

return $attributes;
}
}

0 comments on commit 5f3eb7d

Please sign in to comment.