|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Bpost\BpostApiClient\Bpost\ProductConfiguration; |
|
5
|
|
|
|
|
6
|
|
|
use SimpleXMLElement; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class DeliveryMethod |
|
10
|
|
|
*/ |
|
11
|
|
|
class DeliveryMethod |
|
12
|
|
|
{ |
|
13
|
|
|
public const DELIVERY_METHOD_NAME_HOME_OR_OFFICE = 'home or office'; |
|
14
|
|
|
public const DELIVERY_METHOD_NAME_PICKUP_POINT = 'pick-up point'; |
|
15
|
|
|
public const DELIVERY_METHOD_NAME_PARCEL_LOCKER = 'parcel locker'; |
|
16
|
|
|
public const DELIVERY_METHOD_NAME_CLICK_AND_COLLECT = 'Click & Collect'; |
|
17
|
|
|
|
|
18
|
|
|
private string $name; |
|
19
|
|
|
private string $visibility; |
|
20
|
|
|
/** @var Product[] */ |
|
21
|
|
|
private array $products = []; |
|
22
|
|
|
|
|
23
|
|
|
public static function createFromXML(SimpleXMLElement $xml): self |
|
24
|
|
|
{ |
|
25
|
|
|
$attributes = $xml->attributes(); |
|
26
|
|
|
$children = $xml->children(); |
|
27
|
|
|
|
|
28
|
|
|
$instance = new self(); |
|
29
|
|
|
if (isset($attributes['name'])) { |
|
30
|
|
|
$instance->setName((string) $attributes['name']); |
|
31
|
|
|
} |
|
32
|
|
|
// Correction de l'attribut: visibility (et fallback si l’API renvoie l’ancienne coquille) |
|
33
|
|
|
if (isset($attributes['visibility'])) { |
|
34
|
|
|
$instance->setVisibility((string) $attributes['visibility']); |
|
35
|
|
|
} elseif (isset($attributes['visiblity'])) { |
|
36
|
|
|
$instance->setVisibility((string) $attributes['visiblity']); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if (isset($children->product)) { |
|
40
|
|
|
foreach ($children->product as $productXml) { |
|
41
|
|
|
$instance->addProduct(Product::createFromXML($productXml)); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $instance; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getName(): string |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->name; |
|
51
|
|
|
} |
|
52
|
|
|
public function setName(string $name): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->name = $name; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function getVisibility(): string |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->visibility; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function setVisibility(string $visibility): void |
|
63
|
|
|
{ |
|
64
|
|
|
$this->visibility = $visibility; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function isVisibleAndActive(): bool |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->getVisibility() === Visibility::DELIVERY_METHOD_VISIBILITY_VISIBLE; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getProducts(): array |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->products; |
|
75
|
|
|
} |
|
76
|
|
|
public function addProduct(Product $product): void |
|
77
|
|
|
{ |
|
78
|
|
|
$this->products[] = $product; |
|
79
|
|
|
} |
|
80
|
|
|
} |