Completed
Push — 1.x ( 0f5ece...c9692e )
by Asao
36:16
created

Accessor::getObj()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 19
rs 8.8571
cc 5
eloc 12
nc 5
nop 3
1
<?php
2
namespace Tuum\Form\Data;
3
4
use ArrayAccess;
5
6
class Accessor
7
{
8
    /**
9
     * get value from an array or an object.
10
     *
11
     * @param array|object $data
12
     * @param string       $key
13
     * @param mixed        $default
14
     * @return mixed
15
     */
16
    public static function get($data, $key, $default = null)
17
    {
18
        if (self::isArrayAccess($data)) {
19
            return self::getArray($data, $key, $default);
0 ignored issues
show
Bug introduced by
It seems like $data defined by parameter $data on line 16 can also be of type object; however, Tuum\Form\Data\Accessor::getArray() does only seem to accept array|object<ArrayAccess>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
20
        }
21
        if (is_object($data)) {
22
            return self::getObj($data, $key, $default);
23
        }
24
25
        return $default;
26
    }
27
28
    /**
29
     * @param mixed $data
30
     * @return bool
31
     */
32
    public static function isArrayAccess($data)
33
    {
34
        if ((is_array($data))) {
35
            return true;
36
        }
37
        if (is_object($data) &&  $data instanceof ArrayAccess) {
38
            return true;
39
        }
40
        return false;
41
    }
42
43
    /**
44
     * @param array|ArrayAccess $data
45
     * @param string            $key
46
     * @param null              $default
47
     * @return mixed
48
     */
49
    public static function getArray($data, $key, $default = null)
50
    {
51
        return array_key_exists($key, $data) ? $data[$key]: $default;
52
    }
53
54
    /**
55
     * @param object $data
56
     * @param string $key
57
     * @param null   $default
58
     * @return mixed
59
     */
60
    public static function getObj($data, $key, $default = null)
61
    {
62
        $method = 'get' . $key;
63
        if (method_exists($data, $method)) {
64
            return $data->$method();
65
        }
66
        $method = 'get' . str_replace('_', '', $key);
67
        if (method_exists($data, $method)) {
68
            return $data->$method();
69
        }
70
        if (method_exists($data, 'get')) {
71
            return $data->get($key);
72
        }
73
        if (isset($data->$key)) {
74
            return $data->$key;
75
        }
76
77
        return $default;
78
    }
79
80
    /**
81
     * check if $data has a $key.
82
     * $data can be an array or an object.
83
     *
84
     * @param array|object $data
85
     * @param string       $key
86
     * @return bool
87
     */
88
    public static function has($data, $key)
89
    {
90
        if (self::isArrayAccess($data)) {
91
            return array_key_exists($key, $data);
92
        }
93
        if (!is_object($data)) {
94
            return false;
95
        }
96
        if (method_exists($data, 'get' . $key)) {
97
            return true;
98
        }
99
        if (method_exists($data, 'get' . str_replace('_', '', $key))) {
100
            return true;
101
        }
102
        if (isset($data->$key)) {
103
            return true;
104
        }
105
106
        return false;
107
    }
108
}