1. Go to this page and download the library: Download maaaarcel/devalue-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
maaaarcel / devalue-php example snippets
use Maaaarcel\DevaluePhp\Devalue;
use Maaaarcel\DevaluePhp\JavaScript\JsBigInt;
use Maaaarcel\DevaluePhp\JavaScript\JsNumberObject;
use Maaaarcel\DevaluePhp\JavaScript\JsRegExp;
use DateTime;
$obj = new stdClass();
$obj->self = $obj;
$data = [
'recursiveObject' => $obj,
'regex' => new JsRegExp('.+', 'g'),
'date' => new DateTime('2023-01-16'),
'autoConverted' => INF,
'undefined' => JsValue::Undefined,
'normalNumber' => 1,
'array' => [new JsBigInt(1), new JsNumberObject(2)]
];
$dataStr = Devalue::stringify($data);
// => '[{"recursiveObject":1,"regex":2,"date":3,"autoConverted":-4,"undefined":-1,"normalNumber":4,"array":5},{"self":1},["RegExp",".+","g"],["Date","2023-01-16T00:00:00.000Z"],1,[6,7],["BigInt","1"],["Object",2]]'
$parsedData = Devalue::parse($dataStr);
// => object(stdClass)#507 (7) {
// ["recursiveObject"]=>
// object(stdClass)#505 (1) {
// ["self"]=>
// *RECURSION*
// }
// ["regex"]=>
// object(Maaaarcel\DevaluePhp\JavaScript\JsRegExp)#506 (2) {
// ["source":"Maaaarcel\DevaluePhp\JavaScript\JsRegExp":private]=>
// string(2) ".+"
// ["flags":"Maaaarcel\DevaluePhp\JavaScript\JsRegExp":private]=>
// string(1) "g"
// }
// ["date"]=>
// object(DateTime)#504 (3) {
// ["date"]=>
// string(26) "2023-01-16 00:00:00.000000"
// ["timezone_type"]=>
// int(2)
// ["timezone"]=>
// string(1) "Z"
// }
// ["autoConverted"]=>
// enum(Maaaarcel\DevaluePhp\JavaScript\JsValue::PositiveInfinity)
// ["undefined"]=>
// enum(Maaaarcel\DevaluePhp\JavaScript\JsValue::Undefined)
// ["normalNumber"]=>
// int(1)
// ["array"]=>
// object(ArrayObject)#503 (1) {
// ["storage":"ArrayObject":private]=>
// array(2) {
// [0]=>
// object(Maaaarcel\DevaluePhp\JavaScript\JsBigInt)#501 (1) {
// ["value":"Maaaarcel\DevaluePhp\JavaScript\JsBigInt":private]=>
// int(1)
// }
// [1]=>
// object(Maaaarcel\DevaluePhp\JavaScript\JsNumberObject)#500 (1) {
// ["value":"Maaaarcel\DevaluePhp\JavaScript\JsNumberObject":private]=>
// int(2)
// }
// }
// }
// }
// if your devalue payload was inside a normal JSON string, you can use the value from json_decode with the `unflatten`
// method like so:
$json = '{
"type": "data",
"data": [{...devalue data...}]
}';
$payload = json_decode($json);
$data = Devalue::unflatten($payload->data);
// You can create custom types to transfer common objects. Custom types must implement the `DevalueSerializable` interface
use Maaaarcel\DevaluePhp\DevalueSerializable;
class CustomType implements DevalueSerializable
{
public function __construct(
public readonly string $field1 = '',
public readonly string $field2 = ''
)
{
}
// specify the name of the type (used for parsing in the frontend library)
static function devalueType(): string
{
return 'CustomType';
}
// create a class instance from the serialized data
static function devalueParse(array $serialized): static
{
return new self($serialized['field1'], $serialized['field2']);
}
// serialize the data
function devalueSerialize(): array
{
return [
'field1' => $this->field1,
'field2' => $this->field2
];
}
}
$stringifiedCustomType = Devalue::stringify(new CustomType('foo', 'bar'));
// => '[["CustomType",1],{"field1":2,"field2":3},"foo","bar"]'
// To parse custom types, you can either specify your custom types globally, or individually for every parse.
// register type globally
Devalue::registerCustomTypes([CustomType::class]);
$parsedCustomType = Devalue::parse($stringifiedCustomType, [CustomType::class]); // register type only for this parse
// => class CustomType#552 (2) {
// public readonly string $field1 =>
// string(3) "foo"
// public readonly string $field2 =>
// string(3) "bar"
// }
shell
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.