Add Types

Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
davidarendsen 2022-08-15 21:22:01 +00:00
commit a50e33ba04
5 changed files with 93 additions and 1 deletions

View file

@ -3,6 +3,7 @@
namespace Arendsen\FluxQueryBuilder;
use DateTime;
use Arendsen\FluxQueryBuilder\Type\Time;
class Formatters
{
@ -34,6 +35,6 @@ class Formatters
public static function dateTimeToString(DateTime $dateTime): string
{
return 'time(v: ' . $dateTime->format('Y-m-d\TH:i:s\Z') . ')';
return new Type($dateTime);
}
}

29
src/Type.php Normal file
View file

@ -0,0 +1,29 @@
<?php
namespace Arendsen\FluxQueryBuilder;
use Arendsen\FluxQueryBuilder\Type\Time;
use DateTime;
class Type
{
public function __construct($value)
{
$this->value = $value;
}
public function __toString(): string
{
switch (gettype($this->value)) {
case 'string':
return '"' . $this->value . '"';
case 'object':
if ($this->value instanceof DateTime) {
return new Time($this->value);
}
return $this->value->__toString();
default:
return (string)$this->value;
}
}
}

18
src/Type/Time.php Normal file
View file

@ -0,0 +1,18 @@
<?php
namespace Arendsen\FluxQueryBuilder\Type;
use DateTime;
class Time implements TypeInterface
{
public function __construct(DateTime $dateTime)
{
$this->dateTime = $dateTime;
}
public function __toString(): string
{
return 'time(v: ' . $this->dateTime->format('Y-m-d\TH:i:s\Z') . ')';
}
}

View file

@ -0,0 +1,8 @@
<?php
namespace Arendsen\FluxQueryBuilder\Type;
interface TypeInterface
{
public function __toString(): string;
}