Completed
Push — master ( db6b3b...545c49 )
by Oss
02:55
created

DataModel   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 11 3
A setItemList() 0 4 1
A validate() 0 12 3
A addItem() 0 7 2
A getItemList() 0 3 1
A getRequiredFields() 0 3 1
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;
0 ignored issues
show
Bug Best Practice introduced by
The property fields does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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) {
0 ignored issues
show
Bug introduced by
The method getItems() does not exist on Brownie\CartsGuru\Model\DataModel. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        foreach ($this->/** @scrutinizer ignore-call */ getItems()->toArray() as $item) {
Loading history...
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