Passed
Branch master (776013)
by payever
03:51
created

ProductRequestEntity::getImagesUuid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
17
/**
18
 * PHP version 5.4 and 7
19
 *
20
 * @package   Payever\Products
21
 * @author    Hennadii.Shymanskyi <[email protected]>
22
 * @copyright 2017-2019 payever GmbH
23
 * @license   MIT <https://opensource.org/licenses/MIT>
24
 *
25
 * @method string getExternalId()
26
 * @method string[] getImages()
27
 * @method string[] getImagesUrl()
28
 * @method bool getEnabled()
29
 * @method bool getInventoryTrackingEnabled()
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 string getSku()
39
 * @method int getInventory()
40
 * @method string getBarcode()
41
 * @method string getType()
42
 * @method self[]|null 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 setEnabled(bool $enabled)
50
 * @method self setInventoryTrackingEnabled(bool $enabled)
51
 * @method self setUuid(string $uuid)
52
 * @method self setBusinessUuid(string $businessUuid)
53
 * @method self setCurrency(string $currency)
54
 * @method self setTitle(string $title)
55
 * @method self setDescription(string $description)
56
 * @method self setPrice(float $price)
57
 * @method self setSku(string $sku)
58
 * @method self setInventory(int $inventory)
59
 * @method self setBarcode(string $barcode)
60
 * @method self setType(string $type)
61
 */
62
class ProductRequestEntity extends RequestEntity
63
{
64
    const UNDERSCORE_ON_SERIALIZATION = false;
65
66
    /**
67
     * Subscription external id.
68
     * Required for all requests.
69
     *
70
     * @var string
71
     */
72
    protected $externalId;
73
74
    /** @var array */
75
    protected $images = [];
76
77
    /** @var array */
78
    protected $imagesUrl = [];
79
80
    /** @var bool */
81
    protected $enabled = true;
82
83
    /** @var bool */
84
    protected $inventoryTrackingEnabled = false;
85
86
    /** @var string */
87
    protected $uuid;
88
89
    /** @var string */
90
    protected $businessUuid;
91
92
    /** @var ProductCategoryEntity[]|string[] */
93
    protected $categories;
94
95
    /** @var string */
96
    protected $currency;
97
98
    /** @var string */
99
    protected $title;
100
101
    /** @var string */
102
    protected $description;
103
104
    /** @var float */
105
    protected $price;
106
107
    /** @var float|null */
108
    protected $salePrice;
109
110
    /**
111
     * Read as 'salePriceHidden'
112
     *
113
     * @var bool
114
     */
115
    protected $hidden = true;
116
117
    /** @var float */
118
    protected $sortPrice;
119
120
    /** @var string */
121
    protected $sku;
122
123
    /** @var int */
124
    protected $inventory;
125
126
    /** @var string */
127
    protected $barcode;
128
129
    /** @var string */
130
    protected $type;
131
132
    /** @var self[]|null */
133
    protected $variants;
134
135
    /** @var ProductShippingEntity|null */
136
    protected $shipping;
137
138
    /** @var \DateTime|bool */
139
    protected $createdAt;
140
141
    /** @var \DateTime|bool */
142
    protected $updatedAt;
143
144
    /**
145
     * @param string $updatedAt
146
     *
147
     * @return static
148
     */
149
    public function setUpdatedAt($updatedAt)
150
    {
151
        $this->updatedAt = date_create($updatedAt);
152
153
        return $this;
154
    }
155
156
    /**
157
     * @param string $createdAt
158
     *
159
     * @return static
160
     */
161
    public function setCreatedAt($createdAt)
162
    {
163
        $this->createdAt = date_create($createdAt);
164
165
        return $this;
166
    }
167
168
    /**
169
     * @param array $data
170
     *
171
     * @return static
172
     */
173
    public function setShipping($data)
174
    {
175
        $this->shipping = new ProductShippingEntity($data);
176
177
        return $this;
178
    }
179
180
    /**
181
     * @param array $data
182
     *
183
     * @return static
184
     */
185
    public function setCategories($data)
186
    {
187
        foreach ($data as $plainCategory) {
188
            /** Both ProductCategoryEntity fields array and simple title are possible */
189
            $this->categories[] = is_array($plainCategory)
190
                ? new ProductCategoryEntity($plainCategory)
191
                : $plainCategory
192
            ;
193
        }
194
195
        return $this;
196
    }
197
198
    /**
199
     * @param array $data
200
     *
201
     * @return static
202
     */
203
    public function setVariants($data)
204
    {
205
        foreach ($data as $plainVariant) {
206
            $variant = new static($plainVariant);
207
208
            $variant->setCurrency($this->getCurrency());
209
210
            $this->variants[] = $variant;
211
        }
212
213
        return $this;
214
    }
215
216
    /**
217
     * @return array
218
     */
219
    public function getImagesUuid()
220
    {
221
        return array_map(
222
            function ($imageName) {
223
                return substr($imageName, 0, strpos($imageName, '.'));
224
            },
225
            $this->getImages()
226
        );
227
    }
228
229
    /**
230
     * @param float $price
231
     *
232
     * @return self
233
     */
234
    public function setSalePrice($price)
235
    {
236
        $this->salePrice = $price;
237
        $this->hidden = !$price;
238
239
        return $this;
240
    }
241
}
242