igorw / edn
Extensible Data Notation Parser.
Installs: 11 321
Dependents: 1
Suggesters: 0
Security: 0
Stars: 42
Watchers: 5
Forks: 3
Open Issues: 3
Requires
- php: >=5.4.0
- nikic/phlexy: ~1.0-dev
Requires (Dev)
- rhumsaa/uuid: ~2.4
- shaunxcode/edn-tests: dev-master
Suggests
- rhumsaa/uuid: If you want to use UUID tags.
This package is not auto-updated.
Last update: 2024-11-09 14:04:07 UTC
README
Parser for the edn format. In PHP.
The parser internally relies on Phlexy for lexing.
Usage
Parsing
To parse edn, just use the parse
function:
$edn = file_get_contents('examples/sample.edn');
$data = igorw\edn\parse($edn);
print_r($data);
You can also define custom tag handlers and pass them as a second argument to
parse
:
use igorw\edn;
class Person {
public $firstName;
public $lastName;
function __construct($firstName, $lastName) {
$this->firstName = $firstName;
$this->lastName = $lastName;
}
}
$edn = '#myapp/Person {:first "Fred" :last "Mertz"}';
$data = edn\parse($edn, [
'myapp/Person' => function ($node) {
return new Person(
$node[edn\keyword('first')],
$node[edn\keyword('last')]
);
},
]);
// [new Person('Fred', 'Mertz')]
Encoding
If you want to take an in-memory data structure and encode it as edn, you can
use the encode
function:
use igorw\edn;
$person = new edn\Map();
$person[edn\keyword('name')] = 'igorw';
$list = new edn\LinkedList([
edn\symbol('foo'),
edn\symbol('bar'),
edn\symbol('baz'),
edn\keyword('qux'),
1.0,
$person,
]);
$edn = edn\encode([$list]);
// (foo bar baz :qux 1.0 {:name "igorw"})
Tests
This library runs against the shaunxcode/edn-tests suite.