Completed
Push — master ( 267dd6...b89699 )
by Johnny
14:25 queued 04:58
created

ArgumentObject::camelCase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace Redbox\Cli\Object;
3
4
class ArgumentObject {
5
6
    /**
7
     * @var array
8
     */
9
    protected $modelData = [];
10
11
    /**
12
     * Construct this magic object. Give it a array amd
13
     * it will turn into an object. Its a hydration.
14
     *
15
     * Object constructor.
16
     * @param $array
17
     */
18 16
    public function __construct($array = array())
19
    {
20 16
        $this->mapTypes($array);
21 16
    }
22
23
    /**
24
     * Convert a string to camelCase
25
     *
26
     * @param  string $value
27
     * @return string
28
     */
29 1
    public static function camelCase($value)
30
    {
31 1
        $value = ucwords(str_replace(array('-', '_'), ' ', $value));
32 1
        $value = str_replace(' ', '', $value);
33 1
        $value[0] = strtolower($value[0]);
34 1
        return $value;
35
    }
36
37
    /**
38
     * Initialize this object's properties from an array.
39
     *
40
     * @param array $array Used to seed this object's properties.
41
     * @return void
42
     */
43 16
    protected function mapTypes($array)
44
    {
45 16
        foreach ($array as $key => $val) {
46 16
            if (!property_exists($this, $this->keyType($key)) &&
47 16
                property_exists($this, $key)) {
48 16
                $this->$key = $val;
49 16
                unset($array[$key]);
50 1
            } elseif (property_exists($this, $camelKey = self::camelCase($key))) {
51 16
                $this->$camelKey = $val;
52
            }
53
        }
54 16
        $this->modelData = $array;
55 16
    }
56
57
    /**
58
     * Return the keyType for a given key.
59
     *
60
     * @param $key
61
     * @return string
62
     */
63 16
    protected function keyType($key)
64
    {
65 16
        return $key."Type";
66
    }
67
68
    /**
69
     * Return the dataType for a key.
70
     *
71
     * @param $key
72
     * @return string
73
     */
74
    protected function dataType($key)
75
    {
76
        return $key."DataType";
77
    }
78
79
    /**
80
     * Check to see if a given key is set or not.
81
     *
82
     * @param $key
83
     * @return bool
84
     */
85
    public function __isset($key)
86
    {
87
        return isset($this->modelData[$key]);
88
    }
89
90
    /**
91
     * Unset a given key.
92
     *
93
     * @param $key
94
     */
95
    public function __unset($key)
96
    {
97
        unset($this->modelData[$key]);
98
    }
99
}
100