Code Duplication    Length = 117-117 lines in 2 locations

src/Brownie/CartsGuru/Model/Base/DataModel.php 1 location

@@ 19-135 (lines=117) @@
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
    public function toArray()
42
    {
43
        $args = parent::toArray();
44
        if ($args['items']) {
45
            $list = array();
46
            foreach ($args['items']->toArray() as $item) {
47
                $list[] = array_filter($item->toArray());
48
            }
49
            $args['items'] = $list;
50
        }
51
        return array_filter($args);
52
    }
53
54
    /**
55
     * Add item.
56
     *
57
     * @param Item  $item   Product.
58
     *
59
     * @return self
60
     */
61
    public function addItem(Item $item)
62
    {
63
        if (is_null($this->getItemList())) {
64
            $this->setItemList(new ItemList());
65
        }
66
        $this->getItemList()->add($item);
67
        return $this;
68
    }
69
70
    /**
71
     * Set item list.
72
     *
73
     * @param ItemList      $itemList       Item list.
74
     *
75
     * @return self
76
     */
77
    private function setItemList(ItemList $itemList)
78
    {
79
        $this->fields['items'] = $itemList;
80
        return $this;
81
    }
82
83
    /**
84
     * Return item list.
85
     *
86
     * @return ItemList
87
     */
88
    private function getItemList()
89
    {
90
        return $this->fields['items'];
91
    }
92
93
    /**
94
     * Validates contact data.
95
     *
96
     * @throws ValidateException
97
     */
98
    public function validate()
99
    {
100
        $args = array_filter($this->toArray());
101
102
        $keys = array_diff($this->getRequiredFields(), array_keys($args));
103
104
        if (!empty($keys)) {
105
            throw new ValidateException('No required fields: ' . implode(', ', $keys));
106
        }
107
108
        /**
109
         * @var $item   Item    Product.
110
         */
111
        foreach ($this->getItemList()->toArray() as $item) {
112
            $item->validate();
113
        }
114
    }
115
116
    /**
117
     * Returns a list of required fields.
118
     *
119
     * @return array
120
     */
121
    protected function getRequiredFields()
122
    {
123
        return $this->requiredFields;
124
    }
125
126
    /**
127
     * Returns a endpoint name.
128
     *
129
     * @return string
130
     */
131
    public function getEndpoint()
132
    {
133
        return $this->endpoint;
134
    }
135
}
136

src/Brownie/CartsGuru/Model/DataModel.php 1 location

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