Passed
Push — master ( c525eb...8357f2 )
by payever
04:06
created

ProductRequestEntity::addOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 2
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 float getVatRate()
42
 * @method ProductVariantOptionEntity[]|array getOptions()
43
 * @method string getType()
44
 * @method self[]|array getVariants()
45
 * @method ProductShippingEntity|null getShipping()
46
 * @method \DateTime|false getCreatedAt()
47
 * @method \DateTime|false getUpdatedAt()
48
 * @method string|null getParent()
49
 * @method string|null getProduct()
50
 * @method self setExternalId(string $externalId)
51
 * @method self setImages(array $images)
52
 * @method self setImagesUrl(array $imagesUrl)
53
 * @method self setActive(bool $active)
54
 * @method self setUuid(string $uuid)
55
 * @method self setBusinessUuid(string $businessUuid)
56
 * @method self setCurrency(string $currency)
57
 * @method self setTitle(string $title)
58
 * @method self setDescription(string $description)
59
 * @method self setPrice(float $price)
60
 * @method self setOnSales(bool $onSales)
61
 * @method self setSku(string $sku)
62
 * @method self setBarcode(string $barcode)
63
 * @method self setVatRate(float $vatRate)
64
 * @method self setType(string $type)
65
 */
66
class ProductRequestEntity extends RequestEntity
67
{
68
    const UNDERSCORE_ON_SERIALIZATION = false;
69
70
    /**
71
     * Subscription external id.
72
     * Required for all requests.
73
     *
74
     * @var string
75
     */
76
    protected $externalId;
77
78
    /** @var array */
79
    protected $images = array();
80
81
    /** @var array */
82
    protected $imagesUrl = array();
83
84
    /** @var bool */
85
    protected $active = true;
86
87
    /** @var string */
88
    protected $uuid;
89
90
    /** @var string */
91
    protected $businessUuid;
92
93
    /** @var ProductCategoryEntity[]|string[] */
94
    protected $categories = array();
95
96
    /** @var string */
97
    protected $currency;
98
99
    /** @var string */
100
    protected $title;
101
102
    /** @var string */
103
    protected $description;
104
105
    /** @var float */
106
    protected $price;
107
108
    /** @var float|null */
109
    protected $salePrice;
110
111
    /** @var bool */
112
    protected $onSales = false;
113
114
    /** @var string */
115
    protected $sku;
116
117
    /** @var string */
118
    protected $barcode;
119
120
    /** @var string */
121
    protected $type;
122
123
    /** @var float */
124
    protected $vatRate;
125
126
    /** @var self[]|array */
127
    protected $variants = array();
128
129
    /**
130
     * Parent product id for variants.
131
     * Present only in request with a single variant data inside.
132
     *
133
     * @var string|null
134
     */
135
    protected $parent;
136
137
    /**
138
     * Parent product id for variants.
139
     * Present only in request with parent product with variants.
140
     *
141
     * @var string|null
142
     */
143
    protected $product;
144
145
    /** @var ProductShippingEntity|null */
146
    protected $shipping;
147
148
    /** @var \DateTime|bool */
149
    protected $createdAt;
150
151
    /** @var \DateTime|bool */
152
    protected $updatedAt;
153
154
    /**
155
     * Available only for product variants
156
     *
157
     * @var ProductVariantOptionEntity[]|array
158
     */
159
    protected $options = array();
160
161
    /**
162
     * @param string $updatedAt
163
     *
164
     * @return static
165
     */
166
    public function setUpdatedAt($updatedAt)
167
    {
168
        $this->updatedAt = date_create($updatedAt);
169
170
        return $this;
171
    }
172
173
    /**
174
     * @param string $createdAt
175
     *
176
     * @return static
177
     */
178
    public function setCreatedAt($createdAt)
179
    {
180
        $this->createdAt = date_create($createdAt);
181
182
        return $this;
183
    }
184
185
    /**
186
     * @param array $data
187
     *
188
     * @return static
189
     */
190
    public function setShipping($data)
191
    {
192
        $this->shipping = new ProductShippingEntity($data);
193
194
        return $this;
195
    }
196
197
    /**
198
     * @param array $data
199
     *
200
     * @return static
201
     */
202
    public function setCategories($data)
203
    {
204
        foreach ($data as $plainCategory) {
205
            /** Both ProductCategoryEntity fields array and simple title are possible */
206
            $this->categories[] = is_array($plainCategory)
207
                ? new ProductCategoryEntity($plainCategory)
208
                : $plainCategory
209
            ;
210
        }
211
212
        return $this;
213
    }
214
215
    /**
216
     * @param array[]|static[] $data
217
     *
218
     * @return static
219
     */
220
    public function setVariants($data)
221
    {
222
        foreach ($data as $variant) {
223
            $this->addVariant($variant);
224
        }
225
226
        return $this;
227
    }
228
229
    /**
230
     * @param array|static $variant
231
     *
232
     * @return self
233
     */
234
    public function addVariant($variant)
235
    {
236
        if (is_array($variant)) {
237
            $variant = new static($variant);
238
            $variant->setCurrency($this->getCurrency());
239
        }
240
241
        $this->variants[] = $variant;
242
243
        return $this;
244
    }
245
246
    /**
247
     * Set product variant option
248
     *
249
     * @param array $options
250
     *
251
     * @return self
252
     */
253
    public function setOptions($options)
254
    {
255
        if (is_array($options)) {
0 ignored issues
show
introduced by
The condition is_array($options) is always true.
Loading history...
256
            foreach ($options as $option) {
257
                $this->options[] = new ProductVariantOptionEntity($option);
258
            }
259
        }
260
261
        return $this;
262
    }
263
264
    /**
265
     * Add product variant option
266
     *
267
     * @param string $name
268
     * @param string $value
269
     *
270
     * @return self
271
     */
272
    public function addOption($name, $value)
273
    {
274
        $this->options[] = new ProductVariantOptionEntity(compact('name', 'value'));
275
276
        return $this;
277
    }
278
279
    /**
280
     * @return array
281
     */
282
    public function getImagesUuid()
283
    {
284
        return array_map(
285
            function ($imageName) {
286
                return substr($imageName, 0, strpos($imageName, '.'));
287
            },
288
            $this->getImages()
289
        );
290
    }
291
292
    /**
293
     * @param float $salePrice
294
     *
295
     * @return self
296
     */
297
    public function setSalePrice($salePrice)
298
    {
299
        $this->salePrice = $salePrice;
300
        $this->onSales = $salePrice > 0 && (!$this->getPrice() || $this->getPrice() > $salePrice);
301
302
        return $this;
303
    }
304
305
    /**
306
     * Whether this product entity represents product variant and should be linked to a product
307
     *
308
     * @return bool
309
     */
310
    public function isVariant()
311
    {
312
        return $this->getParent() || $this->getProduct() || count($this->getOptions());
313
    }
314
}
315