Add Types
Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
parent
02810bd7fa
commit
a50e33ba04
5 changed files with 93 additions and 1 deletions
|
|
@ -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
29
src/Type.php
Normal 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
18
src/Type/Time.php
Normal 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') . ')';
|
||||
}
|
||||
}
|
||||
8
src/Type/TypeInterface.php
Normal file
8
src/Type/TypeInterface.php
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace Arendsen\FluxQueryBuilder\Type;
|
||||
|
||||
interface TypeInterface
|
||||
{
|
||||
public function __toString(): string;
|
||||
}
|
||||
36
tests/Type/FactoryTypeTest.php
Normal file
36
tests/Type/FactoryTypeTest.php
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Type;
|
||||
|
||||
use DateTime;
|
||||
use Arendsen\FluxQueryBuilder\Type;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class FactoryTypeTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider getFluxStringProvider
|
||||
*/
|
||||
public function testGetFluxString($value, $expected)
|
||||
{
|
||||
$type = new Type($value);
|
||||
|
||||
$this->assertEquals($expected, $type->__toString());
|
||||
}
|
||||
|
||||
public function getFluxStringProvider()
|
||||
{
|
||||
return [
|
||||
'DateTime' => [
|
||||
new DateTime('2022-08-15 23:09:00'),
|
||||
'time(v: 2022-08-15T23:09:00Z)',
|
||||
],
|
||||
'String' => [
|
||||
'value',
|
||||
'"value"',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
editor.link_modal.header
Reference in a new issue