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\Product; |
9
|
|
|
use LauLamanApps\IzettleApi\API\Product\ProductCollection; |
10
|
|
|
use LauLamanApps\IzettleApi\Client\ImageParser; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use Ramsey\Uuid\Uuid; |
13
|
|
|
|
14
|
|
|
final class ProductParser |
15
|
|
|
{ |
16
|
2 |
View Code Duplication |
public static function createFromResponse(ResponseInterface $response): array |
17
|
|
|
{ |
18
|
2 |
|
$products = []; |
19
|
2 |
|
$data = json_decode($response->getBody()->getContents(), true); |
20
|
|
|
|
21
|
2 |
|
foreach ($data as $purchase) { |
22
|
1 |
|
$products[] = self::parse($purchase); |
23
|
|
|
} |
24
|
|
|
|
25
|
2 |
|
return $products; |
26
|
|
|
} |
27
|
|
|
|
28
|
2 |
|
public static function parseArray(array $products): ProductCollection |
29
|
|
|
{ |
30
|
2 |
|
$productCollection = new ProductCollection(); |
31
|
|
|
|
32
|
2 |
|
foreach ($products as $product) { |
33
|
1 |
|
$productCollection->add(self::parse($product)); |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
return $productCollection; |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
private static function parse(array $product): Product |
40
|
|
|
{ |
41
|
2 |
|
return Product::create( |
42
|
2 |
|
Uuid::fromString($product['uuid']), |
43
|
2 |
|
CategoryParser::parseArray($product['categories']), |
44
|
2 |
|
$product['name'], |
45
|
2 |
|
$product['description'], |
46
|
2 |
|
ImageParser::parseArray($product['imageLookupKeys']), |
47
|
2 |
|
VariantParser::parseArray($product['variants']), |
48
|
2 |
|
$product['externalReference'], |
49
|
2 |
|
$product['etag'], |
50
|
2 |
|
new DateTime($product['updated']), |
51
|
2 |
|
Uuid::fromString($product['updatedBy']), |
52
|
2 |
|
new DateTime($product['created']), |
53
|
2 |
|
(float) $product['vatPercentage'] |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|