Passed
Push — master ( b14078...3674b6 )
by payever
03:28
created

ProductRequestEntity::addVariant()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * PHP version 5.4 and 7
4
 *
5
 * @package   Payever\Products
6
 * @author    Hennadii.Shymanskyi <[email protected]>
7
 * @copyright 2017-2019 payever GmbH
8
 * @license   MIT <https://opensource.org/licenses/MIT>
9
 */
10
11
namespace Payever\ExternalIntegration\Products\Http\RequestEntity;
12
13
use Payever\ExternalIntegration\Core\Http\RequestEntity;
14
use Payever\ExternalIntegration\Products\Http\MessageEntity\ProductCategoryEntity;
15
use Payever\ExternalIntegration\Products\Http\MessageEntity\ProductShippingEntity;
16
use Payever\ExternalIntegration\Products\Http\MessageEntity\ProductVariantOptionEntity;
17
18
/**
19
 * PHP version 5.4 and 7
20
 *
21
 * @package   Payever\Products
22
 * @author    Hennadii.Shymanskyi <[email protected]>
23
 * @copyright 2017-2019 payever GmbH
24
 * @license   MIT <https://opensource.org/licenses/MIT>
25
 *
26
 * @method string getExternalId()
27
 * @method string[] getImages()
28
 * @method string[] getImagesUrl()
29
 * @method bool getActive()
30
 * @method string getUuid()
31
 * @method string getBusinessUuid()
32
 * @method ProductCategoryEntity[]|string[] getCategories()
33
 * @method string getCurrency()
34
 * @method string getTitle()
35
 * @method string getDescription()
36
 * @method float getPrice()
37
 * @method float|null getSalePrice()
38
 * @method bool getOnSales()
39
 * @method string getSku()
40
 * @method string getBarcode()
41
 * @method string getType()
42
 * @method self[]|array getVariants()
43
 * @method ProductShippingEntity|null getShipping()
44
 * @method \DateTime|false getCreatedAt()
45
 * @method \DateTime|false getUpdatedAt()
46
 * @method self setExternalId(string $externalId)
47
 * @method self setImages(array $images)
48
 * @method self setImagesUrl(array $imagesUrl)
49
 * @method self setActive(bool $active)
50
 * @method self setUuid(string $uuid)
51
 * @method self setBusinessUuid(string $businessUuid)
52
 * @method self setCurrency(string $currency)
53
 * @method self setTitle(string $title)
54
 * @method self setDescription(string $description)
55
 * @method self setPrice(float $price)
56
 * @method self setOnSales(bool $onSales)
57
 * @method self setSku(string $sku)
58
 * @method self setBarcode(string $barcode)
59
 * @method self setType(string $type)
60
 */
61
class ProductRequestEntity extends RequestEntity
62
{
63
    const UNDERSCORE_ON_SERIALIZATION = false;
64
65
    /**
66
     * Subscription external id.
67
     * Required for all requests.
68
     *
69
     * @var string
70
     */
71
    protected $externalId;
72
73
    /** @var array */
74
    protected $images = array();
75
76
    /** @var array */
77
    protected $imagesUrl = array();
78
79
    /** @var bool */
80
    protected $active = true;
81
82
    /** @var string */
83
    protected $uuid;
84
85
    /** @var string */
86
    protected $businessUuid;
87
88
    /** @var ProductCategoryEntity[]|string[] */
89
    protected $categories = array();
90
91
    /** @var string */
92
    protected $currency;
93
94
    /** @var string */
95
    protected $title;
96
97
    /** @var string */
98
    protected $description;
99
100
    /** @var float */
101
    protected $price;
102
103
    /** @var float|null */
104
    protected $salePrice;
105
106
    /** @var bool */
107
    protected $onSales = false;
108
109
    /** @var string */
110
    protected $sku;
111
112
    /** @var string */
113
    protected $barcode;
114
115
    /** @var string */
116
    protected $type;
117
118
    /** @var self[]|array */
119
    protected $variants = array();
120
121
    /** @var ProductShippingEntity|null */
122
    protected $shipping;
123
124
    /** @var \DateTime|bool */
125
    protected $createdAt;
126
127
    /** @var \DateTime|bool */
128
    protected $updatedAt;
129
130
    /**
131
     * Available only for product variants
132
     *
133
     * @var ProductVariantOptionEntity[]|array
134
     */
135
    protected $options = array();
136
137
    /**
138
     * @param string $updatedAt
139
     *
140
     * @return static
141
     */
142
    public function setUpdatedAt($updatedAt)
143
    {
144
        $this->updatedAt = date_create($updatedAt);
145
146
        return $this;
147
    }
148
149
    /**
150
     * @param string $createdAt
151
     *
152
     * @return static
153
     */
154
    public function setCreatedAt($createdAt)
155
    {
156
        $this->createdAt = date_create($createdAt);
157
158
        return $this;
159
    }
160
161
    /**
162
     * @param array $data
163
     *
164
     * @return static
165
     */
166
    public function setShipping($data)
167
    {
168
        $this->shipping = new ProductShippingEntity($data);
169
170
        return $this;
171
    }
172
173
    /**
174
     * @param array $data
175
     *
176
     * @return static
177
     */
178
    public function setCategories($data)
179
    {
180
        foreach ($data as $plainCategory) {
181
            /** Both ProductCategoryEntity fields array and simple title are possible */
182
            $this->categories[] = is_array($plainCategory)
183
                ? new ProductCategoryEntity($plainCategory)
184
                : $plainCategory
185
            ;
186
        }
187
188
        return $this;
189
    }
190
191
    /**
192
     * @param array[]|static[] $data
193
     *
194
     * @return static
195
     */
196
    public function setVariants($data)
197
    {
198
        foreach ($data as $variant) {
199
            $this->addVariant($variant);
200
        }
201
202
        return $this;
203
    }
204
205
    /**
206
     * @param array|static $variant
207
     *
208
     * @return self
209
     */
210
    public function addVariant($variant)
211
    {
212
        if (is_array($variant)) {
213
            $variant = new static($variant);
214
            $variant->setCurrency($this->getCurrency());
215
        }
216
217
        $this->variants[] = $variant;
218
219
        return $this;
220
    }
221
222
    /**
223
     * Set product variant option
224
     *
225
     * @param array $options
226
     *
227
     * @return self
228
     */
229
    public function setOptions($options)
230
    {
231
        if (is_array($options)) {
0 ignored issues
show
introduced by
The condition is_array($options) is always true.
Loading history...
232
            foreach ($options as $option) {
233
                $this->options[] = new ProductVariantOptionEntity($option);
234
            }
235
        }
236
237
        return $this;
238
    }
239
240
    /**
241
     * Add product variant option
242
     *
243
     * @param string $name
244
     * @param string $value
245
     *
246
     * @return self
247
     */
248
    public function addOption($name, $value)
249
    {
250
        $this->options[] = new ProductVariantOptionEntity(compact('name', 'value'));
251
252
        return $this;
253
    }
254
255
    /**
256
     * @return array
257
     */
258
    public function getImagesUuid()
259
    {
260
        return array_map(
261
            function ($imageName) {
262
                return substr($imageName, 0, strpos($imageName, '.'));
263
            },
264
            $this->getImages()
265
        );
266
    }
267
268
    /**
269
     * @param float $salePrice
270
     *
271
     * @return self
272
     */
273
    public function setSalePrice($salePrice)
274
    {
275
        $this->salePrice = $salePrice;
276
        $this->onSales = $salePrice > 0 && (!$this->getPrice() || $this->getPrice() > $salePrice);
277
278
        return $this;
279
    }
280
}
281