|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LauLamanApps\IzettleApi\Client\Purchase; |
|
6
|
|
|
|
|
7
|
|
|
use DateTime; |
|
8
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\Coordinates; |
|
9
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\Purchase; |
|
10
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\PurchaseHistory; |
|
11
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\User; |
|
12
|
|
|
use Money\Currency; |
|
13
|
|
|
use Money\Money; |
|
14
|
|
|
use Ramsey\Uuid\Uuid; |
|
15
|
|
|
|
|
16
|
|
|
final class PurchaseBuilder implements PurchaseBuilderInterface |
|
17
|
|
|
{ |
|
18
|
|
|
private $coordinatesBuilder; |
|
19
|
|
|
private $productBuilder; |
|
20
|
|
|
private $paymentBuilder; |
|
21
|
|
|
private $vatBuilder; |
|
22
|
|
|
|
|
23
|
2 |
|
public function __construct( |
|
24
|
|
|
CoordinatesBuilderInterface $coordinatesBuilder, |
|
25
|
|
|
ProductBuilderInterface $productBuilder, |
|
26
|
|
|
PaymentBuilderInterface $paymentBuilder, |
|
27
|
|
|
VatBuilderInterface $vatBuilder |
|
28
|
|
|
) { |
|
29
|
2 |
|
$this->coordinatesBuilder = $coordinatesBuilder; |
|
30
|
2 |
|
$this->productBuilder = $productBuilder; |
|
31
|
2 |
|
$this->paymentBuilder = $paymentBuilder; |
|
32
|
2 |
|
$this->vatBuilder = $vatBuilder; |
|
33
|
2 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return Purchase[] |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function buildFromJsonArray(array $purchases): array |
|
39
|
|
|
{ |
|
40
|
1 |
|
$data = []; |
|
41
|
|
|
|
|
42
|
1 |
|
foreach ($purchases as $purchase) { |
|
43
|
1 |
|
$data[] = $this->build($purchase); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
return $data; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
public function buildFromJson(string $jsonData): Purchase |
|
50
|
|
|
{ |
|
51
|
1 |
|
$data = json_decode($jsonData, true); |
|
52
|
|
|
|
|
53
|
1 |
|
return $this->build($data); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
public function build(array $data): Purchase |
|
57
|
|
|
{ |
|
58
|
2 |
|
$currency = new Currency($data['currency']); |
|
59
|
|
|
|
|
60
|
2 |
|
return new Purchase( |
|
61
|
2 |
|
$data['purchaseUUID'], |
|
62
|
2 |
|
Uuid::fromString($data['purchaseUUID1']), |
|
63
|
2 |
|
new DateTime($data['timestamp']), |
|
64
|
2 |
|
$this->getCoordinatesFromKey('gpsCoordinates', $data), |
|
65
|
2 |
|
$data['country'], |
|
66
|
2 |
|
new User($data['userId'], $data['userDisplayName']), |
|
67
|
2 |
|
$data['organizationId'], |
|
68
|
2 |
|
$data['purchaseNumber'], |
|
69
|
2 |
|
new Money($data['amount'], $currency), |
|
70
|
2 |
|
new Money($data['vatAmount'], $currency), |
|
71
|
2 |
|
$this->productBuilder->buildFromJsonArray($data['products'], $currency), |
|
72
|
2 |
|
$this->paymentBuilder->buildFromJsonArray($data['payments'], $currency), |
|
73
|
2 |
|
$this->vatBuilder->createFromJson($data['groupedVatAmounts'], $currency), |
|
74
|
2 |
|
$data['receiptCopyAllowed'], |
|
75
|
2 |
|
$this->getBoolFromKey('published', $data), |
|
76
|
2 |
|
$data['refund'], |
|
77
|
2 |
|
$data['refunded'] |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
private function getBoolFromKey($key, array $data): ?bool |
|
82
|
|
|
{ |
|
83
|
2 |
|
if (!array_key_exists($key, $data)) { |
|
84
|
1 |
|
return null; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
2 |
|
return $data[$key]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
private function getCoordinatesFromKey($key, array $data): ?Coordinates |
|
91
|
|
|
{ |
|
92
|
2 |
|
if (!array_key_exists($key, $data)) { |
|
93
|
1 |
|
return null; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
2 |
|
return $this->coordinatesBuilder->createFromJson($data[$key]); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.