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
|
|
|
|