Passed
Branch master (5e8728)
by Oss
02:18
created

DataModel::getEndpoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
     * List of required fields.
21
     *
22
     * @var array
23
     */
24
    protected $requiredFields = array();
25
26
    /**
27
     * Endpoint name.
28
     *
29
     * @var string
30
     */
31
    protected $endpoint = '';
32
33
    /**
34
     * Returns the field list as an array.
35
     *
36
     * @return array
37
     */
38 4
    public function toArray()
39
    {
40 4
        $args = parent::toArray();
41 4
        if ($args['items']) {
42 2
            $list = array();
43 2
            foreach ($args['items']->toArray() as $item) {
44 2
                $list[] = array_filter($item->toArray());
45
            }
46 2
            $args['items'] = $list;
47
        }
48 4
        return array_filter($args);
49
    }
50
51
    /**
52
     * Add item.
53
     *
54
     * @param Item  $item   Product.
55
     *
56
     * @return self
57
     */
58 2
    public function addItem(Item $item)
59
    {
60 2
        if (is_null($this->getItemList())) {
61 2
            $this->setItemList(new ItemList());
62
        }
63 2
        $this->getItemList()->add($item);
64 2
        return $this;
65
    }
66
67
    /**
68
     * Set item list.
69
     *
70
     * @param ItemList      $itemList       Item list.
71
     *
72
     * @return self
73
     */
74 2
    private function setItemList(ItemList $itemList)
75
    {
76 2
        $this->fields['items'] = $itemList;
77 2
        return $this;
78
    }
79
80
    /**
81
     * Return item list.
82
     *
83
     * @return ItemList
84
     */
85 2
    private function getItemList()
86
    {
87 2
        return $this->fields['items'];
88
    }
89
90
    /**
91
     * Validates contact data.
92
     *
93
     * @throws ValidateException
94
     */
95 4
    public function validate()
96
    {
97 4
        $args = array_filter($this->toArray());
98
99 4
        $keys = array_diff($this->getRequiredFields(), array_keys($args));
100
101 4
        if (!empty($keys)) {
102 2
            throw new ValidateException('No required fields: ' . implode(', ', $keys));
103
        }
104
105
        /**
106
         * @var $item   Item    Product.
107
         */
108 2
        foreach ($this->getItemList()->toArray() as $item) {
109 2
            $item->validate();
110
        }
111 2
    }
112
113
    /**
114
     * Returns a list of required fields.
115
     *
116
     * @return array
117
     */
118 4
    protected function getRequiredFields()
119
    {
120 4
        return $this->requiredFields;
121
    }
122
123
    /**
124
     * Returns a endpoint name.
125
     *
126
     * @return string
127
     */
128 2
    public function getEndpoint()
129
    {
130 2
        return $this->endpoint;
131
    }
132
}
133