railt / carbon-extension
Carbon库提供用于处理日期和时间的对象的扩展
Requires
- php: ^7.1.3
- nesbot/carbon: ~1.9|~2.0
- railt/railt: 1.3.*|dev-master
Requires (Dev)
- phpunit/phpunit: ^6.5
This package is auto-updated.
Last update: 2022-02-01 13:13:05 UTC
README
Carbon Extension
目录
安装
composer require railt/carbon-extension
- 将扩展添加到您的应用程序中
$app = new Railt\Foundation\Application(); $app->extend(Railt\CarbonExtension\Extension::class); // Here
Laravel
在这种情况下,如果您使用Laravel Service Provider,可以按照以下方式添加此扩展
打开railt.php
配置文件并添加
'extensions' => [ // ... \Railt\CarbonExtension\Extension::class, // Here ]
Symfony
在这种情况下,如果您使用Symfony Bundle,可以按照以下方式添加此扩展
打开config.yml
配置文件并添加
railt: extensions: - Railt\CarbonExtension\Extension # Here
输出
您可以在您的类型中使用它。在添加了Carbon
类型后,该类型定义的字段中出现两个可选参数。
这些。客户端将看到以下模式
# Definition type YourExampleType { id: ID! some: String! createdAt: Carbon! } # What the client will see type YourExampleType { id: ID! some: String! createdAt( """ An argument that provides a format of the given value that are contained in a CarbonFormat enumeration type. """ format: CarbonFormat = RFC3339 ): Carbon! }
为了正确返回数据,只需传递日期类型。
注意:“createdAt”字段应提供兼容datetime的类型,如
- DateTime对象:https://php.ac.cn/manual/en/class.datetime.php
- Carbon对象:https://carbon.nesbot.com/docs/
- 字符串日期格式
- 整数时间戳
public function resolver(): array { return [ 'id' => 42, 'some' => 'Example', 'createdAt' => '2018-04-28T17:55:27+00:00', // Yesterday ]; }
请求可能如下所示
{ example { id some a: createdAt(format: COOKIE) b: createdAt(format: HUMAN_READABLE) } }
响应如下
{ "example": { "id": 42, "some": "Example", "createdAt": "Saturday, 28-Apr-2018 17:55:27 GMT+0000", "a": "Saturday, 28-Apr-2018 17:55:27 GMT+0000", "b": "4 months ago" } }
输出格式
返回值可以对应于在CarbonFormat
枚举中定义的有效格式之一。为了指定响应中您希望看到的日期格式,应将其作为format
参数的值传递。
{ example { createdAt(format: COOKIE) # createdAt field date will return in the COOKIE format. } }
以下是有效的CarbonFormat
枚举格式列表
- ISO8601 - ISO-8601日期格式。
示例:
2005-08-15T15:52:01+00:00
注意:此格式是RFC 3339规范的别名:ISO8601:https://www.iso.org/iso-8601-date-and-time-format.html RFC3339:https://www.ietf.org/rfc/rfc3339.txt
- RFC822 - RFC 822日期格式。
示例:
Mon, 15 Aug 05 15:52:01 +0000
- RFC850 - RFC 850日期格式。
示例:
Monday, 15-Aug-05 15:52:01 UTC
- RFC1036 - RFC 1036日期格式。
示例:
Mon, 15 Aug 05 15:52:01 +0000
- RFC1123 - RFC 1123日期格式。
示例:
Mon, 15 Aug 2005 15:52:01 +0000
- RFC2822 - RFC 2822日期格式。
示例:
Mon, 15 Aug 2005 15:52:01 +0000
- RFC3339 - RFC 3339日期格式。
示例:
2005-08-15T15:52:01+00:00
注意:此格式是 ISO-8601 规范的别名:RFC3339: https://www.ietf.org/rfc/rfc3339.txt ISO8601: https://www.iso.org/iso-8601-date-and-time-format.html
- RFC3339_EXTENDED - RFC 3339 日期格式。与通常的 RFC3339 不同,还包括毫秒。
示例:
2005-08-15T15:52:01.000+00:00
- RFC7231 - RFC 7231 日期格式。
示例:
Mon, 15 Aug 2005 15:52:01 GMT
- COOKIE - HTTP Cookie 日期格式。
示例:
Monday, 15-Aug-2005 15:52:01 UTC
- DATE_TIME - 简单的日期时间格式。
示例:
2005-08-15 15:52:01
- DATE - 简单的日期格式。
示例:
2005-08-15
- TIME - 简单的时间格式。
示例:
15:52:01
- RSS - RSS 日期格式。
示例:
Mon, 15 Aug 2005 15:52:01 +0000
- W3C - 万维网联盟日期格式。
示例:
2005-08-15T15:52:01+00:00
- HUMAN_READABLE - 人类可读的字符串。
示例:
2 days ago
输入
可以将标量 Carbon
类型作为参数传递给任何字段。在这种情况下,它将被强制转换为 Carbon
PHP 对象。
# Definition type Example { field(arg: Carbon!): String }
// Resolver // Note: "$arg" argument definition similar with "$input->get('arg')" public function handle(\DateTimeInterface $arg) { return $arg->format(\DateTime::RFC3339); }
# Query { field(arg: "now") }
{ "field": "2018-04-29T17:55:27+00:00" }
输入格式
作为可接受的输入值,允许以下格式: