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