platine-php /
console
| 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 https://www.platine-php.com |
||
| 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 | * Convert the given string to camel case |
||
| 62 | * @param string $str |
||
| 63 | * @return string |
||
| 64 | */ |
||
| 65 | public static function toCamelCase(string $str): string |
||
| 66 | { |
||
| 67 | $words = str_replace(['-', '_'], ' ', $str); |
||
| 68 | $wordsToUpper = str_replace(' ', '', ucwords($words)); |
||
| 69 | |||
| 70 | return lcfirst($wordsToUpper); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Convert the given string to capitalized words |
||
| 75 | * @param string $str |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | public static function toWords(string $str): string |
||
| 79 | { |
||
| 80 | $words = trim(str_replace(['-', '_'], ' ', $str)); |
||
| 81 | |||
| 82 | return ucwords($words); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Normalize arguments like splitting "-abc" and "--xyz=...". |
||
| 87 | * @param array<int, string> $args |
||
| 88 | * @return array<string> |
||
| 89 | */ |
||
| 90 | public static function normalizeArguments(array $args): array |
||
| 91 | { |
||
| 92 | $normalized = []; |
||
| 93 | |||
| 94 | foreach ($args as $arg) { |
||
| 95 | if (preg_match('/^\-\w=/', $arg)) { |
||
| 96 | $parts = explode('=', $arg); |
||
| 97 | if ($parts !== false) { |
||
| 98 | $normalized = array_merge($normalized, $parts); |
||
| 99 | } |
||
| 100 | } elseif (preg_match('/^\-\w{2,}/', $arg)) { |
||
| 101 | $splitArgs = implode(' -', str_split(ltrim($arg, '-'))); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 102 | $parts = explode(' ', '-' . $splitArgs); |
||
| 103 | if ($parts !== false) { |
||
| 104 | $normalized = array_merge($normalized, $parts); |
||
| 105 | } |
||
| 106 | } elseif (preg_match('/^\-\-([^\s\=]+)\=/', $arg)) { |
||
| 107 | $parts = explode('=', $arg); |
||
| 108 | if ($parts !== false) { |
||
| 109 | $normalized = array_merge($normalized, $parts); |
||
| 110 | } |
||
| 111 | } else { |
||
| 112 | $normalized[] = $arg; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | return $normalized; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Normalizes value as per context and runs though filter if possible. |
||
| 121 | * @param Parameter $parameter |
||
| 122 | * @param string|null $value |
||
| 123 | * @return mixed |
||
| 124 | */ |
||
| 125 | public static function normalizeValue(Parameter $parameter, ?string $value = null): mixed |
||
| 126 | { |
||
| 127 | if ($parameter instanceof Option && $parameter->isBool()) { |
||
| 128 | return !$parameter->getDefault(); |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($parameter->isVariadic()) { |
||
| 132 | return (array) $value; |
||
| 133 | } |
||
| 134 | |||
| 135 | if ($value === null) { |
||
| 136 | return $parameter->isRequired() ? null : true; |
||
| 137 | } |
||
| 138 | |||
| 139 | return $parameter->filter($value); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Check if the array is associative. |
||
| 144 | * @param array<int|string, mixed> $values |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public static function isAssocArray(array $values): bool |
||
| 148 | { |
||
| 149 | return !empty($values) |
||
| 150 | && array_keys($values) !== range(0, count($values) - 1); |
||
| 151 | } |
||
| 152 | } |
||
| 153 |