-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStruct.php
More file actions
280 lines (239 loc) · 7.07 KB
/
Struct.php
File metadata and controls
280 lines (239 loc) · 7.07 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
namespace {
/**
* Add struct function to global namespace
*
* @param $name
* @param $properties
* @param array $methods
* @return \Struct\Struct
*/
function struct($name, $properties, $methods = array()) {
return new Struct\Struct($name, $properties, $methods);
}
}
namespace Struct {
/**
* Class Struct
* @package Struct
*/
class Struct
{
/**
* @var string
*/
protected $name;
/**
* @var array
*/
protected $properties;
/**
* @var array
*/
protected $methods;
/**
* @var bool
*/
public static $strict = true;
/**
* @var string
*/
protected $src;
/**
* @param string $name
* @param array $properties
* @param array $methods
*/
public function __construct(string $name, array $properties, $methods = array()) {
$this->name = $name;
$this->properties = $properties;
$this->methods = $methods;
$this->src = '';
if (!preg_match('/^[A-Z]\w+/', $name)) {
throw new \InvalidArgumentException('Invalid struct name: ' . $name);
}
$this->compile();
eval($this->src);
return new $name();
}
/**
* @return string
*/
public function getSource() {
return $this->src;
}
/**
* @param $property
* @param $type
*/
public function addProperty($property, $type) {
$this->properties[$property] = $type;
}
/**
* Compile properties and methods into a string that resembles a PHP class.
*
* @return void
*/
protected function compile() {
$this->classHeader();
$this->arrayHelpers();
$this->properties();
$this->methods();
$this->classFooter();
}
/**
* Provide array hydrate/export functionality
*
* @return void
*/
protected function arrayHelpers() {
$this->src .= <<<TOARRAYHELPER
public function toArray() {
\$array = array();
foreach (\$this as \$prop => \$val) {
\$array[\$prop] = \$val;
}
return \$array;
}
TOARRAYHELPER;
$existCheck = 'if (!$this->offsetExists($prop)) {';
if (self::$strict) {
$existCheck .= 'throw new \InvalidArgumentException(\'Struct does not contain property `\' . $key . \'`\');';
} else {
$existCheck .= 'continue;';
}
$existCheck .= '}';
$this->src .= <<<FROMARRAYHELPER
public function fromArray(\$array) {
foreach (\$array as \$prop => \$val) {
{$existCheck}
\$this->offsetSet(\$prop,\$val);
}
}
FROMARRAYHELPER;
}
/**
* Attach methods to PHP class
*
* @return void
*/
protected function methods() {
foreach ($this->methods as $name => $fn) {
$ref = new \ReflectionFunction($fn);
$filename = $ref->getFileName();
$start_line = $ref->getStartLine();
$end_line = $ref->getEndLine()-1;
$length = $end_line - $start_line;
$source = file($filename);
$body = implode("", array_slice($source, $start_line, $length));
$this->src .= <<<METHOD
public function {$name}() {
{$body}
}
METHOD;
}
}
/**
* Attach properties to PHP class
*
* @return void
*/
protected function properties() {
$propArray = array();
foreach ($this->properties as $property => $type) {
if (!in_array($type, array('int','string','float','bool')) && !class_exists($type)) {
throw new \InvalidArgumentException('Unknown property type for ' . $property . ': ' . $type);
}
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $property)) {
throw new \InvalidArgumentException('Invalid property name: ' . $property);
}
$propArray[] = "'{$property}'";
$this->property($property, $type);
}
$this->src .= PHP_EOL . ' private $properties = array(' . implode(',', $propArray) . ');';
}
/**
* Attach a property to PHP class source
*
* @param $name
* @param $type
*/
protected function property($name, $type) {
$this->src .= <<<PROPERTY
private \${$name};
private function set_{$name}({$type} \$val) {
\$this->{$name} = \$val;
}
private function get_{$name}():{$type} {
return \$this->{$name};
}
PROPERTY;
}
/**
* End PHP class
*
* @return void
*/
protected function classFooter() {
$this->src .= PHP_EOL . '}';
}
/**
* Boilerplate header for PHP class
*
* @return void
*/
protected function classHeader() {
$prepend = '';
if (self::$strict === true) {
$prepend = 'declare(strict_types=1);' . PHP_EOL . PHP_EOL;
}
$this->src .= sprintf('
%sclass %s implements \ArrayAccess, \Iterator {', $prepend, $this->name) . PHP_EOL . PHP_EOL;
$this->src .= <<<BOILERPLATE
private \$idx;
public function __construct() {
\$this->idx = 0;
}
public function current() {
\$getter = 'get_' . \$this->properties[\$this->idx];
return \$this->{\$getter}();
}
public function key() {
return \$this->properties[\$this->idx];
}
public function next() {
++\$this->idx;
}
public function rewind() {
\$this->idx = 0;
}
public function valid() {
return isset(\$this->properties[\$this->idx]);
}
public function offsetSet(\$key, \$value) {
if (!\$this->offsetExists(\$key)) {
throw new \InvalidArgumentException('Struct does not contain property `' . \$key . '`');
}
\$setter = 'set_' . \$key;
\$this->{\$setter}(\$value);
}
public function offsetExists(\$key) {
return property_exists(\$this, \$key);
}
public function offsetUnset(\$key) {
if (!\$this->offsetExists(\$key)) {
throw new \InvalidArgumentException('Struct does not contain property `' . \$key . '`');
}
\$this->{\$key} = null;
}
public function offsetGet(\$key) {
if (!\$this->offsetExists(\$key)) {
throw new \InvalidArgumentException('Struct does not contain property `' . \$key . '`');
}
\$getter = 'get_' . \$key;
return \$this->{\$getter}();
}
BOILERPLATE;
}
}
}