Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Handle of DateTime objects in whereIn and whereBetween #466

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/CacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Support\Str;
use Ramsey\Uuid\Uuid;
use UnitEnum;
use DateTime;

class CacheKey
{
Expand Down Expand Up @@ -99,7 +100,12 @@ protected function getColumnClauses(array $where) : string

protected function getCurrentBinding(string $type, $bindingFallback = null)
{
return data_get($this->query->bindings, "{$type}.{$this->currentBinding}", $bindingFallback);
$binding = data_get($this->query->bindings, "{$type}.{$this->currentBinding}", $bindingFallback);
if ($binding instanceof DateTime) {
return $binding->format("Y-m-d-H-i-s");
}

return $binding;
}

protected function getHavingClauses()
Expand Down Expand Up @@ -321,10 +327,7 @@ protected function getValuesClause(array $where = []) : string

protected function getValuesFromWhere(array $where) : string
{
if (array_key_exists("value", $where)
&& is_object($where["value"])
&& get_class($where["value"]) === "DateTime"
) {
if (array_key_exists("value", $where) && $where["value"] instanceof DateTime) {
return $where["value"]->format("Y-m-d-H-i-s");
}

Expand Down Expand Up @@ -442,12 +445,14 @@ protected function recursiveImplode(array $items, string $glue = ",") : string
return $result;
}

private function processEnum(BackedEnum|UnitEnum|Expression|string|null $value): ?string
private function processEnum(BackedEnum|UnitEnum|Expression|DateTime|string|null $value): ?string
{
if ($value instanceof BackedEnum) {
return $value->value;
} elseif ($value instanceof UnitEnum) {
return $value->name;
} elseif ($value instanceof DateTime) {
return $value->format("Y-m-d-H-i-s");
} elseif ($value instanceof Expression) {
return $this->expressionToString($value);
}
Expand Down
Loading