Issues (31)

src/Pohoda/Stock.php (1 issue)

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