|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Platine Console |
|
5
|
|
|
* |
|
6
|
|
|
* Platine Console is a powerful library with support of custom |
|
7
|
|
|
* style to build command line interface applications |
|
8
|
|
|
* |
|
9
|
|
|
* This content is released under the MIT License (MIT) |
|
10
|
|
|
* |
|
11
|
|
|
* Copyright (c) 2020 Platine Console |
|
12
|
|
|
* Copyright (c) 2017-2020 Jitendra Adhikari |
|
13
|
|
|
* |
|
14
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
15
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
16
|
|
|
* in the Software without restriction, including without limitation the rights |
|
17
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
18
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
19
|
|
|
* furnished to do so, subject to the following conditions: |
|
20
|
|
|
* |
|
21
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
22
|
|
|
* copies or substantial portions of the Software. |
|
23
|
|
|
* |
|
24
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
25
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
26
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
27
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
28
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
29
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
30
|
|
|
* SOFTWARE. |
|
31
|
|
|
*/ |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @file Helper.php |
|
35
|
|
|
* |
|
36
|
|
|
* The console helper class |
|
37
|
|
|
* |
|
38
|
|
|
* @package Platine\Console\Util |
|
39
|
|
|
* @author Platine Developers Team |
|
40
|
|
|
* @copyright Copyright (c) 2020 |
|
41
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
42
|
|
|
* @link http://www.iacademy.cf |
|
43
|
|
|
* @version 1.0.0 |
|
44
|
|
|
* @filesource |
|
45
|
|
|
*/ |
|
46
|
|
|
|
|
47
|
|
|
declare(strict_types=1); |
|
48
|
|
|
|
|
49
|
|
|
namespace Platine\Console\Util; |
|
50
|
|
|
|
|
51
|
|
|
use Platine\Console\Input\Option; |
|
52
|
|
|
use Platine\Console\Input\Parameter; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Class Helper |
|
56
|
|
|
* @package Platine\Console\Util |
|
57
|
|
|
*/ |
|
58
|
|
|
class Helper |
|
59
|
|
|
{ |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Convert the given string to camel case |
|
63
|
|
|
* @param string $str |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function toCamelCase(string $str): string |
|
67
|
|
|
{ |
|
68
|
|
|
$words = str_replace(['-', '_'], ' ', $str); |
|
69
|
|
|
$wordsToUpper = str_replace(' ', '', ucwords($words)); |
|
70
|
|
|
|
|
71
|
|
|
return lcfirst($wordsToUpper); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Convert the given string to capitalized words |
|
76
|
|
|
* @param string $str |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function toWords(string $str): string |
|
80
|
|
|
{ |
|
81
|
|
|
$words = trim(str_replace(['-', '_'], ' ', $str)); |
|
82
|
|
|
|
|
83
|
|
|
return ucwords($words); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Normalize arguments like splitting "-abc" and "--xyz=...". |
|
88
|
|
|
* @param array<int, string> $args |
|
89
|
|
|
* @return array<string> |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function normalizeArguments(array $args): array |
|
92
|
|
|
{ |
|
93
|
|
|
$normalized = []; |
|
94
|
|
|
|
|
95
|
|
|
foreach ($args as $arg) { |
|
96
|
|
|
if (preg_match('/^\-\w=/', $arg)) { |
|
97
|
|
|
$parts = explode('=', $arg); |
|
98
|
|
|
if ($parts !== false) { |
|
99
|
|
|
$normalized = array_merge($normalized, $parts); |
|
100
|
|
|
} |
|
101
|
|
|
} elseif (preg_match('/^\-\w{2,}/', $arg)) { |
|
102
|
|
|
$splitArgs = implode(' -', str_split(ltrim($arg, '-'))); |
|
|
|
|
|
|
103
|
|
|
$parts = explode(' ', '-' . $splitArgs); |
|
104
|
|
|
if ($parts !== false) { |
|
105
|
|
|
$normalized = array_merge($normalized, $parts); |
|
106
|
|
|
} |
|
107
|
|
|
} elseif (preg_match('/^\-\-([^\s\=]+)\=/', $arg)) { |
|
108
|
|
|
$parts = explode('=', $arg); |
|
109
|
|
|
if ($parts !== false) { |
|
110
|
|
|
$normalized = array_merge($normalized, $parts); |
|
111
|
|
|
} |
|
112
|
|
|
} else { |
|
113
|
|
|
$normalized[] = $arg; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $normalized; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Normalizes value as per context and runs though filter if possible. |
|
122
|
|
|
* @param Parameter $parameter |
|
123
|
|
|
* @param string|null $value |
|
124
|
|
|
* @return mixed |
|
125
|
|
|
*/ |
|
126
|
|
|
public static function normalizeValue(Parameter $parameter, ?string $value = null) |
|
127
|
|
|
{ |
|
128
|
|
|
if ($parameter instanceof Option && $parameter->isBool()) { |
|
129
|
|
|
return !$parameter->getDefault(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if ($parameter->isVariadic()) { |
|
133
|
|
|
return (array) $value; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if ($value === null) { |
|
137
|
|
|
return $parameter->isRequired() ? null : true; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $parameter->filter($value); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Check if the array is associative. |
|
145
|
|
|
* @param array<int|string, mixed> $values |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
|
|
public static function isAssocArray(array $values): bool |
|
149
|
|
|
{ |
|
150
|
|
|
return !empty($values) |
|
151
|
|
|
&& array_keys($values) !== range(0, count($values) - 1); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|