Skip to content

Commit

Permalink
Pull request for issues #10 and #11. (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
agrism committed Oct 27, 2023
1 parent 12b50c3 commit e143597
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 44 deletions.
4 changes: 3 additions & 1 deletion src/Builder.php
Expand Up @@ -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, ':');
Expand Down
82 changes: 39 additions & 43 deletions src/Model.php
Expand Up @@ -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())));
Expand All @@ -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.
*
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e143597

Please sign in to comment.