-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.php
194 lines (151 loc) · 4.3 KB
/
example.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
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
<?php
use Aza\Components\PhpGen\CustomCode;
use Aza\Components\PhpGen\IPhpGenerable;
use Aza\Components\PhpGen\PhpGen;
require __DIR__ . '/../vendor/autoload.php';
/**
* AzaPhpGen examples
*
* @project Anizoptera CMF
* @package system.phpgen
* @author Amal Samally <amal.samally at gmail.com>
* @license MIT
*/
// Example #1 - Simple dump
// ------------------------
// Get singleton instance of PhpGen
$phpGen = PhpGen::instance();
// Integer
echo $phpGen->getCode(123456789) . PHP_EOL; // 123456789;
// String (binary strings are supported as well)
echo $phpGen->getCode('some string' . ' example') . PHP_EOL; // "some string example";
// Float without trailing semicolon
echo $phpGen->getCodeNoTail(12.345) . PHP_EOL; // 12.345
// Simple serializable objects
$var = new stdClass();
echo $phpGen->getCode($var) . PHP_EOL; // unserialize("O:8:\"stdClass\":0:{}");
// Another object example
$var = new DateTime('2013-02-23 00:49:36', new DateTimeZone('UTC'));
echo $phpGen->getCode($var) . PHP_EOL; // unserialize("O:8:\"DateTime\":3:{s:4:\"date\";s:19:\"2013-02-23 00:49:36\";s:13:\"timezone_type\";i:3;s:8:\"timezone\";s:3:\"UTC\";}");
// Example #2 - Array dump
// ------------------------
// AzaPhpGen will use short array syntax if possible by default (PHP >= 5.4)
echo $phpGen->getCode(array(
true, false, null
)) . PHP_EOL;
/*
[
true,
false,
null,
];
*/
// Build code without formatting
echo $phpGen->getCodeNoFormat(array(
true, false, null
)) . PHP_EOL;
/*
[true,false,null];
*/
// Complex array (some sort of config for example)
$array = array(
'key1' => 'value',
'long_key' => 'value',
'array' => array(
'short_value'
),
'array2' => array(
'very very very very very very very very very very very very long value'
),
'other',
123456789
);
echo $phpGen->getCode($array) . PHP_EOL;
/*
[
"key1" => "value",
"long_key" => "value",
"array" => ["short_value"],
"array2" => [
"very very very very very very very very very very very very long value",
],
0 => "other",
1 => 123456789,
];
*/
// And wothout formatting
echo $phpGen->getCodeNoFormat($array) . PHP_EOL;
/*
["key1"=>"value","long_key"=>"value","array"=>["short_value"],"array2"=>["very very very very very very very very very very very very long value"],0=>"other",1=>123456789];
*/
// Example #3 - Traversable dump
// ------------------------
$var = new SplFixedArray(3);
$var[0] = 'a';
$var[1] = 'b';
echo $phpGen->getCodeNoFormat($var) . PHP_EOL; // ["a","b",null];
// Example #4 - Closure example
// ------------------------
$closure = function($a, $b) {
return round($a, $b) . "example\t\n";
};
echo $phpGen->getCode($closure) . PHP_EOL;
/*
function($a, $b) {
return round($a, $b) . "example\t\n";
};
*/
echo $phpGen->getCode(array('key' => $closure)) . PHP_EOL;
/*
[
"key" => function($a, $b) {
return round($a, $b) . "example\t\n";
},
];
*/
// Example #5 - Custom object dumping with IPhpGenerable interface
// ------------------------
class ExampleCustomCode implements IPhpGenerable
{
public function generateCode()
{
return '32434 + 5678';
}
}
$var = new ExampleCustomCode();
echo $phpGen->getCode($var) . PHP_EOL; // 32434 + 5678;
echo $phpGen->getCode(array($var)) . PHP_EOL; // [32434 + 5678];
// Example #6 - Bundled simple CustomCode class usage
// ------------------------
$var = new CustomCode('"some code" . PHP_EOL');
echo $phpGen->getCode($var) . PHP_EOL; // "some code" . PHP_EOL;
echo $phpGen->getCode(array($var)) . PHP_EOL; // ["some code" . PHP_EOL];
// Example #7 - Custom object dumping with defined handlers
// ------------------------
// Set custom handler for DateTime type
$phpGen->addCustomHandler('DateTime', function($data) use ($phpGen) {
/** @var $data \DateTime */
return $phpGen->getCodeNoTail(
$data->format("Y-m-dO")
);
});
// Build code
$var = new DateTime('2013-02-23 00:49:36', new DateTimeZone('UTC'));
echo $phpGen->getCode($var) . PHP_EOL; // "2013-02-23+0000";
// Example #8 - AzaPhpGen customization
// ------------------------
// Disable short array syntax and use 6 spaces for indentation
$phpGen->shortArraySyntax = false;
$phpGen->useSpaces = true;
$phpGen->tabLength = 6;
$var = array(array(array(23 => 'example')));
echo $phpGen->getCode($var) . PHP_EOL;
/*
array(
array(
array(
23 => "example",
),
),
);
*/