-
Notifications
You must be signed in to change notification settings - Fork 22
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 53842bf
Showing
6 changed files
with
593 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,118 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tpetry\QueryExpressions\Function\Date; | ||
|
||
use Illuminate\Contracts\Database\Query\Expression; | ||
use Illuminate\Database\Grammar; | ||
use Tpetry\QueryExpressions\Concerns\IdentifiesDriver; | ||
use Tpetry\QueryExpressions\Concerns\StringizeExpression; | ||
use Tpetry\QueryExpressions\Function\String\Concat; | ||
use Tpetry\QueryExpressions\Value\Value; | ||
|
||
class DateFormat implements Expression | ||
{ | ||
use DirectDateFormat; | ||
use EmulatedDateFormat; | ||
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 | ||
{ | ||
/** @var non-empty-array<int, Expression> $expressions */ | ||
$expressions = []; | ||
|
||
$characters = $this->getFormatCharacters(); | ||
|
||
foreach ($characters as $character) { | ||
$emulatedCharacter = $this->getEmulatableCharacter($grammar, $character); | ||
$formatCharacter = $this->formatCharacters[$this->identify($grammar)][$character] ?? null; | ||
|
||
if ($emulatedCharacter) { | ||
$expressions[] = $this->getEmulatedExpression($grammar, $emulatedCharacter); | ||
} elseif ($formatCharacter) { | ||
$expressions[] = $this->getDateFormatExpression($grammar, $character); | ||
} else { | ||
$expressions[] = $this->getCharacterExpression($character); | ||
} | ||
} | ||
|
||
return count($expressions) == 1 ? | ||
(string) $expressions[0]->getValue($grammar) : (new Concat($expressions))->getValue($grammar); | ||
} | ||
|
||
protected function getCharacterExpression(string $character): Expression | ||
{ | ||
$isEscaped = str_starts_with($character, '\\') && strlen($character) > 1; | ||
|
||
return new Value( | ||
$isEscaped ? substr($character, 1) : $character, | ||
); | ||
} | ||
|
||
/** | ||
* @return array<int, 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,94 @@ | ||
<?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 DirectDateFormat | ||
{ | ||
/** | ||
* @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' => [ | ||
'A' => '%p', | ||
'd' => '%d', | ||
'H' => '%H', | ||
'i' => '%M', | ||
'm' => '%m', | ||
's' => '%S', | ||
'U' => '%s', | ||
'Y' => '%Y', | ||
], | ||
'pgsql' => [ | ||
'A' => 'AM', | ||
'd' => 'DD', | ||
'D' => 'Dy', | ||
'h' => 'HH12', | ||
'H' => 'HH24', | ||
'i' => 'MI', | ||
'j' => 'FMDD', | ||
'm' => 'MM', | ||
'M' => 'Mon', | ||
'n' => 'FMMM', | ||
'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 getDateFormatExpression(Grammar $grammar, string $character): Expression | ||
{ | ||
$formatCharacter = $this->formatCharacters[$this->identify($grammar)][$character]; | ||
|
||
return new QueryExpression( | ||
match ($this->identify($grammar)) { | ||
'mysql' => "DATE_FORMAT({$this->stringize($grammar, $this->expression)}, '{$formatCharacter}')", | ||
'sqlite' => "STRFTIME('{$formatCharacter}', {$this->stringize($grammar, $this->expression)})", | ||
'pgsql' => "TO_CHAR({$this->stringize($grammar, $this->expression)}, '{$formatCharacter}')", | ||
'sqlsrv' => "FORMAT({$this->stringize($grammar, $this->expression)}, '{$formatCharacter}')", | ||
} | ||
); | ||
} | ||
} |
Oops, something went wrong.