1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Larium\Pay; |
6
|
|
|
|
7
|
|
|
use XMLWriter; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* XmlBuilder provides a fluent way to create xml strings. |
11
|
|
|
* |
12
|
|
|
* @author Andreas Kollaros <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class XmlBuilder |
15
|
|
|
{ |
16
|
|
|
protected $writer; |
17
|
|
|
|
18
|
1 |
|
public function __construct() |
19
|
|
|
{ |
20
|
1 |
|
$this->writer = new XMLWriter(); |
21
|
1 |
|
$this->writer->openMemory(); |
22
|
|
|
} |
23
|
|
|
|
24
|
1 |
|
public function instruct($version, $encoding = null, $indent = true) |
25
|
|
|
{ |
26
|
1 |
|
$this->writer->startDocument($version, $encoding); |
27
|
1 |
|
if ($indent) { |
28
|
1 |
|
$this->writer->setIndent(true); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function docType($qualifiedName, $publicId = null, $systemId = null) |
33
|
|
|
{ |
34
|
|
|
$this->writer->startDTD($qualifiedName, $publicId, $systemId); |
35
|
|
|
$this->writer->endDTD(); |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
public function build() |
39
|
|
|
{ |
40
|
1 |
|
$args = func_get_args(); |
41
|
|
|
|
42
|
1 |
|
$args = reset($args); |
43
|
1 |
|
$name = array_shift($args); |
44
|
1 |
|
$block_or_string = array_shift($args); |
45
|
1 |
|
$attributes = array_shift($args); |
46
|
|
|
|
47
|
1 |
|
$this->createElement($name, $block_or_string, $attributes); |
48
|
|
|
|
49
|
1 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
public function __call($name, $args) |
53
|
|
|
{ |
54
|
1 |
|
$args = array_merge([$name], $args); |
55
|
|
|
|
56
|
1 |
|
return $this->build($args); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function __toString() |
60
|
|
|
{ |
61
|
1 |
|
$this->writer->endDocument(); |
62
|
|
|
|
63
|
1 |
|
return $this->writer->outputMemory(); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
private function createElement($name, $body, array $attributes = null) |
67
|
|
|
{ |
68
|
1 |
|
$this->writer->startElement($name); |
69
|
|
|
|
70
|
1 |
|
$this->createAttributes($attributes); |
71
|
|
|
|
72
|
1 |
|
if (is_callable($body)) { |
73
|
1 |
|
$body($this); |
74
|
|
|
} else { |
75
|
1 |
|
$this->writer->text($body); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
$this->writer->endElement(); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
private function createAttributes(array $attributes = null) |
82
|
|
|
{ |
83
|
1 |
|
if ($attributes) { |
84
|
1 |
|
foreach ($attributes as $key => $value) { |
85
|
1 |
|
$this->writer->startAttribute($key); |
86
|
1 |
|
$this->writer->text($value); |
87
|
1 |
|
$this->writer->endAttribute(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|