rodnaph / edhen
Encode/Decode EDN
Installs: 124 634
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 1
Forks: 3
Open Issues: 0
Requires
- php: >=5.3.3
Requires (Dev)
- phpunit/phpunit: 3.7.*
README
A tool to encode/decode between EDN and PHP data structures.
Note
When converting from EDN to PHP the conversion is lossy as the richness of datatypes supported by EDN is not available in PHP. So a conversion from EDN to PHP and back to EDN would not lose you data, but it would lose type information.
Usage
The interface is via some static functions on the Edhen class. To decode an EDN element...
$element = Edhen::decode('(1 :foo [2])'); // array(1, ':foo', array(2))
If you have EDN with multiple elements, you can use decodeAll
$elements = Edhen::decodeAll(':foo :bar :baz'); // array(':foo', ':bar', ':baz')
Then for encoding use the encode function, passing it the data to encode...
$ednString = Edhen::encode(array(1, 2)); // '[1 2]'
Data Type Translations
When decoding EDN to PHP...
Builtin Tags
When encoding PHP to EDN...
The decision on if an array is to be converted to a vector or hashmap is done by checking its keys. If any of the keys are non-numeric then a hashmap is used.
EDN is generated as a single string, no pretty-printing is currently supported. Another tool should be used for this.
Custom Tag Handlers
To implement your own tag handlers, create a class which implements the Edhen\TagHandler interface and pass it in an array as the second argument to decode/decodeAll
$myHandler = new MyCustomTagHandler(); $element = Edhen::decode($edn, array($myHandler));
You can see an example in the tests.
Installation
Edhen can be installed via Composer.