-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
134 lines (119 loc) · 3.99 KB
/
example.php
File metadata and controls
134 lines (119 loc) · 3.99 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
define('STRUCT_PARAM_CHECKING', true);
error_reporting(E_ALL & ~E_DEPRECATED);
require_once __DIR__.'/vendor/autoload.php';
use Daison\Struct\Collection;
use Daison\Struct\Contract;
use Daison\Struct\Struct;
use Daison\Struct\Common;
/**
* @method string email() Get the email
* @method string firstName() Get the first name
* @method string lastName() Get the last name
* @method string gender() Get the gender
* @method int age() Get the age
* @method Collection photos() Get the lists of photos
* @method bool married()
* @method TypeLocationInterface location()
* @method Closure closure()
* @method DummyClass class()
*/
interface TypeUserInterface extends Contract
{
// avoid filling this up, the purpose of this interface
// is only to support your IDE / Code Editor
}
/**
* @method string url()
* @method string name()
*/
interface TypePhotoInterface extends Contract
{
// avoid filling this up, the purpose of this interface
// is only to support your IDE / Code Editor
}
/**
* @method float x() Get the x axis
* @method float y() Get the y axis
*/
interface TypeLocationInterface extends Contract
{
// avoid filling this up, the purpose of this interface
// is only to support your IDE / Code Editor
}
$locationStruct = new Struct([
'x' => Common::FLOAT(),
'y' => Common::FLOAT(),
]);
/** @var TypePhotoInterface */
$photoStruct = new Struct([
'name' => Common::STRING(),
'url' => Common::STRING(),
]);
/** @var TypeUserInterface */
$userStruct = new Struct([
'any' => Common::ANY(),
'nullStringKey1' => fn (string $value) => $value,
'nullStringKey2' => fn (string $value) => $value,
'email' => Common::STRING(),
'firstName' => Common::STRING(),
'lastName' => Common::STRING(),
'gender' => Common::STRING(),
'age' => Common::INTEGER(),
'married' => Common::BOOLEAN(),
'location' => fn (array $location) => $locationStruct->load($location),
'photos' => fn (array $photos) => new Collection($photoStruct, $photos ?? []),
'closure' => fn (Closure $x): Closure => $x,
'class' => fn (DummyClass $x): DummyClass => $x,
]);
class DummyClass
{
}
$userStruct->load([
'any' => 111222,
// 'nullStringKey1' => 'imagine this is not passed by your Clients, but you are allowing them.',
'nullStringKey2' => 'this is nullable string key',
'email' => 'johndoe@email.com',
'firstName' => 'John',
'lastName' => 'Doe',
'gender' => 'male',
'age' => 31,
'married' => true,
'location' => ['x' => 0.111, 'y' => 0.555],
'photos' => [
[
'name' => 'GitHub',
'url' => 'https://github.com/daison12006013',
'no-reference-should-not-be-added' => '',
],
],
'closure' => fn () => true,
'class' => new DummyClass(),
]);
var_dump([
'email' => $userStruct->email,
'firstName' => $userStruct->firstName,
'lastName' => $userStruct->lastName,
'gender' => $userStruct->gender,
'age' => $userStruct->age,
'married' => $userStruct->married,
'location' => $userStruct->location,
'location_x' => $userStruct->location->x,
'location_y' => $userStruct->location->y,
'photos' => $userStruct->photos,
'photos_0' => $userStruct->photos[0],
'photos_0_name' => $userStruct->photos[0]->name,
'photos_is_empty' => $userStruct->photos->empty(),
'toArray' => $userStruct->toArray(),
'closure' => $userStruct->closure,
'class' => $userStruct->class,
'nullStringKey1' => $userStruct->nullStringKey1, // returns null
'nullStringKey2' => $userStruct->nullStringKey2, // this is nullable string key
]);
/** @var TypePhotoInterface */
foreach ($userStruct->photos as $idx => $photo) {
var_dump($idx, $photo->name);
var_dump($idx, $photo->url);
// expect that this will throw an error, unless it is part of the loaded struct
// var_dump($idx, $photo->whatever);
}