This repository was archived by the owner on Dec 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.php
78 lines (72 loc) · 1.86 KB
/
basic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env php
<?php
require_once __DIR__ . "/../vendor/autoload.php";
use SK\StructArray\Exception\StructValidationException;
use function SK\StructArray\{
allOf, anyOf, arrayOf, classOf, not, optional, struct, validate
};
$eventStruct = struct('Event', [
'id' => allOf('is_string', 'is_numeric'),
'type' => optional('is_string', 'general'),
'date' => anyOf(classOf(DateTime::class), 'is_null'),
'priceFrom' => 'is_float',
'tickets' => arrayOf(not('is_null')),
'tagMap' => arrayOf(arrayOf('is_string')),
]);
$events = [
[
'id' => '123',
'type' => 'theatre',
'date' => new DateTime(),
'priceFrom' => 20.5,
'tickets' => ['General', 10],
'tagMap' => [['family', 'kids'], ['gig', 'club']],
],
[
'id' => '123',
'type' => 'theatre',
'date' => new DateTime(),
'priceFrom' => 20.5,
'tickets' => ['General', 10],
'tagMap' => ['family', 'kids'],
],
[
'id' => '123',
'type' => 'sport',
'date' => '02-02-2020',
'priceFrom' => 20.5,
'tickets' => [],
'tagMap' => [],
],
[
'id' => 'abc',
'type' => 'sport',
'date' => new DateTime(),
'priceFrom' => 20.5,
'tickets' => [],
'tagMap' => [],
],
[
'id' => '123',
'date' => new DateTime(),
'priceFrom' => 20.5,
'tickets' => ['General', null],
'tagMap' => [],
],
[
'id' => '123',
'type' => 'theatre',
'date' => null,
'priceFrom' => 20.5,
'tickets' => ['General', 10],
'what is this' => 'get out',
'tagMap' => [],
],
];
foreach ($events as $id => $event) {
try {
validate($event, $eventStruct);
} catch (StructValidationException $e) {
echo $e->getMessage() . PHP_EOL;
}
}