1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the PayBreak/paybreak-sdk-php package. |
4
|
|
|
* |
5
|
|
|
* (c) PayBreak <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace PayBreak\Sdk\Entities; |
12
|
|
|
|
13
|
|
|
use WNowicki\Generic\AbstractEntity; |
14
|
|
|
use WNowicki\Generic\Exception; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Group Entity |
18
|
|
|
* |
19
|
|
|
* @author EB |
20
|
|
|
* @method $this setId(int $id) |
21
|
|
|
* @method int|null getId() |
22
|
|
|
* @method $this setName(string $name) |
23
|
|
|
* @method string|null getName() |
24
|
|
|
* @package PayBreak\Sdk\Entities |
25
|
|
|
*/ |
26
|
|
|
class GroupEntity extends AbstractEntity |
27
|
|
|
{ |
28
|
|
|
private $products = []; |
29
|
|
|
|
30
|
|
|
protected $properties = [ |
31
|
|
|
'id', |
32
|
|
|
'name', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return ProductEntity[] |
37
|
|
|
*/ |
38
|
3 |
|
public function getProducts() |
39
|
|
|
{ |
40
|
3 |
|
return $this->products; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param ProductEntity $product |
45
|
|
|
*/ |
46
|
1 |
|
public function addProduct(ProductEntity $product) |
47
|
|
|
{ |
48
|
1 |
|
$this->products[] = $product; |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @author EB, WN |
53
|
|
|
* @param array $products |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
2 |
|
public function setProducts(array $products) |
57
|
|
|
{ |
58
|
2 |
|
$this->products = []; |
59
|
|
|
|
60
|
2 |
|
foreach ($products as $product) { |
61
|
2 |
|
if (is_array($product)) { |
62
|
1 |
|
$this->addProduct(ProductEntity::make($product)); |
63
|
1 |
|
continue; |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
if ($product instanceof ProductEntity) { |
67
|
1 |
|
$this->addProduct($product); |
68
|
1 |
|
} |
69
|
2 |
|
} |
70
|
|
|
|
71
|
2 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Make Entity |
76
|
|
|
* |
77
|
|
|
* @author WN |
78
|
|
|
* @param array $components |
79
|
|
|
* @return static |
80
|
|
|
*/ |
81
|
4 |
|
public static function make(array $components) |
82
|
|
|
{ |
83
|
4 |
|
$entity = parent::make($components); |
84
|
|
|
|
85
|
4 |
|
if (array_key_exists('products', $components) && is_array($components['products'])) { |
86
|
2 |
|
$entity->setProducts($components['products']); |
87
|
2 |
|
} |
88
|
|
|
|
89
|
4 |
|
return $entity; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* To Array |
94
|
|
|
* |
95
|
|
|
* @author WN |
96
|
|
|
* @param bool $recursively If set to `true` then toArray(true) will be called on each `Arrayable` property |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
1 |
|
public function toArray($recursively = false) |
100
|
|
|
{ |
101
|
1 |
|
$ar = parent::toArray($recursively); |
102
|
|
|
|
103
|
1 |
|
if ($recursively) { |
104
|
|
|
foreach ($this->getProducts() as $product) { |
105
|
|
|
$ar['products'][] = $product->toArray(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
return $ar; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|