Download the PHP package maaaarcel/devalue-php without Composer
On this page you can find all versions of the php package maaaarcel/devalue-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download maaaarcel/devalue-php
More information about maaaarcel/devalue-php
Files in maaaarcel/devalue-php
Package devalue-php
Short Description A library for stringifying and parsing complex data. Inspired by the devalue JavaScript library.
License MIT
Homepage https://github.com/Maaaarcel/devalue-php
Informations about the package devalue-php
devalue-php
This is an PHP port of devalue, a JavaScript library for stringifying and parsing of data that is too complex for JSON. Take a look at the original repo for all features.
All credit for the parsing and stringifying logic goes to the creator and contributors of the devalue library. The logic in this implementation is mostly the same, with some slight changes to improve the performance with PHP.
Installation
Install it using composer:
Usage
The Devalue
class has the stringify
, parse
and unflatten
methods, used for stringifying and parsing on the backend.
Devalue::stringify
can handle the following data types:
\Maaaarcel\DevaluePhp\JavaScript\JsBooleanObject
\Maaaarcel\DevaluePhp\JavaScript\JsNumberObject
\Maaaarcel\DevaluePhp\JavaScript\JsStringObject
\Maaaarcel\DevaluePhp\JavaScript\JsBigInt
\Maaaarcel\DevaluePhp\JavaScript\JsMap
\Maaaarcel\DevaluePhp\JavaScript\JsSet
\Maaaarcel\DevaluePhp\JavaScript\JsRegExp
int
(gets automatically converted to aBigInt
, if the number is greater than9007199254740991
)float
string
bool
null
NAN
(gets converted toNaN
)INF
(gets converted toInfinity
/-Infinity
)\DateTimeInterface
(gets converted toDate()
)\JsonSerializable
(gets converted to a JS object)\stdClass
(gets converted to a JS object)\ArrayObject
(gets converted to a JS array)[1, 2, 3] (list)
(gets converted to a JS array)['a' => 1, 'b' => 2, 'c' => 3] (assoc array)
(gets converted to a JS object)
The Devalue::parse
and Devalue::unflatten
methods convert some JavaScript values into objects/enum values for a more
accurate representation of the data. Those objects/enum values can also be used to pass those JavaScript values when
using Devalue::stringify
.
Here is a list of the conversions (left: JavaScript value, right: PHP value):
objects
:\stdClass()
arrays
:\ArrayObject()
Date()
:\DateTime()
BigInt()
:\Maaaarcel\DevaluePhp\JavaScript\JsBigInt()
Boolean()
:\Maaaarcel\DevaluePhp\JavaScript\JsBooleanObject()
Number()
:\Maaaarcel\DevaluePhp\JavaScript\JsNumberObject()
String()
:\Maaaarcel\DevaluePhp\JavaScript\JsStringObject()
RegExp()
:\Maaaarcel\DevaluePhp\JavaScript\JsRegExp()
Map()
:\Maaaarcel\DevaluePhp\JavaScript\JsMap()
Set()
:\Maaaarcel\DevaluePhp\JavaScript\JsSet()
undefined
:\Maaaarcel\DevaluePhp\JavaScript\JsValue::Undefined
NaN
:\Maaaarcel\DevaluePhp\JavaScript\JsValue::Nan
-0
:\Maaaarcel\DevaluePhp\JavaScript\JsValue::NegativeZero
Infinity
:\Maaaarcel\DevaluePhp\JavaScript\JsValue::PositiveInfinity
-Infinity
:\Maaaarcel\DevaluePhp\JavaScript\JsValue::NegativeInfinity
Stringify converts some specific PHP values for better compatability / usability before creating the string:
To consume your data in your frontend code, you have to use the original devalue library.
Performance
Please see the github action result of the current commit for performance numbers. The benchmarks are run with phpbench
License
MIT