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