-
Notifications
You must be signed in to change notification settings - Fork 17
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
Showing
2 changed files
with
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of EC-CUBE | ||
* | ||
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved. | ||
* | ||
* http://www.ec-cube.co.jp/ | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Plugin\Api\GraphQL\Type\Definition; | ||
|
||
use DateTime; | ||
use DateTimeImmutable; | ||
use DateTimeInterface; | ||
use GraphQL\Error\Error; | ||
use GraphQL\Language\AST\Node; | ||
use GraphQL\Language\AST\StringValueNode; | ||
use GraphQL\Type\Definition\ScalarType; | ||
use GraphQL\Utils\Utils; | ||
|
||
class DateTimeType extends ScalarType | ||
{ | ||
private static $DateTimeType; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $name = 'DateTime'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $description = 'The `DateTime` scalar type represents time data, represented as an ISO-8601 encoded UTC date string.'; | ||
|
||
/** | ||
* @param mixed $value | ||
* | ||
* @return string | ||
* | ||
* @throws Error | ||
*/ | ||
public function serialize($value) | ||
{ | ||
if (!$value instanceof DateTimeInterface) { | ||
throw new Error('DateTime is not an instance of DateTimeInterface: '.Utils::printSafe($value)); | ||
} | ||
|
||
return $value->format(DateTime::ATOM); | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* | ||
* @return DateTimeImmutable|false|null | ||
*/ | ||
public function parseValue($value) | ||
{ | ||
return DateTimeImmutable::createFromFormat(DateTime::ATOM, $value) ?: null; | ||
} | ||
|
||
/** | ||
* @param Node $valueNode | ||
* | ||
* @return string|null | ||
*/ | ||
public function parseLiteral($valueNode, ?array $variables = null) | ||
{ | ||
if ($valueNode instanceof StringValueNode) { | ||
return $valueNode->value; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @api | ||
*/ | ||
public static function DateTime(): ScalarType | ||
{ | ||
if (static::$DateTimeType === null) { | ||
static::$DateTimeType = new DateTimeType(); | ||
} | ||
|
||
return static::$DateTimeType; | ||
} | ||
} |
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