Skip to content

Commit

Permalink
Merge pull request #18 from gpmpart/master
Browse files Browse the repository at this point in the history
Add a check before casting $value to string when firing events
  • Loading branch information
jpkleemans authored Dec 14, 2023
2 parents 83b99df + 59ec28b commit de18768
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/AttributeEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private function fireAttributeEvents(): void
|| $expected === 'false' && $value === false
|| is_numeric($expected) && Str::contains($expected, '.') && $value === (float) $expected // float
|| is_numeric($expected) && $value === (int) $expected // int
|| (string) $value === $expected
|| $this->canBeCastedToString($value) && (string) $value === $expected
) {
$this->fireModelEvent($change, false);
}
Expand Down Expand Up @@ -137,4 +137,16 @@ private function hasAccessor(string $attribute): bool

return false;
}

private function canBeCastedToString($value): bool
{
if ($value instanceof \UnitEnum) {
return false;
}

return is_scalar($value)
|| is_null($value)
|| is_array($value)
|| is_object($value) && method_exists($value, '__toString');
}
}

0 comments on commit de18768

Please sign in to comment.