1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @category Brownie/CartsGuru |
4
|
|
|
* @author Brownie <[email protected]> |
5
|
|
|
* @license http://www.gnu.org/copyleft/lesser.html |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Brownie\CartsGuru\Model\Base; |
9
|
|
|
|
10
|
|
|
use Brownie\CartsGuru\Exception\ValidateException; |
11
|
|
|
use Brownie\CartsGuru\Model\Item; |
12
|
|
|
use Brownie\CartsGuru\Model\ItemList; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Data model. |
16
|
|
|
* |
17
|
|
|
* @method DataModel setSiteId($siteId) Sets siteId. |
18
|
|
|
*/ |
19
|
|
|
abstract class DataModel extends ArrayList |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* List of required fields. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $requiredFields = array(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Endpoint name. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $endpoint = ''; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns the field list as an array. |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
4 |
|
public function toArray() |
42
|
|
|
{ |
43
|
4 |
|
$args = parent::toArray(); |
44
|
4 |
|
if ($args['items']) { |
45
|
2 |
|
$list = array(); |
46
|
2 |
|
foreach ($args['items']->toArray() as $item) { |
47
|
2 |
|
$list[] = array_filter($item->toArray()); |
48
|
|
|
} |
49
|
2 |
|
$args['items'] = $list; |
50
|
|
|
} |
51
|
4 |
|
return array_filter($args); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Add item. |
56
|
|
|
* |
57
|
|
|
* @param Item $item Product. |
58
|
|
|
* |
59
|
|
|
* @return self |
60
|
|
|
*/ |
61
|
2 |
|
public function addItem(Item $item) |
62
|
|
|
{ |
63
|
2 |
|
if (is_null($this->getItemList())) { |
64
|
2 |
|
$this->setItemList(new ItemList()); |
65
|
|
|
} |
66
|
2 |
|
$this->getItemList()->add($item); |
67
|
2 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set item list. |
72
|
|
|
* |
73
|
|
|
* @param ItemList $itemList Item list. |
74
|
|
|
* |
75
|
|
|
* @return self |
76
|
|
|
*/ |
77
|
2 |
|
private function setItemList(ItemList $itemList) |
78
|
|
|
{ |
79
|
2 |
|
$this->fields['items'] = $itemList; |
80
|
2 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Return item list. |
85
|
|
|
* |
86
|
|
|
* @return ItemList |
87
|
|
|
*/ |
88
|
2 |
|
private function getItemList() |
89
|
|
|
{ |
90
|
2 |
|
return $this->fields['items']; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Validates contact data. |
95
|
|
|
* |
96
|
|
|
* @throws ValidateException |
97
|
|
|
*/ |
98
|
4 |
|
public function validate() |
99
|
|
|
{ |
100
|
4 |
|
$args = array_filter($this->toArray()); |
101
|
|
|
|
102
|
4 |
|
$keys = array_diff($this->getRequiredFields(), array_keys($args)); |
103
|
|
|
|
104
|
4 |
|
if (!empty($keys)) { |
105
|
2 |
|
throw new ValidateException('No required fields: ' . implode(', ', $keys)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @var $item Item Product. |
110
|
|
|
*/ |
111
|
2 |
|
foreach ($this->getItemList()->toArray() as $item) { |
112
|
2 |
|
$item->validate(); |
113
|
|
|
} |
114
|
2 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns a list of required fields. |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
4 |
|
protected function getRequiredFields() |
122
|
|
|
{ |
123
|
4 |
|
return $this->requiredFields; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns a endpoint name. |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
2 |
|
public function getEndpoint() |
132
|
|
|
{ |
133
|
2 |
|
return $this->endpoint; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|