Passed
Push — master ( d123dc...1b75c0 )
by Johnny
02:40
created

ArgumentObject   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 94
ccs 21
cts 24
cp 0.875
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __isset() 0 3 1
A mapTypes() 0 13 5
A __unset() 0 3 1
A keyType() 0 3 1
A camelCase() 0 6 1
1
<?php
2
/**
3
 * Cli.php
4
 *
5
 * PHP version 7.3 and up.
6
 *
7
 * @category Core
8
 * @package  Redbox_Cli
9
 * @author   Johnny Mast <[email protected]>
10
 * @license  https://opensource.org/licenses/MIT MIT
11
 * @link     https://github.com/johnnymast/redbox-cli
12
 * @since    1.0
13
 */
14
15
namespace Redbox\Cli\Object;
16
17
/**
18
 * The base class for the Argument class.
19
 *
20
 * @category Core
21
 * @package  Redbox_Cli
22
 * @author   Johnny Mast <[email protected]>
23
 * @license  https://opensource.org/licenses/MIT MIT
24
 * @link     https://github.com/johnnymast/redbox-cli
25
 * @since    1.0
26
 */
27
class ArgumentObject
28
{
29
30
    /**
31
     * The parsed data for a argument.
32
     *
33
     * @var array
34
     */
35
    protected $modelData = [];
36
37
    /**
38
     * Construct this magic object. Give it a array amd
39
     * it will turn into an object. Its a hydration.
40
     *
41
     * Object constructor.
42
     *
43
     * @param array $array The options for the argument.
44
     */
45 29
    public function __construct($array = array())
46
    {
47 29
        $this->mapTypes($array);
48 29
    }
49
50
    /**
51
     * Convert a string to camelCase
52
     *
53
     * @param string $value The value to turn into camelCase.
54
     *
55
     * @return string
56
     */
57 1
    public static function camelCase(string $value): string
58
    {
59 1
        $value = ucwords(str_replace(array('-', '_'), ' ', $value));
60 1
        $value = str_replace(' ', '', $value);
61 1
        $value[0] = strtolower($value[0]);
62 1
        return $value;
63
    }
64
65
    /**
66
     * Initialize this object's properties from an array.
67
     *
68
     * @param array $array Used to seed this object's properties.
69
     *
70
     * @return void
71
     */
72 29
    protected function mapTypes(array $array): void
73
    {
74 29
        foreach ($array as $key => $val) {
75 29
            if (!property_exists($this, $this->keyType($key))
76 29
                && property_exists($this, $key)
77
            ) {
78 29
                $this->$key = $val;
79 29
                unset($array[$key]);
80 1
            } elseif (property_exists($this, $camelKey = self::camelCase($key))) {
81
                $this->$camelKey = $val;
82
            }
83
        }
84 29
        $this->modelData = $array;
85 29
    }
86
87
    /**
88
     * Return the keyType for a given key.
89
     *
90
     * @param string $key Return the type of the value.
91
     *
92
     * @return string
93
     */
94 29
    protected function keyType(string $key): string
95
    {
96 29
        return $key . "Type";
97
    }
98
99
    /**
100
     * Check to see if a given key is set or not.
101
     *
102
     * @param string $key The key to check.
103
     *
104
     * @return bool
105
     */
106 5
    public function __isset(string $key): bool
107
    {
108 5
        return isset($this->modelData[$key]);
109
    }
110
111
    /**
112
     * Unset a given key.
113
     *
114
     * @param string $key The key value to unset.
115
     *
116
     * @return void
117
     */
118
    public function __unset(string $key): void
119
    {
120
        unset($this->modelData[$key]);
121
    }
122
}
123