Skip to content

Commit fea823a

Browse files
authored
Merge pull request #4 from BJX3Y/master
add DateType
2 parents ae72eb2 + a19b2af commit fea823a

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

src/TypeParser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use rethink\typedphp\types\BooleanType;
66
use rethink\typedphp\types\BinaryType;
7+
use rethink\typedphp\types\DateType;
78
use rethink\typedphp\types\DictType;
89
use rethink\typedphp\types\InputType;
910
use rethink\typedphp\types\IntegerType;
@@ -43,6 +44,7 @@ public function __construct($mode)
4344

4445
$this->registerBuiltinType(BinaryType::class);
4546
$this->registerBuiltinType(TimestampType::class);
47+
$this->registerBuiltinType(DateType::class);
4648
}
4749

4850
public function registerBuiltinType(string $typeClass)

src/types/DateType.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace rethink\typedphp\types;
4+
5+
/**
6+
* Class DateType
7+
*
8+
* @package rethink\typedphp\types
9+
*/
10+
class DateType implements PrimitiveType
11+
{
12+
public static function name()
13+
{
14+
return 'date';
15+
}
16+
17+
public static function toArray()
18+
{
19+
return [
20+
'type' => 'string',
21+
'format' => 'date',
22+
'pattern' => '^\d{4}-\d{2}-\d{2}$',
23+
];
24+
}
25+
}

tests/TypeTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ public function typeToArrayCases()
9595
'is_admin' => ['type' => 'boolean'],
9696
'file' => ['type' => 'string', 'format' => 'binary',],
9797
'nullable_field' => ['type' => ['string', 'null']],
98+
'date' => [
99+
'type' => ['string', 'null'],
100+
'format' => 'date',
101+
'pattern' => '^\d{4}-\d{2}-\d{2}$'
102+
],
98103
],
99104
'required' => ['id', 'file'],
100105
],
@@ -106,6 +111,12 @@ public function typeToArrayCases()
106111
'is_admin' => ['type' => 'boolean'],
107112
'file' => ['type' => 'string', 'format' => 'binary',],
108113
'nullable_field' => ['type' => 'string', 'nullable' => true],
114+
'date' => [
115+
'type' => 'string',
116+
'format' => 'date',
117+
'nullable' => true,
118+
'pattern' => '^\d{4}-\d{2}-\d{2}$',
119+
],
109120
],
110121
'required' => ['id', 'file'],
111122
],
@@ -222,6 +233,11 @@ public function typeToArrayCases()
222233
'is_admin' => ['type' => 'boolean'],
223234
'file' => ['type' => 'string', 'format' => 'binary',],
224235
'nullable_field' => ['type' => ['string', 'null']],
236+
'date' => [
237+
'type' => ['string', 'null'],
238+
'format' => 'date',
239+
'pattern' => '^\d{4}-\d{2}-\d{2}$'
240+
],
225241
],
226242
'required' => ['id', 'file'],
227243

@@ -258,6 +274,12 @@ public function typeToArrayCases()
258274
'is_admin' => ['type' => 'boolean'],
259275
'file' => ['type' => 'string', 'format' => 'binary',],
260276
'nullable_field' => ['type' => 'string', 'nullable' => true],
277+
'date' => [
278+
'type' => 'string',
279+
'format' => 'date',
280+
'nullable' => true,
281+
'pattern' => '^\d{4}-\d{2}-\d{2}$',
282+
],
261283
],
262284
'required' => ['id', 'file'],
263285

@@ -299,6 +321,11 @@ public function typeToArrayCases()
299321
'is_admin' => ['type' => 'boolean'],
300322
'file' => ['type' => 'string', 'format' => 'binary',],
301323
'nullable_field' => ['type' => ['string', 'null']],
324+
'date' => [
325+
'type' => ['string', 'null'],
326+
'format' => 'date',
327+
'pattern' => '^\d{4}-\d{2}-\d{2}$',
328+
],
302329
],
303330
'required' => ['id', 'file'],
304331

@@ -342,6 +369,12 @@ public function typeToArrayCases()
342369
'is_admin' => ['type' => 'boolean'],
343370
'file' => ['type' => 'string', 'format' => 'binary'],
344371
'nullable_field' => ['type' => 'string', 'nullable' => true],
372+
'date' => [
373+
'type' => 'string',
374+
'format' => 'date',
375+
'nullable' => true,
376+
'pattern' => '^\d{4}-\d{2}-\d{2}$',
377+
],
345378
],
346379
'required' => ['id', 'file'],
347380

@@ -443,6 +476,11 @@ public function typeToArrayCases()
443476
'nullable_field' => [
444477
'type' => ['string', 'null'],
445478
],
479+
'date' => [
480+
'type' => ['string', 'null'],
481+
'format' => 'date',
482+
'pattern' => '^\d{4}-\d{2}-\d{2}$'
483+
],
446484
],
447485
'required' => ['id', 'file'],
448486
],
@@ -469,6 +507,12 @@ public function typeToArrayCases()
469507
'type' => 'string',
470508
'nullable' => true,
471509
],
510+
'date' => [
511+
'type' => 'string',
512+
'format' => 'date',
513+
'nullable' => true,
514+
'pattern' => '^\d{4}-\d{2}-\d{2}$',
515+
],
472516
],
473517
'required' => ['id', 'file'],
474518
],
@@ -731,6 +775,7 @@ class Product001Type extends ProductType
731775
public static $is_admin = 'boolean';
732776
public static $file = '!binary';
733777
public static $nullable_field = 'string?';
778+
public static $date = 'date?';
734779
}
735780

736781
/**

0 commit comments

Comments
 (0)