prewk/serialized-to-ast

Parses serialized PHP things to an AST

0.1.0 2018-04-30 14:35 UTC

This package is auto-updated.

Last update: 2024-10-29 05:22:34 UTC


README

Parses serialized PHP data, without deserializing, into a simple AST.

Example

class Test {
    private $foo = "Private Foo";
    public $bar = 12345;
    protected $baz = ["lorem", "ipsum" => "dolor amet", 67890];
}

$parser = new Prewk\SerializedToAst;
$node = $parser->parse(serialize(new Test));

// Array representation..
$arrayAst = $node->toArray();

// ..or JSON (see below)
$jsonAst = json_encode($node);
{
    "type": "object",
    "public_properties": {
        "bar": {
            "type": "integer",
            "value": 12345
        }
    },
    "protected_properties": {
        "baz": {
            "type": "array",
            "items": {
                "0": {
                    "type": "string",
                    "value": "lorem"
                },
                "ipsum": {
                    "type": "string",
                    "value": "dolor amet"
                },
                "1": {
                    "type": "integer",
                    "value": 67890
                }
            }
        }
    },
    "private_properties": {
        "foo": {
            "type": "string",
            "value": "Private Foo"
        }
    }
}

Installation

composer require prewk/serialized-to-ast

Works for

  • Booleans
  • Integers
  • Strings
  • Floats
  • Null
  • Arrays
  • Objects

Will cry if fed other stuff.

Warning: Experimental and not tested for edge cases.

License

MIT