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\Stock\Header; |
||
15 | use Riesenia\Pohoda\Stock\Price; |
||
16 | use Riesenia\Pohoda\Stock\StockItem; |
||
17 | |||
18 | class Stock extends AbstractAgenda |
||
19 | { |
||
20 | use Common\AddActionTypeTrait; |
||
21 | use Common\AddParameterToHeaderTrait; |
||
22 | |||
23 | /** |
||
24 | * {@inheritdoc} |
||
25 | */ |
||
26 | 20 | public function setData(array $data): parent |
|
27 | { |
||
28 | // pass to header |
||
29 | 20 | if (!empty($data)) { |
|
30 | 20 | $header = new Header($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory); |
|
31 | 20 | $data = ['header' => $header->setDirectionalVariable($this->useOneDirectionalVariables)->setData($data)]; |
|
32 | } |
||
33 | |||
34 | 20 | return parent::setData($data); |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
35 | } |
||
36 | |||
37 | 1 | public function getImportRoot(): string |
|
38 | { |
||
39 | 1 | return 'lStk:stock'; |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * Add stock item. |
||
44 | * |
||
45 | * @param array<string,mixed> $data |
||
46 | * |
||
47 | * @return $this |
||
48 | */ |
||
49 | 1 | public function addStockItem(array $data): self |
|
50 | { |
||
51 | 1 | if (!isset($this->data['stockDetail']) |
|
52 | 1 | || !( |
|
53 | 1 | is_array($this->data['stockDetail']) |
|
54 | 1 | || (is_object($this->data['stockDetail']) && is_a($this->data['stockDetail'], \ArrayAccess::class)) |
|
55 | 1 | ) |
|
56 | ) { |
||
57 | 1 | $this->data['stockDetail'] = []; |
|
58 | } |
||
59 | |||
60 | 1 | $stockDetail = new StockItem($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory); |
|
61 | 1 | $this->data['stockDetail'][] = $stockDetail->setDirectionalVariable($this->useOneDirectionalVariables)->setData($data); |
|
62 | |||
63 | 1 | return $this; |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * Add price. |
||
68 | * |
||
69 | * @param string $code |
||
70 | * @param float $value |
||
71 | * |
||
72 | * @return $this |
||
73 | */ |
||
74 | 1 | public function addPrice(string $code, float $value): self |
|
75 | { |
||
76 | 1 | if (!isset($this->data['stockPriceItem']) |
|
77 | 1 | || !( |
|
78 | 1 | is_array($this->data['stockPriceItem']) |
|
79 | 1 | || (is_object($this->data['stockPriceItem']) && is_a($this->data['stockPriceItem'], \ArrayAccess::class)) |
|
80 | 1 | ) |
|
81 | ) { |
||
82 | 1 | $this->data['stockPriceItem'] = []; |
|
83 | } |
||
84 | |||
85 | 1 | $price = new Price($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory); |
|
86 | 1 | $this->data['stockPriceItem'][] = $price->setDirectionalVariable($this->useOneDirectionalVariables)->setData([ |
|
87 | 1 | 'ids' => $code, |
|
88 | 1 | 'price' => $value, |
|
89 | 1 | ]); |
|
90 | |||
91 | 1 | return $this; |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * Add image. |
||
96 | * |
||
97 | * @param string $filepath |
||
98 | * @param string $description |
||
99 | * @param int|null $order |
||
100 | * @param bool $default |
||
101 | * |
||
102 | * @return $this |
||
103 | */ |
||
104 | 1 | public function addImage(string $filepath, string $description = '', int $order = null, bool $default = false): self |
|
105 | { |
||
106 | 1 | $object = $this->data['header']; |
|
107 | /** @var Header $object */ |
||
108 | 1 | $object->addImage($filepath, $description, $order, $default); |
|
109 | |||
110 | 1 | return $this; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Add category. |
||
115 | * |
||
116 | * @param int $categoryId |
||
117 | * |
||
118 | * @return $this |
||
119 | */ |
||
120 | 1 | public function addCategory(int $categoryId): self |
|
121 | { |
||
122 | 1 | $object = $this->data['header']; |
|
123 | /** @var Header $object */ |
||
124 | 1 | $object->addCategory($categoryId); |
|
125 | |||
126 | 1 | return $this; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * Add int parameter. |
||
131 | * |
||
132 | * @param array<string,mixed> $data |
||
133 | * |
||
134 | * @return $this |
||
135 | */ |
||
136 | 1 | public function addIntParameter(array $data): self |
|
137 | { |
||
138 | 1 | $object = $this->data['header']; |
|
139 | /** @var Header $object */ |
||
140 | 1 | $object->addIntParameter($data); |
|
141 | |||
142 | 1 | return $this; |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | 18 | public function getXML(): \SimpleXMLElement |
|
149 | { |
||
150 | 18 | $xml = $this->createXML()->addChild('stk:stock', '', $this->namespace('stk')); |
|
151 | 18 | $xml->addAttribute('version', '2.0'); |
|
152 | |||
153 | 18 | $this->addElements($xml, ['actionType', 'header', 'stockDetail', 'stockPriceItem'], 'stk'); |
|
154 | |||
155 | 18 | return $xml; |
|
156 | } |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | 2 | protected function configureOptions(Common\OptionsResolver $resolver): void |
|
162 | { |
||
163 | // available options |
||
164 | 2 | $resolver->setDefined(['header']); |
|
165 | } |
||
166 | } |
||
167 |