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\Purchase; |
9
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\User; |
10
|
|
|
use Money\Currency; |
11
|
|
|
use Money\Money; |
12
|
|
|
use Ramsey\Uuid\Uuid; |
13
|
|
|
|
14
|
|
|
final class PurchaseParser |
15
|
|
|
{ |
16
|
3 |
|
public static function parseArray(array $purchases): array |
17
|
|
|
{ |
18
|
3 |
|
$data = []; |
19
|
|
|
|
20
|
3 |
|
foreach ($purchases as $purchase) { |
21
|
1 |
|
$data[] = self::parse($purchase); |
22
|
|
|
} |
23
|
|
|
|
24
|
3 |
|
return $data; |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
public static function createFromResponse($response): Purchase |
28
|
|
|
{ |
29
|
1 |
|
$data = json_decode($response->getBody()->getContents(), true); |
30
|
|
|
|
31
|
1 |
|
return self::parse($data); |
32
|
|
|
} |
33
|
|
|
|
34
|
3 |
|
public static function parse(array $purchase): Purchase |
35
|
|
|
{ |
36
|
3 |
|
$currency = new Currency($purchase['currency']); |
37
|
|
|
|
38
|
3 |
|
$coordinates = null; |
39
|
3 |
|
if (array_key_exists('gpsCoordinates', $purchase)) { |
40
|
3 |
|
$coordinates = CoordinatesParser::parse($purchase['gpsCoordinates']); |
41
|
|
|
} |
42
|
|
|
|
43
|
3 |
|
$published = null; |
44
|
3 |
|
if (array_key_exists('published', $purchase)) { |
45
|
3 |
|
$published = $purchase['published']; |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
return new Purchase( |
49
|
3 |
|
$purchase['purchaseUUID'], |
50
|
3 |
|
Uuid::fromString($purchase['purchaseUUID1']), |
51
|
3 |
|
new DateTime($purchase['timestamp']), |
52
|
3 |
|
$coordinates, |
53
|
3 |
|
$purchase['country'], |
54
|
3 |
|
new User($purchase['userId'], $purchase['userDisplayName']), |
55
|
3 |
|
$purchase['organizationId'], |
56
|
3 |
|
$purchase['purchaseNumber'], |
57
|
3 |
|
new Money($purchase['amount'], $currency), |
58
|
3 |
|
new Money($purchase['vatAmount'], $currency), |
59
|
3 |
|
ProductParser::parseArray($purchase['products'], $currency), |
60
|
3 |
|
PaymentParser::parseArray($purchase['payments'], $currency), |
61
|
3 |
|
VatParser::parseArray($purchase['groupedVatAmounts'], $currency), |
62
|
3 |
|
$purchase['receiptCopyAllowed'], |
63
|
3 |
|
$published, |
64
|
3 |
|
$purchase['refund'], |
65
|
3 |
|
$purchase['refunded'] |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|