1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpBoot\Entity; |
4
|
|
|
|
5
|
|
|
use PhpBoot\Metas\PropertyMeta; |
6
|
|
|
use PhpBoot\Validator\Validator; |
7
|
|
|
|
8
|
|
|
class EntityContainer implements TypeContainerInterface |
9
|
|
|
{ |
10
|
9 |
|
public function __construct($className) |
11
|
|
|
{ |
12
|
9 |
|
$this->className = $className; |
13
|
9 |
|
} |
14
|
|
|
|
15
|
5 |
|
public function make($data, $validate = true) |
16
|
|
|
{ |
17
|
5 |
|
if($data === null){ |
18
|
|
|
return null; |
19
|
|
|
} |
20
|
5 |
|
$data instanceof \ArrayAccess || is_array($data) or \PhpBoot\abort(new \InvalidArgumentException("array is required by make {$this->className}, $data given")); |
21
|
5 |
|
$className = $this->getClassName(); |
22
|
5 |
|
$obj = new $className(); |
23
|
5 |
|
$vld = new Validator(); |
24
|
5 |
|
foreach ($this->properties as $p){ |
25
|
5 |
|
if($p->container && isset($data[$p->name])){ |
26
|
5 |
|
$var = $data[$p->name]; |
27
|
5 |
|
if($p->container instanceof EntityContainer |
28
|
5 |
|
|| $p->container instanceof ArrayContainer){ |
29
|
4 |
|
if(!$var){ |
30
|
|
|
$var = []; |
31
|
4 |
|
}elseif(is_string($var)){ |
32
|
3 |
|
$var = json_decode($var, true); |
33
|
3 |
|
!json_last_error() or \PhpBoot\abort(new \InvalidArgumentException(__METHOD__.' failed while json_decode with '.json_last_error_msg())); |
34
|
|
|
} |
35
|
4 |
|
} |
36
|
5 |
|
$data[$p->name] = $p->container->make($var, $validate); |
37
|
5 |
|
} |
38
|
|
|
|
39
|
5 |
|
if($p->validation){ |
40
|
4 |
|
if(is_array($p->validation)){ |
41
|
1 |
|
$vld->rule($p->validation[0], $p->name.'.'.$p->validation[1]); |
42
|
1 |
|
}else{ |
43
|
3 |
|
$vld->rule($p->validation, $p->name); |
44
|
|
|
} |
45
|
4 |
|
} |
46
|
5 |
|
if(!$p->isOptional && !$vld->hasRule('optional', $p->name)){ |
47
|
5 |
|
$vld->rule('required', $p->name); |
48
|
5 |
|
} |
49
|
5 |
|
} |
50
|
5 |
|
if($validate){ |
51
|
4 |
|
$vld = $vld->withData($data); |
52
|
4 |
|
$vld->validate() or \PhpBoot\abort( |
53
|
4 |
|
new \InvalidArgumentException( |
54
|
4 |
|
json_encode( |
55
|
4 |
|
$vld->errors(), |
56
|
4 |
|
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |
57
|
4 |
|
) |
58
|
4 |
|
) |
59
|
4 |
|
); |
60
|
4 |
|
} |
61
|
|
|
|
62
|
5 |
|
foreach ($this->properties as $p){ |
63
|
5 |
|
if(isset($data[$p->name])){ |
64
|
5 |
|
$obj->{$p->name} = $data[$p->name]; |
65
|
5 |
|
} |
66
|
5 |
|
} |
67
|
5 |
|
return $obj; |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function makeExample() |
72
|
|
|
{ |
73
|
|
|
$className = $this->getClassName(); |
74
|
|
|
$obj = new $className(); |
75
|
|
|
foreach ($this->properties as $p){ |
76
|
|
|
if($p->isOptional){ |
77
|
|
|
$obj->{$p->name} = $p->default; |
78
|
|
|
}elseif($p->container){ |
79
|
|
|
$var = $p->container->makeExample(); |
80
|
|
|
$obj->{$p->name} = $var; |
81
|
|
|
}else{ |
82
|
|
|
$obj->{$p->name} = null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
return $obj; |
87
|
|
|
} |
88
|
9 |
|
public function getProperty($target){ |
89
|
9 |
|
if(array_key_exists($target, $this->properties)){ |
90
|
9 |
|
return $this->properties[$target]; |
91
|
|
|
} |
92
|
|
|
return null; |
93
|
|
|
} |
94
|
9 |
|
public function setProperty($target, PropertyMeta $meta){ |
95
|
9 |
|
$this->properties[$target] = $meta; |
96
|
9 |
|
} |
97
|
|
|
/** |
98
|
|
|
* @return PropertyMeta[] |
99
|
|
|
*/ |
100
|
7 |
|
public function getProperties(){ |
101
|
7 |
|
return $this->properties; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
10 |
|
public function getClassName() |
108
|
|
|
{ |
109
|
10 |
|
return $this->className; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $fileName |
114
|
|
|
*/ |
115
|
9 |
|
public function setFileName($fileName) |
116
|
|
|
{ |
117
|
9 |
|
$this->fileName = $fileName; |
118
|
9 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $className |
122
|
|
|
*/ |
123
|
|
|
public function setClassName($className) |
124
|
|
|
{ |
125
|
|
|
$this->className = $className; |
126
|
|
|
} |
127
|
|
|
/** |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
1 |
|
public function getDescription() |
131
|
|
|
{ |
132
|
1 |
|
return $this->description; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
1 |
|
public function getSummary() |
139
|
|
|
{ |
140
|
1 |
|
return $this->summary; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $description |
145
|
|
|
*/ |
146
|
9 |
|
public function setDescription($description) |
147
|
|
|
{ |
148
|
9 |
|
$this->description = $description; |
149
|
9 |
|
} |
150
|
|
|
/** |
151
|
|
|
* @param string $summary |
152
|
|
|
*/ |
153
|
9 |
|
public function setSummary($summary) |
154
|
|
|
{ |
155
|
9 |
|
$this->summary = $summary; |
156
|
9 |
|
} |
157
|
|
|
/** |
158
|
|
|
* @var PropertyMeta[] |
159
|
|
|
*/ |
160
|
|
|
private $properties=[]; |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @var string |
164
|
|
|
*/ |
165
|
|
|
private $className; |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @var string |
169
|
|
|
*/ |
170
|
|
|
private $description=''; |
171
|
|
|
/** |
172
|
|
|
* @var string |
173
|
|
|
*/ |
174
|
|
|
private $summary=''; |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @var string |
178
|
|
|
*/ |
179
|
|
|
private $fileName; |
180
|
|
|
} |