-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
65 lines (49 loc) · 1.35 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
error_reporting( E_ALL );
ini_set('display_errors', 'On');
require('vendor/autoload.php');
use PE\Encoders\XmlEncoder;
use PE\Nodes\EncoderNode;
use PE\Nodes\EncoderNodeVariable;
// create a simple class with 1 variable and a setter/getter
class HelloWorld {
private $foo;
public function setFoo($value) {
$this->foo = $value;
}
public function getFoo() {
return $this->foo;
}
}
// create a corresponding node and add the variable
class HelloWorldNode extends EncoderNode {
function __construct() {
parent::__construct('hello-worlds', 'hello-world', null);
$this->addVariable(new EncoderNodeVariable('foo'));
}
}
// register the node so it becomes known to the encoder
EncoderNode::addNode(new HelloWorldNode());
// create a HelloWorld object
$helloWorld = new HelloWorld();
$helloWorld->setFoo('hello world');
// make an instance of an encoder type and encode the object
$encoder = new XmlEncoder();
$encodedResultXml = $encoder->encode($helloWorld);
// will output:
/* <?xml version="1.0" encoding="UTF-8"?>
* <encoded>
* <hello-world foo="hello world"/>
* </encoded>
*/
echo htmlentities($encodedResultXml->saveXML());
// decode the XML again
$decoded = $encoder->decode($encodedResultXml->saveXML());
// will output:
/*
* HelloWorld Object
* (
* [foo:HelloWorld:private] => hello world
* )
*/
print_r($decoded['hello-world']);