, (*1)
, (*2)
Carbon Extension
Table of contents
- Installation
- Laravel
- Symfony
- Output
- Formats
- Input
- Formats, (*3)
Installation
composer require railt/carbon-extension
- Add extension to your application:
$app = new Railt\Foundation\Application();
$app->extend(Railt\CarbonExtension\Extension::class); // Here
Laravel
In that case, if you use Laravel Service Provider
this extension can be added as follows:, (*4)
Open the railt.php
configuration file and add:, (*5)
'extensions' => [
// ...
\Railt\CarbonExtension\Extension::class, // Here
]
Symfony
In that case, if you use Symfony Bundle
this extension can be added as follows:, (*6)
Open the config.yml
configuration file and add:, (*7)
railt:
extensions:
- Railt\CarbonExtension\Extension # Here
Output
You can use it within your types. After you add type Carbon
,
two optional arguments appear in the field which is defined by this type., (*8)
Those. Client will see the following scheme:, (*9)
# 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!
}
In order to correctly return data - just pass the date type., (*10)
Note: The "createdAt" field should provide datetime compatible type, like:
1) DateTime object: http://php.net/manual/en/class.datetime.php
2) Carbon object: https://carbon.nesbot.com/docs/
3) String datetime format
4) Integer timestamp, (*11)
public function resolver(): array
{
return [
'id' => 42,
'some' => 'Example',
'createdAt' => '2018-04-28T17:55:27+00:00', // Yesterday
];
}
The request might look like this:, (*12)
{
example {
id
some
a: createdAt(format: COOKIE)
b: createdAt(format: HUMAN_READABLE)
}
}
The response is as follows:, (*13)
{
"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"
}
}
The return value can correspond to one of the valid formats defined in the
CarbonFormat
enumeration. In order to specify what date format in the
response you want to see - it should be passed as the value of the format
argument., (*14)
{
example {
createdAt(format: COOKIE)
# createdAt field date will return in the COOKIE format.
}
}
Below is a list of valid CarbonFormat
enum formats:, (*15)
-
ISO8601 - ISO-8601 date format., (*16)
Example: 2005-08-15T15:52:01+00:00
Note: This format is an alias of the RFC 3339 specification:
ISO8601: https://www.iso.org/iso-8601-date-and-time-format.html
RFC3339: https://www.ietf.org/rfc/rfc3339.txt, (*17)
-
RFC822 - RFC 822 date format., (*18)
Example: Mon, 15 Aug 05 15:52:01 +0000
, (*19)
-
RFC850 - RFC 850 date format., (*20)
Example: Monday, 15-Aug-05 15:52:01 UTC
, (*21)
-
RFC1036 - RFC 1036 date format., (*22)
Example: Mon, 15 Aug 05 15:52:01 +0000
, (*23)
-
RFC1123 - RFC 1123 date format., (*24)
Example: Mon, 15 Aug 2005 15:52:01 +0000
, (*25)
-
RFC2822 - RFC 2822 date format., (*26)
Example: Mon, 15 Aug 2005 15:52:01 +0000
, (*27)
-
RFC3339 - RFC 3339 date format., (*28)
Example: 2005-08-15T15:52:01+00:00
Note: This format is an alias of the ISO-8601 specification:
RFC3339: https://www.ietf.org/rfc/rfc3339.txt
ISO8601: https://www.iso.org/iso-8601-date-and-time-format.html, (*29)
-
RFC3339_EXTENDED - RFC 3339 date format. In contrast to the usual RFC3339 additionally contains milliseconds., (*30)
Example: 2005-08-15T15:52:01.000+00:00
, (*31)
-
RFC7231 - RFC 7231 date format., (*32)
Example: Mon, 15 Aug 2005 15:52:01 GMT
, (*33)
-
COOKIE - HTTP Cookies date format., (*34)
Example: Monday, 15-Aug-2005 15:52:01 UTC
, (*35)
-
DATE_TIME - Simple DateTime format., (*36)
Example: 2005-08-15 15:52:01
, (*37)
-
DATE - Simple Date format., (*38)
Example: 2005-08-15
, (*39)
-
TIME - Simple Time format., (*40)
Example: 15:52:01
, (*41)
-
RSS - RSS date format., (*42)
Example: Mon, 15 Aug 2005 15:52:01 +0000
, (*43)
-
W3C - World Wide Web Consortium date format., (*44)
Example: 2005-08-15T15:52:01+00:00
, (*45)
-
HUMAN_READABLE - Human readable string., (*46)
Example: 2 days ago
, (*47)
A scalar Carbon
type can be passed as an argument to any field.
In this case it will be coerced into Carbon
PHP object., (*48)
# 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"
}
As the admissible input values, the following formats are allowed:, (*49)