-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d72cee
commit cd463bf
Showing
6 changed files
with
747 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tpetry\QueryExpressions\Function\Date; | ||
|
||
use Illuminate\Contracts\Database\Query\Expression; | ||
use Illuminate\Database\Grammar; | ||
use Illuminate\Database\Query\Expression as QueryExpression; | ||
use Illuminate\Database\Query\Grammars\SqlServerGrammar; | ||
use Tpetry\QueryExpressions\Concerns\IdentifiesDriver; | ||
use Tpetry\QueryExpressions\Concerns\StringizeExpression; | ||
use Tpetry\QueryExpressions\Function\String\Concat; | ||
|
||
class DateFormat implements Expression | ||
{ | ||
use DirectDateFormatTrait; | ||
use EmulatedDateFormatTrait; | ||
use IdentifiesDriver; | ||
use StringizeExpression; | ||
|
||
/** | ||
* @var array<string> | ||
*/ | ||
protected array $unsupportedCharacters = [ | ||
'B', | ||
'c', | ||
'e', | ||
'I', | ||
'L', | ||
'N', | ||
'O', | ||
'P', | ||
'p', | ||
'r', | ||
'S', | ||
'T', | ||
'u', | ||
'v', | ||
'X', | ||
'x', | ||
'z', | ||
'Z', | ||
]; | ||
|
||
public function __construct( | ||
private readonly string|Expression $expression, | ||
private readonly string $format | ||
) { | ||
} | ||
|
||
public function getValue(Grammar $grammar): string | ||
{ | ||
$expressions = $this->buildExpressions($grammar); | ||
|
||
return $this->concatenateExpressions($expressions, $grammar); | ||
} | ||
|
||
/** | ||
* @return non-empty-array<int,Expression> | ||
*/ | ||
protected function buildExpressions(Grammar $grammar): array | ||
{ | ||
$characters = $this->getFormatCharacters(); | ||
|
||
/** @var non-empty-array<string|Expression> $expressions */ | ||
$expressions = array_map(function (string $character) use ($grammar) { | ||
$emulatedCharacter = $this->getEmulatableCharacter($grammar, $character); | ||
$formatCharacter = $this->formatCharacters[$this->identify($grammar)][$character] ?? null; | ||
|
||
if ($emulatedCharacter) { | ||
return $this->getEmulatedExpression($grammar, $emulatedCharacter); | ||
} | ||
|
||
if ($formatCharacter) { | ||
return $formatCharacter; | ||
} | ||
|
||
return $character; | ||
}, $characters); | ||
|
||
return $this->processExpressions($expressions, $grammar); | ||
} | ||
|
||
/** | ||
* @param non-empty-array<string|Expression> $expressions | ||
* @return non-empty-array<int,Expression> | ||
*/ | ||
protected function processExpressions(array $expressions, Grammar $grammar): array | ||
{ | ||
$expressions = array_reduce(array_keys($expressions), function (array $expressions, int $index) use ($grammar) { | ||
$currentExpression = $expressions[$index]; | ||
|
||
if (! is_string($currentExpression)) { | ||
return $expressions; | ||
} | ||
|
||
$nextExpression = $expressions[$index + 1] ?? null; | ||
$isLiteral = mb_strlen($currentExpression) == 1; | ||
$isEscaped = str_starts_with($currentExpression, '\\'); | ||
|
||
// First, process escaped characters | ||
if ($isEscaped) { | ||
$expressions[$index] = new QueryExpression( | ||
$grammar->quoteString(stripslashes($currentExpression)) | ||
); | ||
|
||
return $expressions; | ||
} | ||
|
||
// Next, concatenate adjacent strings | ||
if (is_string($nextExpression)) { | ||
$expressions[$index + 1] = $currentExpression.$nextExpression; | ||
|
||
unset($expressions[$index]); | ||
|
||
return $expressions; | ||
} | ||
|
||
// Since it's a single character it's guaranteed to be a non-formatting literal. | ||
// In SQL Server, calls like FORMAT(column, '-') will just return null. | ||
if ( | ||
$grammar instanceof SqlServerGrammar && | ||
$isLiteral | ||
) { | ||
$expressions[$index] = new QueryExpression( | ||
$grammar->quoteString($currentExpression) | ||
); | ||
} | ||
|
||
// Finally, convert direct date formats to expressions | ||
if (is_string($expressions[$index])) { | ||
$expressions[$index] = $this->getDirectDateFormat($grammar, $expressions[$index]); | ||
} | ||
|
||
return $expressions; | ||
}, $expressions); | ||
|
||
/** @var non-empty-array<Expression> $expressions */ | ||
return array_values($expressions); | ||
} | ||
|
||
/** | ||
* @param non-empty-array<Expression> $expressions | ||
*/ | ||
protected function concatenateExpressions(array $expressions, Grammar $grammar): string | ||
{ | ||
if (count($expressions) == 1) { | ||
return (string) $expressions[0]->getValue($grammar); | ||
} | ||
|
||
return (new Concat($expressions))->getValue($grammar); | ||
} | ||
|
||
/** | ||
* @return array<string> | ||
*/ | ||
protected function getFormatCharacters(): array | ||
{ | ||
$characters = str_split($this->format); | ||
|
||
$characters = array_reduce(array_keys($characters), function (array $characters, int $index) { | ||
if ($characters[$index] == '\\') { | ||
$characters[$index + 1] = $characters[$index].($characters[$index + 1] ?? null); | ||
unset($characters[$index]); | ||
} | ||
|
||
return $characters; | ||
}, $characters); | ||
|
||
array_walk($characters, function (string $character) { | ||
if (in_array($character, $this->unsupportedCharacters)) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'Unsupported format character: %s', | ||
$character, | ||
)); | ||
} | ||
}); | ||
|
||
return $characters; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tpetry\QueryExpressions\Function\Date; | ||
|
||
use Illuminate\Contracts\Database\Query\Expression; | ||
use Illuminate\Database\Grammar; | ||
use Illuminate\Database\Query\Expression as QueryExpression; | ||
|
||
/** | ||
* @property-read string|\Illuminate\Database\Query\Expression $expression | ||
* | ||
* @uses \Tpetry\QueryExpressions\Concerns\IdentifiesDriver | ||
* @uses \Tpetry\QueryExpressions\Concerns\StringizeExpression | ||
*/ | ||
trait DirectDateFormatTrait | ||
{ | ||
/** | ||
* @var array<'mysql'|'sqlite'|'pgsql'|'sqlsrv', array<string, string>> | ||
*/ | ||
protected array $formatCharacters = [ | ||
'mysql' => [ | ||
'A' => '%p', | ||
'd' => '%d', | ||
'D' => '%a', | ||
'F' => '%M', | ||
'H' => '%H', | ||
'h' => '%h', | ||
'i' => '%i', | ||
'j' => '%e', | ||
'l' => '%W', | ||
'm' => '%m', | ||
'M' => '%b', | ||
'n' => '%c', | ||
'o' => '%x', | ||
's' => '%s', | ||
'W' => '%v', | ||
'Y' => '%Y', | ||
'y' => '%y', | ||
], | ||
'sqlite' => [ | ||
'd' => '%d', | ||
'H' => '%H', | ||
'i' => '%M', | ||
'm' => '%m', | ||
's' => '%S', | ||
'U' => '%s', | ||
'w' => '%w', | ||
'Y' => '%Y', | ||
], | ||
'pgsql' => [ | ||
'A' => 'AM', | ||
'd' => 'DD', | ||
'D' => 'Dy', | ||
'h' => 'HH12', | ||
'H' => 'HH24', | ||
'i' => 'MI', | ||
'j' => 'FMDD', | ||
'm' => 'MM', | ||
'M' => 'Mon', | ||
'n' => 'FMMM', | ||
'o' => 'IYYY', | ||
's' => 'SS', | ||
'W' => 'IW', | ||
'y' => 'YY', | ||
'Y' => 'YYYY', | ||
], | ||
'sqlsrv' => [ | ||
'A' => 'tt', | ||
'd' => 'dd', | ||
'D' => 'ddd', | ||
'h' => 'hh', | ||
'H' => 'HH', | ||
'i' => 'mm', | ||
'm' => 'MM', | ||
's' => 'ss', | ||
'Y' => 'yyyy', | ||
], | ||
]; | ||
|
||
protected function getDirectDateFormat(Grammar $grammar, string $format): Expression | ||
{ | ||
return new QueryExpression( | ||
match ($this->identify($grammar)) { | ||
'mysql' => "DATE_FORMAT({$this->stringize($grammar, $this->expression)}, '{$format}')", | ||
'sqlite' => "STRFTIME('{$format}', {$this->stringize($grammar, $this->expression)})", | ||
'pgsql' => "TO_CHAR({$this->stringize($grammar, $this->expression)}, '{$format}')", | ||
'sqlsrv' => "FORMAT({$this->stringize($grammar, $this->expression)}, '{$format}')", | ||
} | ||
); | ||
} | ||
} |
Oops, something went wrong.