1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of riesenia/pohoda package. |
5
|
|
|
* |
6
|
|
|
* Licensed under the MIT License |
7
|
|
|
* (c) RIESENIA.com |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Riesenia\Pohoda; |
13
|
|
|
|
14
|
|
|
use Riesenia\Pohoda\Common\AddParameterToHeaderTrait; |
15
|
|
|
use Riesenia\Pohoda\Common\OptionsResolver; |
16
|
|
|
use Riesenia\Pohoda\ValueTransformer\SanitizeEncoding; |
17
|
|
|
|
18
|
|
|
abstract class AbstractDocument extends AbstractAgenda |
19
|
|
|
{ |
20
|
|
|
use AddParameterToHeaderTrait; |
21
|
|
|
|
22
|
|
|
protected DocumentPartFactory $documentPartFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
56 |
|
public function __construct( |
28
|
|
|
Common\NamespacesPaths $namespacesPaths, |
29
|
|
|
SanitizeEncoding $sanitizeEncoding, |
30
|
|
|
OptionsResolver\Normalizers\NormalizerFactory $normalizerFactory = new OptionsResolver\Normalizers\NormalizerFactory(), |
31
|
|
|
) { |
32
|
56 |
|
$this->documentPartFactory = new DocumentPartFactory($namespacesPaths, $sanitizeEncoding, $normalizerFactory); |
33
|
|
|
|
34
|
56 |
|
parent::__construct($namespacesPaths, $sanitizeEncoding, $normalizerFactory); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Add document item. |
39
|
|
|
* |
40
|
|
|
* @param array<string,mixed> $data |
41
|
|
|
* |
42
|
|
|
* @return Document\AbstractPart |
43
|
|
|
*/ |
44
|
12 |
|
public function addItem(array $data): Document\AbstractPart |
45
|
|
|
{ |
46
|
12 |
|
$key = $this->getDocumentName() . 'Detail'; |
47
|
|
|
|
48
|
12 |
|
if (!isset($this->data[$key]) |
49
|
12 |
|
|| !( |
50
|
12 |
|
is_array($this->data[$key]) |
51
|
12 |
|
|| (is_object($this->data[$key]) && is_a($this->data[$key], \ArrayAccess::class)) |
52
|
12 |
|
) |
53
|
|
|
) { |
54
|
12 |
|
$this->data[$key] = []; |
55
|
|
|
} |
56
|
|
|
|
57
|
12 |
|
$part = $this->getDocumentPart('Item')->setDirectionalVariable($this->useOneDirectionalVariables)->setData($data); |
58
|
12 |
|
$this->data[$key][] = $part; |
59
|
|
|
|
60
|
12 |
|
return $part; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Add document summary. |
65
|
|
|
* |
66
|
|
|
* @param array<string,mixed> $data |
67
|
|
|
* |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
11 |
|
public function addSummary(array $data): self |
71
|
|
|
{ |
72
|
11 |
|
$this->data['summary'] = $this->getDocumentPart('Summary')->setDirectionalVariable($this->useOneDirectionalVariables)->setData($data); |
73
|
|
|
|
74
|
11 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
54 |
|
public function setData(array $data): parent |
81
|
|
|
{ |
82
|
|
|
// pass to header |
83
|
54 |
|
if (!empty($data)) { |
84
|
54 |
|
$data = ['header' => $this->getDocumentPart('Header', $this->resolveOptions)->setDirectionalVariable($this->useOneDirectionalVariables)->setData($data)]; |
85
|
|
|
} |
86
|
54 |
|
return parent::setData($data); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
46 |
|
public function getXML(): \SimpleXMLElement |
93
|
|
|
{ |
94
|
46 |
|
$xml = $this->createXML()->addChild( |
95
|
46 |
|
$this->getChildKey($this->getDocumentNamespace() . ':' . $this->getDocumentName()), |
96
|
46 |
|
'', |
97
|
46 |
|
$this->namespace($this->getChildNamespacePrefix($this->getDocumentNamespace())), |
98
|
46 |
|
); |
99
|
46 |
|
$xml->addAttribute('version', '2.0'); |
100
|
|
|
|
101
|
46 |
|
$this->addElements($xml, $this->getDocumentElements(), $this->getDocumentNamespace()); |
102
|
|
|
|
103
|
46 |
|
return $xml; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
10 |
|
protected function configureOptions(OptionsResolver $resolver): void |
110
|
|
|
{ |
111
|
|
|
// available options |
112
|
10 |
|
$resolver->setDefined(['header']); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Document part factory. |
117
|
|
|
* This code is the loader for things like "Header", "Summary", "Item" |
118
|
|
|
* |
119
|
|
|
* @param string $partName |
120
|
|
|
* @param bool $resolveOptions |
121
|
|
|
* |
122
|
|
|
* @return Document\AbstractPart |
123
|
|
|
*/ |
124
|
54 |
|
protected function getDocumentPart(string $partName, bool $resolveOptions = true): Document\AbstractPart |
125
|
|
|
{ |
126
|
54 |
|
$part = $this->documentPartFactory->getPart(\get_class($this), $partName); |
127
|
54 |
|
$part->setNamespace($this->getDocumentNamespace()); |
128
|
54 |
|
$part->setNodePrefix($this->getDocumentName()); |
129
|
54 |
|
$part->setResolveOptions($resolveOptions); |
130
|
54 |
|
return $part; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get defined elements. |
135
|
|
|
* |
136
|
|
|
* @return string[] |
137
|
|
|
*/ |
138
|
46 |
|
protected function getDocumentElements(): array |
139
|
|
|
{ |
140
|
46 |
|
return ['header', $this->getDocumentName() . 'Detail', 'summary']; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get document namespace. |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
abstract protected function getDocumentNamespace(): string; |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get document name used in XML nodes. |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
abstract protected function getDocumentName(): string; |
156
|
|
|
} |
157
|
|
|
|