1
1
# PHP Variable Exporter #
2
2
3
- * PHPEncoder* is a PHP library for exporting variables into PHP code similar to the
4
- built in function ` var_export() ` . Compared to the built in function, this
5
- library provides a more customizable alternative that makes it easier to
6
- dynamically create configuration files and cache files in the format you desire.
7
-
8
- The purpose of this library is to address some of the problems with
9
- ` var_export() ` . For example, there are no options to control the amount of
10
- whitespace in the output, which sometimes results in too verbose output. This
11
- library also offers more options in how to export objects.
3
+ * PHPEncoder* is a PHP library for exporting variables and generating PHP code
4
+ representations for said variables similar to the built in function
5
+ ` var_export() ` . Compared to the built in function, however, this library
6
+ provides more options to customize the output, which makes it easier to generate
7
+ code for different kinds of purposes such as readable configuration files or
8
+ optimized cache files.
9
+
10
+ The purpose of this library is to address some of the shortcomings with the
11
+ built in ` var_export() ` . For example, there is no way to control the amount of
12
+ whitespace in the output and there is no way to choose between different array
13
+ notations. This library also provides functionality to convert objects into PHP
14
+ code that is actually useful when compared to the built in function.
12
15
13
16
The large number of customization options in this library allows you to create
14
17
code that fits your purposes. You can create very compact code, when you need to
@@ -30,8 +33,8 @@ In order to use this library, the following requirements must be met:
30
33
31
34
## Installation ##
32
35
33
- This library can be installed via [ Composer] ( http://getcomposer.org/ ) . To do
34
- this, download the ` composer.phar ` and require this library as a dependency. For
36
+ This library can be installed via [ Composer] ( http://getcomposer.org/ ) . To do so,
37
+ download the ` composer.phar ` and require this library as a dependency. For
35
38
example:
36
39
37
40
```
@@ -51,12 +54,12 @@ Alternatively, you can add the dependency to your `composer.json` and run
51
54
```
52
55
53
56
Any library that has been installed via Composer can be loaded by including the
54
- ` vendor/autoload.php ` file that was generated by Composer.
57
+ ` vendor/autoload.php ` file that was generated by Composer during the install .
55
58
56
59
It is also possible to install this library manually. To do this, download the
57
60
[ latest release] ( https://github.com/Riimu/Kit-PHPEncoder/releases/latest ) and
58
- extract the ` src ` folder to your project folder. To load the library, include
59
- the provided ` src/autoload.php ` file.
61
+ extract the ` src ` folder to your project folder. To load the library, simply
62
+ include the provided ` src/autoload.php ` file.
60
63
61
64
## Usage ##
62
65
@@ -124,71 +127,81 @@ is possible to set these options in three different ways:
124
127
125
128
* Options can be provided as an array to the ` PHPEncoder ` constructor.
126
129
* Option values can be set via the ` setOption() ` method.
127
- * Options can be passed as an array as the second argument to the ` encode() ` method.
130
+ * Options can be passed as an array as the second argument to the ` encode() `
131
+ method.
128
132
129
133
Note that options passed to the ` encode() ` method are only temporary and do not
130
134
apply to following calls.
131
135
132
136
#### List of Options ####
133
137
134
138
* ** whitespace** : < ; boolean> ; (true)
135
- When set to false, generation of all extra whitespace is disabled and all
139
+ When set to ` false ` , generation of all extra whitespace is disabled and all
136
140
other settings that affect whitespace are ignored.
137
141
138
142
* ** null.capitalize** : < ; boolean> ; (false)
139
- When set to true, nulls are written in upper case instead of lower case.
143
+ When set to ` true ` , all ` null ` values are written in upper case instead of
144
+ lower case.
140
145
141
146
* ** boolean.capitalize** : < ; boolean> ; (false)
142
- When set to true, true and false are written in upper case instead of lower case.
147
+ When set to ` true ` , all ` true ` and ` false ` values are written in upper case
148
+ instead of lower case.
143
149
144
- * ** float.integers** : < ; boolean> ; (false)
145
- When set to true, float values that represent integers are encoded as
146
- integers instead of floats (e.g. '2.0' will be simply written as '2').
150
+ * ** float.integers** : < ; boolean|"all"> ; (false)
151
+ When set to ` true ` , any float that represents an integer and has a value
152
+ that is accurately represented by the floating point number will be encoded
153
+ as an integer instead of a float. (e.g. the value ` 2.0 ` will be encoded as
154
+ ` 2 ` ). To include the values that are not accurately represented, you may set
155
+ option to ` "all" ` .
147
156
148
157
* ** float.precision** : < ; integer|false> ; (17)
149
- Maximum number of decimals in floats. If set to false, the PHP ini setting
150
- 'precision' is used instead. Note that due to the way floating point values
151
- work, more than 17 digits does not provide any additional precision.
158
+ The maximum precision of encoded floating point values, which usually also
159
+ means the maximum number of digits in encoded floats. If the value is set to
160
+ ` false ` , the PHP ini setting ` serialize_precision ` will be used instead.
161
+ Note that due to the way floating point values work, a value greater than 17
162
+ does not provide any additional precision.
152
163
153
164
* ** string.escape** : < ; boolean> ; (true)
154
- When set to true, all strings containing bytes outside the 32-126 ASCII
165
+ When set to ` true ` , all strings containing bytes outside the 32-126 ASCII
155
166
range will be written with double quotes and the characters outside the
156
167
range will be escaped.
157
168
158
169
* ** array.short** : < ; boolean> ; (true)
159
- When set to true, arrays are enclosed using square brackets ` [] ` instead of
160
- the ` array() ` notation .
170
+ When set to ` true ` , arrays are enclosed using square brackets ` [] ` instead
171
+ using of the long array notation ` array() ` .
161
172
162
173
* ** array.base** : < ; integer|string> ; (0)
163
- Base indentation for arrays as number of spaces or as a string. Useful, when
164
- generating code into files with specific level of indentation.
174
+ Base indentation for arrays as a number of spaces or as a string. Provides
175
+ convenience when you need to output code to a file with specific level of
176
+ indentation.
165
177
166
178
* ** array.indent** : < ; integer|string> ; (4)
167
- Amount of indentation for single level of indentation as number of spaces or
168
- a string.
179
+ Amount of indentation for single level of indentation as a number of spaces
180
+ or a string.
169
181
170
182
* ** array.align** : < ; boolean> ; (false)
171
- When set to true, array assignment operators ` => ` are aligned to the same
183
+ When set to ` true ` , array assignment operators ` => ` are aligned to the same
172
184
column using spaces. Even if enabled, ` array.omit ` and ` array.inline `
173
- options are still respected, but only if all the keys in the array (or in
174
- any multi level array) can be omitted.
185
+ options are still respected, but only if all the keys in the specific array
186
+ can be omitted.
175
187
176
188
* ** array.inline** : < ; boolean|integer> ; (70)
177
- When set to true, any array that can be written without any array keys will
178
- be written in a single line. If an integer is provided instead, the array
179
- will be written as a single line only if it does not exceed that number of
180
- characters. This option has no effect when ` array.omit ` is set to false.
189
+ When set to ` true ` , any array that can be written without any array keys
190
+ will be written in a single line. If an integer is provided instead, the
191
+ array will be written as a single line only if it does not exceed that
192
+ number of characters. This option has no effect when ` array.omit ` is set to
193
+ false.
181
194
182
195
* ** array.omit** : < ; boolean> ; (true)
183
- When set to true, any redundant array key will not be written. (e.g. the
184
- array ` [0 => 'a', 1 => 'b'] ` would be written just as ` ['a', 'b'] ` )
196
+ When set to ` true ` , any redundant array keys will not be included (e.g. the
197
+ array ` [0 => 'a', 1 => 'b'] ` would be encoded just as ` ['a', 'b'] ` ).
185
198
186
199
* ** array.eol** : < ; string|false> ; (false)
187
- The end of line character used by array output. When set to false, the
200
+ The end of line character used by array output. When set to ` false ` , the
188
201
default ` PHP_EOL ` will be used instead.
189
202
190
203
* ** object.method** : < ; boolean> ; (true)
191
- When set to true, any encoded object will be checked for methods ` toPHP() `
204
+ When set to ` true ` , any encoded object will be checked for methods ` toPHP() `
192
205
and ` toPHPValue() ` . If the method ` toPHP() ` exists, the returned string will
193
206
be used as the PHP code representation of the object. If the method
194
207
` toPHPValue() ` exists instead, the returned value will be encoded as PHP and
@@ -206,19 +219,19 @@ apply to following calls.
206
219
207
220
* ** object.cast** : < ; boolean> ; (true)
208
221
Whether to add an ` (object) ` cast in front of arrays generated from objects
209
- when using ` vars ` , ` array ` or ` iterate ` formats .
222
+ or not when using the object encoding formats ` vars ` , ` array ` or ` iterate ` .
210
223
211
224
* ** recursion.detect** : < ; boolean> ; (true)
212
- When set to true, the encoder will attempt to detect circular references in
213
- arrays and objects to avoid infinite loops.
225
+ When set to ` true ` , the encoder will attempt to detect circular references
226
+ in arrays and objects to avoid infinite loops.
214
227
215
228
* ** recursion.ignore** : < ; boolean> ; (false)
216
- When set to true, any circular reference will be replaced with null instead
217
- of throwing an exception.
229
+ When set to ` true ` , any circular reference will be replaced with ` null `
230
+ instead of throwing an exception.
218
231
219
232
* ** recursion.max** : < ; integer|false> ; (false)
220
233
Maximum number of levels when encoding arrays and objects. Exception is
221
- thrown when the maximum is exceeded. Set to false to have no limit.
234
+ thrown when the maximum is exceeded. Set to ` false ` to have no limit.
222
235
223
236
## Credits ##
224
237
0 commit comments