igorw / edn
可扩展数据表示法解析器。
dev-master / 1.0.x-dev
2014-01-08 13:44 UTC
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-09-14 13:16:06 UTC
README
用于edn格式的解析器。用PHP编写。
解析器内部依赖Phlexy进行词法分析。
用法
解析
要解析edn,只需使用parse
函数
$edn = file_get_contents('examples/sample.edn');
$data = igorw\edn\parse($edn);
print_r($data);
您还可以定义自定义标签处理程序并将它们作为第二个参数传递给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')]
编码
如果您想将内存中的数据结构编码为edn,可以使用encode
函数
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"})
测试
此库针对shaunxcode/edn-tests套件进行测试。