-
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.
Merge pull request #28 from okazy/fix/datetime-field
日付を取得できるようにした
- Loading branch information
Showing
2 changed files
with
90 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,88 @@ | ||
<?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 DateTimeInterface; | ||
use GraphQL\Error\InvariantViolation; | ||
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 | ||
*/ | ||
public function serialize($value) | ||
{ | ||
if (!$value instanceof DateTimeInterface) { | ||
throw new InvariantViolation('DateTime is not an instance of DateTimeInterface: '.Utils::printSafe($value)); | ||
} | ||
|
||
return $value->format(DateTime::ATOM); | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* | ||
* @return DateTime|false|null | ||
*/ | ||
public function parseValue($value) | ||
{ | ||
return DateTime::createFromFormat(DateTime::ATOM, $value) ?: null; | ||
} | ||
|
||
/** | ||
* @param Node $valueNode | ||
* @param array|null $variables | ||
* | ||
* @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