From e1435977f7aa1823b055362498323fb557be8115 Mon Sep 17 00:00:00 2001 From: agris <33702214+agrism@users.noreply.github.com> Date: Fri, 27 Oct 2023 10:03:12 +0300 Subject: [PATCH] Pull request for issues #10 and #11. (#12) --- src/Builder.php | 4 ++- src/Model.php | 82 +++++++++++++++++++++++-------------------------- 2 files changed, 42 insertions(+), 44 deletions(-) diff --git a/src/Builder.php b/src/Builder.php index 1b2191d..ecd608b 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -359,7 +359,9 @@ public function compileHashByFields(array $attributes) $stringKey = ''; foreach ($listKey as $key) { - $stringKey .= $key . ':' . ($attributes[$key] ?? "*") . ':'; + $attributeValue = $attributes[$key] ?? '*'; + $attributeValue = is_string($attributeValue) ? $attributeValue : json_encode($attributeValue); + $stringKey .= $key . ':' . $attributeValue . ':'; } return $this->model->getTable() . ":" . rtrim($stringKey, ':'); diff --git a/src/Model.php b/src/Model.php index 32a99df..6dba2a8 100644 --- a/src/Model.php +++ b/src/Model.php @@ -505,15 +505,7 @@ protected function performUpdate(Builder $build) $attributes = $this->getAttributesForInsert(); if ($this->isValidationKeyAndSubKeys($attributes)) { $attributes = collect($attributes)->map(function ($item, $key) { - // Cast the attribute if necessary - $item = $this->hasCast($key) ? $this->castAttribute($key, $item) : $item; - - // If the attribute is a Carbon instance, format it using the model's date format - if ($item instanceof Carbon) { - $item = $item->format($this->getDateFormat()); - } - - return (string) $item; + return (string) $this->castAttributeBeforeSave($key, $item); })->toArray(); $keyOrigin = $build->compileHashByFields($this->parseAttributeKeyBooleanToInt(($this->getOriginal()))); @@ -532,6 +524,42 @@ protected function performUpdate(Builder $build) return false; } + /** + * Casts and prepares an attribute value before saving it to the database. + * + * @param string $key + * @param mixed $value + * + * @return mixed The + */ + protected function castAttributeBeforeSave($key, $value) { + // Cast the attribute if necessary + $value = $this->hasCast($key) ? $this->castAttribute($key, $value) : $value; + + // If the attribute is a Carbon instance, format it using the model's date format + if ($value instanceof Carbon) { + $value = $value->format($this->getDateFormat()); + } + + // If the attribute is an array, encode it to JSON + if (is_array($value)) { + $value = json_encode($value); + } + + // If the attribute is a boolean, cast it to an integer + if (is_bool($value)) { + $value = (int) filter_var($value, FILTER_VALIDATE_BOOLEAN); + } + + // If the attribute is enum castable, extract its value + if ($this->isEnumCastable($key)) { + $value = $value->value; + } + + // Return the transformed and casted attribute value + return $value; + } + /** * Perform a model insert operation. * @@ -569,23 +597,7 @@ protected function performInsert(Builder $build) if ($this->isValidationKeyAndSubKeys($attributes)) { $attributes = collect($attributes)->map(function ($item, $key) { - // Cast the attribute if necessary - $item = $this->hasCast($key) ? $this->castAttribute($key, $item) : $item; - - // If the attribute is a Carbon instance, format it using the model's date format - if ($item instanceof Carbon) { - $item = $item->format($this->getDateFormat()); - } - - if (is_array($item)) { - $item = json_encode($item); - } - - if (is_bool($item)) { - $item = (int) filter_var($item, FILTER_VALIDATE_BOOLEAN); - } - - return (string) $item; + return (string) $this->castAttributeBeforeSave($key, $item); })->toArray(); $keyInsert = $build->compileHashByFields($this->parseAttributeKeyBooleanToInt($attributes)); @@ -657,23 +669,7 @@ public static function insert(array $dataInsert, Redis $hasTransaction = null) } $inserts[$key] = collect($attributes)->map(function ($item, $key) use ($model) { - // Cast the attribute if necessary - $item = $model->hasCast($key) ? $model->castAttribute($key, $item) : $item; - - // If the attribute is a Carbon instance, format it using the model's date format - if ($item instanceof Carbon) { - $item = $item->format($model->getDateFormat()); - } - - if (is_array($item)) { - $item = json_encode($item); - } - - if (is_bool($item)) { - $item = (int) filter_var($item, FILTER_VALIDATE_BOOLEAN); - } - - return (string) $item; + return (string) $this->castAttributeBeforeSave($key, $item); })->toArray(); } else { return false;