1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LauLamanApps\IzettleApi\Client\Purchase; |
6
|
|
|
|
7
|
|
|
use LauLamanApps\IzettleApi\API\Image; |
8
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\Product; |
9
|
|
|
use Money\Currency; |
10
|
|
|
use Money\Money; |
11
|
|
|
use Ramsey\Uuid\Uuid; |
12
|
|
|
use Ramsey\Uuid\UuidInterface; |
13
|
|
|
|
14
|
|
|
final class ProductBuilder implements ProductBuilderInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @return Product[] |
18
|
|
|
*/ |
19
|
3 |
|
public function buildFromArray(array $products, Currency $currency): array |
20
|
|
|
{ |
21
|
3 |
|
$data = []; |
22
|
|
|
|
23
|
3 |
|
foreach ($products as $product) { |
24
|
3 |
|
$data[] = $this->build($product, $currency); |
25
|
|
|
} |
26
|
|
|
|
27
|
3 |
|
return $data; |
28
|
|
|
} |
29
|
|
|
|
30
|
3 |
|
private function build(array $product, Currency $currency): Product |
31
|
|
|
{ |
32
|
3 |
|
return new Product( |
33
|
3 |
|
$this->getUuidFromKey('productUuid', $product), |
34
|
3 |
|
$this->getUuidFromKey('variantUuid', $product), |
35
|
3 |
|
$this->getFromKey('name', $product), |
36
|
3 |
|
$this->getFromKey('variantName', $product), |
37
|
3 |
|
$this->getIntFromKey('quantity', $product), |
38
|
3 |
|
$this->getMoneyFromKey('unitPrice', $currency, $product), |
39
|
3 |
|
$this->getFromKey('vatPercentage', $product), |
40
|
3 |
|
$this->getMoneyFromKey('rowTaxableAmount', $currency, $product), |
41
|
3 |
|
$this->getImageFromKey('imageLookupKey', $product), |
42
|
3 |
|
$this->getFromKey('autoGenerated', $product), |
43
|
3 |
|
$this->getFromKey('libraryProduct', $product) |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
private function getFromKey($key, array $data) |
48
|
|
|
{ |
49
|
3 |
|
if (!array_key_exists($key, $data)) { |
50
|
3 |
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
3 |
|
return $data[$key]; |
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
private function getIntFromKey(string $key, array $data): int |
57
|
|
|
{ |
58
|
3 |
|
return (int) $this->getFromKey($key, $data); |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
private function getUuidFromKey(string $key, array $data): ?UuidInterface |
62
|
|
|
{ |
63
|
3 |
|
$data = $this->getFromKey($key, $data); |
64
|
3 |
|
if (!is_null($data)) { |
65
|
1 |
|
return Uuid::fromString($data); |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
return $data; |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
private function getMoneyFromKey(string $key, Currency $currency, array $data): Money |
72
|
|
|
{ |
73
|
3 |
|
return new Money($this->getFromKey($key, $data), $currency); |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
private function getImageFromKey(string $key, array $data): ?Image |
77
|
|
|
{ |
78
|
3 |
|
$data = $this->getFromKey($key, $data); |
79
|
3 |
|
if (!is_null($data)) { |
80
|
1 |
|
return new Image($data); |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
return $data; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|