Issues (38)

src/Pohoda/AbstractDocument.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace kalanis\Pohoda;
6
7
/**
8
 * @property Document\AbstractDocumentDto $data
9
 */
10
abstract class AbstractDocument extends AbstractAgenda
11
{
12
    use Common\AddParameterToHeaderTrait;
13
14
    /**
15
     * Add document item.
16
     *
17
     * @param Common\Dtos\AbstractItemDto $data
18
     *
19
     * @return Document\AbstractPart
20
     */
21 12
    public function addItem(Common\Dtos\AbstractItemDto $data): Document\AbstractPart
22
    {
23 12
        $part = $this->getDocumentPart('Item');
24 12
        $part->setDirectionalVariable($this->useOneDirectionalVariables);
25 12
        $part->setData($data);
26
        if (
27 12
            is_a($part, Document\AbstractItem::class)
28 12
            && is_object($this->data) && property_exists($this->data, 'details')
29
        ) {
30 12
            $this->data->details[] = $part;
31
        }
32
33 12
        return $part;
34
    }
35
36
    /**
37
     * Add document summary.
38
     *
39
     * @param Common\Dtos\AbstractSummaryDto $data
40
     *
41
     * @return $this
42
     */
43 11
    public function addSummary(Common\Dtos\AbstractSummaryDto $data): self
44
    {
45 11
        $summary = $this->getDocumentPart('Summary');
46 11
        $summary->setDirectionalVariable($this->useOneDirectionalVariables);
47 11
        $summary->setData($data);
48
        if (
49 11
            is_a($summary, Document\AbstractSummary::class)
50 11
            && is_object($this->data) && property_exists($this->data, 'summary')
51
        ) {
52 11
            $this->data->summary = $summary;
53
        }
54
55 11
        return $this;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 54
    public function setData(Common\Dtos\AbstractDto $data): parent
62
    {
63
        // pass to header
64 54
        if (!empty($data->header)) {
65 54
            $header = $this->getDocumentPart('Header', $this->resolveOptions)
66 54
                ->setDirectionalVariable($this->useOneDirectionalVariables)
67 54
                ->setData($data->header);
68 54
            $data->header = $header;
69
        }
70 54
        return parent::setData($data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::setData($data) returns the type kalanis\Pohoda\AbstractAgenda which is incompatible with the type-hinted return parent.
Loading history...
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 46
    public function getXML(): \SimpleXMLElement
77
    {
78 46
        $xml = $this->createXML()->addChild(
79 46
            $this->getChildKey($this->getDocumentNamespace() . ':' . $this->getDocumentName()),
80 46
            '',
81 46
            $this->namespace($this->getChildNamespacePrefix($this->getDocumentNamespace())),
82 46
        );
83 46
        $xml->addAttribute('version', '2.0');
84
85 46
        $this->addElements(
86 46
            $xml,
87 46
            $this->getDataElements(),
88 46
            $this->getDocumentNamespace(),
89 46
        );
90
91 46
        return $xml;
92
    }
93
94
    /**
95
     * Document part factory.
96
     * This code is the loader for things like "Header", "Summary", "Item"
97
     *
98
     * @param string $partName
99
     * @param bool   $resolveOptions
100
     *
101
     * @return Document\AbstractPart
102
     */
103 54
    protected function getDocumentPart(string $partName, bool $resolveOptions = true): Document\AbstractPart
104
    {
105 54
        $part = $this->dependenciesFactory->getDocumentPartFactory()->getPart(\get_class($this), $partName);
106 54
        $part->setNamespace($this->getDocumentNamespace());
107 54
        $part->setNodePrefix($this->getDocumentName());
108 54
        $part->setResolveOptions($resolveOptions);
109 54
        return $part;
110
    }
111
112
    /**
113
     * @{inheritDoc}
114
     */
115 46
    protected function getNodeKey(string $key): string
116
    {
117 46
        if ('details' == $key) {
118 46
            return $this->getDocumentName() . 'Detail';
119
        }
120 46
        return $key;
121
    }
122
123
    /**
124
     * Get document namespace.
125
     *
126
     * @return string
127
     */
128
    abstract protected function getDocumentNamespace(): string;
129
130
    /**
131
     * Get document name used in XML nodes.
132
     *
133
     * @return string
134
     */
135
    abstract protected function getDocumentName(): string;
136
}
137