DataObject::getDataUsingMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright © 2017 Toan Nguyen. All rights reserved.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Gojira\Framework\Data;
10
11
/**
12
 * Class DataObject
13
 *
14
 * @package Gojira\Framework\Data
15
 * @author  Toan Nguyen <[email protected]>
16
 */
17
class DataObject implements DataInterface
18
{
19
    /**
20
     * Object attributes
21
     *
22
     * @var array
23
     */
24
    protected $_data = [];
25
26
    /**
27
     * Constructor
28
     *
29
     * By default is looking for first argument as array and assigns it as object attributes
30
     * This behavior may change in child classes
31
     *
32
     * @param array $data
33
     */
34
    public function __construct(array $data = [])
35
    {
36
        $this->_data = $data;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function addData(array $arr)
43
    {
44
        foreach ($arr as $index => $value) {
45
            $this->setData($index, $value);
46
        }
47
48
        return $this;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 View Code Duplication
    public function setData($key, $value)
55
    {
56
        if ($key === (array)$key) {
57
            $this->_data = $key;
58
        } else {
59
            $this->_data[$key] = $value;
60
        }
61
62
        return $this;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function unsetData($key = null)
69
    {
70
        if ($key === null) {
71
            $this->setData([]);
0 ignored issues
show
Bug introduced by
The call to setData() misses a required argument $value.

This check looks for function calls that miss required arguments.

Loading history...
72
        } elseif (is_string($key)) {
73
            if (isset($this->_data[$key]) || array_key_exists($key, $this->_data)) {
74
                unset($this->_data[$key]);
75
            }
76
        } elseif ($key === (array)$key) {
77
            foreach ($key as $element) {
78
                $this->unsetData($element);
79
            }
80
        }
81
82
        return $this;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function setDataUsingMethod($key, $args = [])
89
    {
90
        $method = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
91
        $this->{$method}($args);
92
93
        return $this;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getData($key = '', $index = null)
100
    {
101
        if ('' === $key) {
102
            return $this->_data;
103
        }
104
        /* process a/b/c key as ['a']['b']['c'] */
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
105
        if (strpos($key, '/')) {
106
            $data = $this->getDataByPath($key);
107
        } else {
108
            $data = $this->_getData($key);
109
        }
110
        if ($index !== null) {
111
            if ($data === (array)$data) {
112
                $data = isset($data[$index]) ? $data[$index] : null;
113
            } elseif (is_string($data)) {
114
                $data = explode(PHP_EOL, $data);
115
                $data = isset($data[$index]) ? $data[$index] : null;
116
            } elseif ($data instanceof \Gojira\Framework\Data\DataObject) {
117
                $data = $data->getData($index);
118
            } else {
119
                $data = null;
120
            }
121
        }
122
123
        return $data;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function getDataByKey($key)
130
    {
131
        return $this->_getData($key);
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function getDataByPath($path)
138
    {
139
        $keys = explode('/', $path);
140
        $data = $this->_data;
141
        foreach ($keys as $key) {
142
            if ((array)$data === $data && isset($data[$key])) {
143
                $data = $data[$key];
144
            } elseif ($data instanceof \Gojira\Framework\Data\DataObject) {
145
                $data = $data->getDataByKey($key);
146
            } else {
147
                return null;
148
            }
149
        }
150
151
        return $data;
152
    }
153
154
    /**
155
     * Get value from _data array without parse key
156
     *
157
     * @param   string $key
158
     *
159
     * @return  mixed
160
     */
161
    protected function _getData($key)
162
    {
163
        if (isset($this->_data[$key])) {
164
            return $this->_data[$key];
165
        }
166
167
        return null;
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function getDataUsingMethod($key, $args = null)
174
    {
175
        $method = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
176
177
        return $this->{$method}($args);
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    public function hasData($key = '')
184
    {
185
        if (empty($key) || !is_string($key)) {
186
            return !empty($this->_data);
187
        }
188
189
        return array_key_exists($key, $this->_data);
190
    }
191
}
192