Skip to content

Commit 723c286

Browse files
committed
Updated the readme
Updated the readme with all the info people might need in the beginning
1 parent 0740a56 commit 723c286

File tree

2 files changed

+174
-7
lines changed

2 files changed

+174
-7
lines changed

README.md

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,116 @@
11
# php-encoder
22

3-
This repository is under construction
3+
[![Build Status](https://travis-ci.org/harm-less/php-encoder.svg?branch=master)](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)

index.php

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,65 @@
11
<?php
2-
32
error_reporting( E_ALL );
4-
ini_set("display_errors", 'On' );
3+
ini_set('display_errors', 'On');
4+
5+
require('vendor/autoload.php');
6+
7+
use PE\Encoders\XmlEncoder;
8+
9+
use PE\Nodes\EncoderNode;
10+
use PE\Nodes\EncoderNodeVariable;
11+
12+
// create a simple class with 1 variable and a setter/getter
13+
class HelloWorld {
14+
15+
private $foo;
16+
17+
public function setFoo($value) {
18+
$this->foo = $value;
19+
}
20+
public function getFoo() {
21+
return $this->foo;
22+
}
23+
}
24+
25+
// create a corresponding node and add the variable
26+
class HelloWorldNode extends EncoderNode {
27+
28+
function __construct() {
29+
parent::__construct('hello-worlds', 'hello-world', null);
30+
31+
$this->addVariable(new EncoderNodeVariable('foo'));
32+
}
33+
}
34+
35+
// register the node so it becomes known to the encoder
36+
EncoderNode::addNode(new HelloWorldNode());
37+
38+
// create a HelloWorld object
39+
$helloWorld = new HelloWorld();
40+
$helloWorld->setFoo('hello world');
41+
42+
// make an instance of an encoder type and encode the object
43+
$encoder = new XmlEncoder();
44+
$encodedResultXml = $encoder->encode($helloWorld);
45+
46+
// will output:
47+
/* <?xml version="1.0" encoding="UTF-8"?>
48+
* <encoded>
49+
* <hello-world foo="hello world"/>
50+
* </encoded>
51+
*/
52+
echo htmlentities($encodedResultXml->saveXML());
553

6-
require('tests/bootstrap.php');
54+
// decode the XML again
55+
$decoded = $encoder->decode($encodedResultXml->saveXML());
756

8-
use PE\Encoder;
57+
// will output:
58+
/*
59+
* HelloWorld Object
60+
* (
61+
* [foo:HelloWorld:private] => hello world
62+
* )
63+
*/
64+
print_r($decoded['hello-world']);
965

10-
$encoder = new Encoder();
11-
//pr($encoder);

0 commit comments

Comments
 (0)