|
1 | 1 | # php-encoder
|
2 | 2 |
|
3 |
| -This repository is under construction |
| 3 | +[](https://travis-ci.org/harm-less/php-encoder) |
| 4 | + |
| 5 | +**php-encoder** is a fast & flexible encoder for PHP 5.3+ |
| 6 | + |
| 7 | +## About |
| 8 | + |
| 9 | +This library will allow you to save a snapshot of your PHP object in various formats like XML or JSON (encoding). |
| 10 | +When your project has been setup properly you can reuse this by decoding the snapshot. It will return the |
| 11 | +same PHP object. |
| 12 | + |
| 13 | +This library is useful for you if you want to quickly find a way to save a state of your PHP object. Think about a |
| 14 | +configurator that allows you to customize a certain product in various ways for example. |
| 15 | + |
| 16 | +## Getting started |
| 17 | + |
| 18 | +1. PHP 5.3.x is required |
| 19 | +2. Install php-encoder using [Composer](#composer-installation) (recommended) or manually |
| 20 | +3. Set up the nodes you need for your PHP objects |
| 21 | + |
| 22 | +## Composer Installation |
| 23 | + |
| 24 | +1. Get [Composer](http://getcomposer.org/) |
| 25 | +2. Require the encoder with `composer require harm-less/php-encoder` |
| 26 | +3. Add the following to your application's main PHP file: `require 'vendor/autoload.php';` |
| 27 | + |
| 28 | +## Included encoders |
| 29 | + |
| 30 | +There are currently 2 encoding types available: *XML* and *JSON*. |
| 31 | +Both can be used interchangeably as long as you have set up your nodes correctly. |
| 32 | + |
| 33 | +## Example |
| 34 | + |
| 35 | +*Hello World* - Obligatory hello world example |
| 36 | + |
| 37 | +```php |
| 38 | +<?php |
| 39 | +require('vendor/autoload.php'); |
| 40 | + |
| 41 | +use PE\Encoders\XmlEncoder; |
| 42 | + |
| 43 | +use PE\Nodes\EncoderNode; |
| 44 | +use PE\Nodes\EncoderNodeVariable; |
| 45 | + |
| 46 | +// create a simple class with 1 variable and a setter/getter |
| 47 | +class HelloWorld { |
| 48 | + |
| 49 | + private $foo; |
| 50 | + |
| 51 | + public function setFoo($value) { |
| 52 | + $this->foo = $value; |
| 53 | + } |
| 54 | + public function getFoo() { |
| 55 | + return $this->foo; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// create a corresponding node and add the variable |
| 60 | +class HelloWorldNode extends EncoderNode { |
| 61 | + |
| 62 | + function __construct() { |
| 63 | + parent::__construct('hello-worlds', 'hello-world', null); |
| 64 | + |
| 65 | + $this->addVariable(new EncoderNodeVariable('foo')); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// register the node so it becomes known to the encoder |
| 70 | +EncoderNode::addNode(new HelloWorldNode()); |
| 71 | + |
| 72 | +// create a HelloWorld object |
| 73 | +$helloWorld = new HelloWorld(); |
| 74 | +$helloWorld->setFoo('hello world'); |
| 75 | + |
| 76 | +// make an instance of an encoder type and encode the object |
| 77 | +$encoder = new XmlEncoder(); |
| 78 | +$encodedResultXml = $encoder->encode($helloWorld); |
| 79 | + |
| 80 | +// will output: |
| 81 | +/* <?xml version="1.0" encoding="UTF-8"?> |
| 82 | + * <encoded> |
| 83 | + * <hello-world foo="hello world"/> |
| 84 | + * </encoded> |
| 85 | + */ |
| 86 | +echo htmlentities($encodedResultXml->saveXML()); |
| 87 | + |
| 88 | +// decode the XML again |
| 89 | +$decoded = $encoder->decode($encodedResultXml->saveXML()); |
| 90 | + |
| 91 | +// will output: |
| 92 | +/* |
| 93 | + * HelloWorld Object |
| 94 | + * ( |
| 95 | + * [foo:HelloWorld:private] => hello world |
| 96 | + * ) |
| 97 | + */ |
| 98 | +print_r($decoded['hello-world']); |
| 99 | +?> |
| 100 | +``` |
| 101 | + |
| 102 | +## Unit Testing |
| 103 | + |
| 104 | +This project uses [PHPUnit](https://github.com/sebastianbergmann/phpunit/) as |
| 105 | +its unit testing framework. |
| 106 | + |
| 107 | +The tests all live in `/tests` and each test extends an abstract class |
| 108 | +`AbstractPETest` |
| 109 | + |
| 110 | +To test the project, simply run `composer install --dev` to download |
| 111 | +a common version of PHPUnit with composer and run the tests from the main |
| 112 | +directory with `phpunit` or `./vendor/bin/phpunit` |
| 113 | + |
| 114 | +## Contributors |
| 115 | + |
| 116 | +- [Harm van der Werf](https://github.com/harm-less) |
0 commit comments