1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace KingsonDe\Marshal; |
6
|
|
|
|
7
|
|
|
use KingsonDe\Marshal\Data\DataStructure; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @method static string serializeItem(AbstractMapper $mapper, ...$data) |
11
|
|
|
* @method static string serializeItemCallable(callable $mappingFunction, ...$data) |
12
|
|
|
* @method static string serializeCollection(AbstractMapper $mapper, ...$data) |
13
|
|
|
* @method static string serializeCollectionCallable(callable $mappingFunction, ...$data) |
14
|
|
|
*/ |
15
|
|
|
class MarshalXml extends Marshal { |
16
|
|
|
|
17
|
|
|
const ATTRIBUTES_KEY = '@attributes'; |
18
|
|
|
const DATA_KEY = '@data'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected static $version = '1.0'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected static $encoding = 'UTF-8'; |
29
|
|
|
|
30
|
|
|
public static function setVersion(string $version) { |
31
|
|
|
static::$version = $version; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static function setEncoding(string $encoding) { |
35
|
|
|
static::$encoding = $encoding; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param DataStructure $dataStructure |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
3 |
|
public static function serialize(DataStructure $dataStructure) { |
43
|
3 |
|
$data = static::buildDataStructure($dataStructure); |
44
|
3 |
|
$xml = new \DOMDocument(static::$version, static::$encoding); |
45
|
|
|
|
46
|
3 |
|
if (null === $data) { |
47
|
|
|
$xmlRootNode = $xml->createElement('root'); |
48
|
|
|
$xml->appendChild($xmlRootNode); |
49
|
|
|
|
50
|
|
|
return $xml->saveXML(); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
3 |
|
\reset($data); |
54
|
3 |
|
$rootNode = \key($data); |
55
|
|
|
|
56
|
3 |
|
if (isset($data[$rootNode][static::DATA_KEY])) { |
57
|
1 |
|
$xmlRootNode = $xml->createElement($rootNode, static::castValueToString($data[$rootNode][static::DATA_KEY])); |
58
|
1 |
|
unset($data[$rootNode][static::DATA_KEY]); |
59
|
|
|
} else { |
60
|
2 |
|
$xmlRootNode = $xml->createElement($rootNode); |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
if (isset($data[$rootNode][static::ATTRIBUTES_KEY])) { |
64
|
3 |
|
static::addAttributes($data[$rootNode][static::ATTRIBUTES_KEY], $xmlRootNode); |
65
|
3 |
|
unset($data[$rootNode][static::ATTRIBUTES_KEY]); |
66
|
|
|
} |
67
|
3 |
|
$xml->appendChild($xmlRootNode); |
68
|
|
|
|
69
|
3 |
|
static::processNodes($data[$rootNode], $xmlRootNode); |
70
|
|
|
|
71
|
3 |
|
return $xml->saveXML(); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
protected static function processNodes(array $nodes, \DOMElement $parentXmlNode) { |
75
|
3 |
|
foreach ($nodes as $node => $value) { |
76
|
2 |
|
$attributes = []; |
77
|
|
|
|
78
|
2 |
|
if (isset($value[static::ATTRIBUTES_KEY])) { |
79
|
2 |
|
$attributes = $value[static::ATTRIBUTES_KEY]; |
80
|
2 |
|
unset($value[static::ATTRIBUTES_KEY]); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
if (isset($value[static::DATA_KEY])) { |
84
|
2 |
|
$value = $value[static::DATA_KEY]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// new node with scalar value |
88
|
2 |
|
if (\is_scalar($value)) { |
89
|
1 |
|
$xmlNode = $parentXmlNode->ownerDocument->createElement($node, static::castValueToString($value)); |
90
|
1 |
|
static::addAttributes($attributes, $xmlNode); |
91
|
1 |
|
$parentXmlNode->appendChild($xmlNode); |
92
|
1 |
|
continue; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// node collection of the same type |
96
|
2 |
|
if (\is_int($node)) { |
97
|
2 |
|
static::processNodes($value, $parentXmlNode); |
98
|
2 |
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// new node that might contain other nodes |
102
|
2 |
|
$xmlNode = $parentXmlNode->ownerDocument->createElement($node); |
103
|
2 |
|
static::addAttributes($attributes, $xmlNode); |
104
|
2 |
|
$parentXmlNode->appendChild($xmlNode); |
105
|
2 |
|
if (\is_array($value)) { |
106
|
2 |
|
static::processNodes($value, $xmlNode); |
107
|
|
|
} |
108
|
|
|
} |
109
|
3 |
|
} |
110
|
|
|
|
111
|
3 |
|
protected static function addAttributes(array $attributes, \DOMElement $xmlNode) { |
112
|
3 |
|
foreach ($attributes as $name => $value) { |
113
|
3 |
|
$xmlNode->setAttribute($name, static::castValueToString($value)); |
114
|
|
|
} |
115
|
3 |
|
} |
116
|
|
|
|
117
|
3 |
|
protected static function castValueToString($value): string { |
118
|
3 |
|
return (\is_string($value) ? $value : var_export($value, true)); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.