1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LauLamanApps\IzettleApi\Client\Product; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
use LauLamanApps\IzettleApi\API\Product\Discount; |
9
|
|
|
use LauLamanApps\IzettleApi\API\Product\DiscountCollection; |
10
|
|
|
use LauLamanApps\IzettleApi\Client\Universal\ImageBuilderInterface; |
11
|
|
|
use Money\Currency; |
12
|
|
|
use Money\Money; |
13
|
|
|
use Ramsey\Uuid\Uuid; |
14
|
|
|
|
15
|
|
|
final class DiscountBuilder implements DiscountBuilderInterface |
16
|
|
|
{ |
17
|
|
|
private $imageBuilder; |
18
|
|
|
|
19
|
7 |
|
public function __construct(ImageBuilderInterface $imageBuilder) |
20
|
|
|
{ |
21
|
7 |
|
$this->imageBuilder = $imageBuilder; |
22
|
7 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return Discount[] |
26
|
|
|
*/ |
27
|
3 |
|
public function buildFromJson(string $json): array |
28
|
|
|
{ |
29
|
3 |
|
$products = []; |
30
|
3 |
|
$data = json_decode($json, true); |
31
|
3 |
|
foreach ($data as $product) { |
32
|
3 |
|
$products[] = $this->build($product); |
33
|
|
|
} |
34
|
|
|
|
35
|
3 |
|
return $products; |
36
|
|
|
} |
37
|
|
|
|
38
|
2 |
|
public function buildFromArray(array $discounts): DiscountCollection |
39
|
|
|
{ |
40
|
2 |
|
$discountCollection = new DiscountCollection(); |
41
|
|
|
|
42
|
2 |
|
foreach ($discounts as $discount) { |
43
|
2 |
|
$discountCollection->add($this->build($discount)); |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
return $discountCollection; |
47
|
|
|
} |
48
|
|
|
|
49
|
5 |
|
private function build(array $product): Discount |
50
|
|
|
{ |
51
|
5 |
|
return Discount::create( |
52
|
5 |
|
Uuid::fromString($product['uuid']), |
53
|
5 |
|
$product['name'], |
54
|
5 |
|
$product['description'], |
55
|
5 |
|
$this->imageBuilder->buildFromArray($product['imageLookupKeys']), |
56
|
5 |
|
$product['amount'] ? new Money($product['amount']['amount'], new Currency($product['amount']['currencyId'])) : null, |
57
|
5 |
|
$product['percentage'] ? (float) $product['percentage'] : null, |
58
|
5 |
|
$product['externalReference'], |
59
|
5 |
|
$product['etag'], |
60
|
5 |
|
new DateTime($product['updated']), |
61
|
5 |
|
Uuid::fromString($product['updatedBy']), |
62
|
5 |
|
new DateTime($product['created']) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|