|
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; |
|
9
|
|
|
|
|
10
|
|
|
use Brownie\CartsGuru\Model\Base\ArrayList; |
|
11
|
|
|
use Brownie\CartsGuru\Exception\ValidateException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Data model. |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class DataModel extends ArrayList |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Returns the field list as an array. |
|
21
|
|
|
* |
|
22
|
|
|
* @return array |
|
23
|
|
|
*/ |
|
24
|
|
|
public function toArray() |
|
25
|
|
|
{ |
|
26
|
|
|
$args = parent::toArray(); |
|
27
|
|
|
if ($args['items']) { |
|
28
|
|
|
$list = array(); |
|
29
|
|
|
foreach ($args['items']->toArray() as $item) { |
|
30
|
|
|
$list[] = array_filter($item->toArray()); |
|
31
|
|
|
} |
|
32
|
|
|
$args['items'] = $list; |
|
33
|
|
|
} |
|
34
|
|
|
return array_filter($args); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Add item. |
|
39
|
|
|
* |
|
40
|
|
|
* @param Item $item Product. |
|
41
|
|
|
* |
|
42
|
|
|
* @return self |
|
43
|
|
|
*/ |
|
44
|
|
|
public function addItem(Item $item) |
|
45
|
|
|
{ |
|
46
|
|
|
if (is_null($this->getItemList())) { |
|
47
|
|
|
$this->setItemList(new ItemList()); |
|
48
|
|
|
} |
|
49
|
|
|
$this->getItemList()->add($item); |
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Set item list. |
|
55
|
|
|
* |
|
56
|
|
|
* @param ItemList $itemList Item list. |
|
57
|
|
|
* |
|
58
|
|
|
* @return self |
|
59
|
|
|
*/ |
|
60
|
|
|
private function setItemList(ItemList $itemList) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->fields['items'] = $itemList; |
|
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Return item list. |
|
68
|
|
|
* |
|
69
|
|
|
* @return ItemList |
|
70
|
|
|
*/ |
|
71
|
|
|
private function getItemList() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->fields['items']; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Validates contact data. |
|
78
|
|
|
* |
|
79
|
|
|
* @throws ValidateException |
|
80
|
|
|
*/ |
|
81
|
|
|
public function validate() |
|
82
|
|
|
{ |
|
83
|
|
|
$args = array_filter($this->toArray()); |
|
84
|
|
|
|
|
85
|
|
|
$keys = array_diff($this->getRequiredFields(), array_keys($args)); |
|
86
|
|
|
|
|
87
|
|
|
if (!empty($keys)) { |
|
88
|
|
|
throw new ValidateException('No required fields: ' . implode(', ', $keys)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
foreach ($this->getItems()->toArray() as $item) { |
|
|
|
|
|
|
92
|
|
|
$item->validate(); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns a list of required fields. |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function getRequiredFields() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->requiredFields; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|