@@ -1,27 +1,27 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * CommandParameter.php |
|
| 5 | - */ |
|
| 4 | + * CommandParameter.php |
|
| 5 | + */ |
|
| 6 | 6 | |
| 7 | 7 | namespace netfocusinc\argh; |
| 8 | 8 | |
| 9 | 9 | use netfocusinc\argh\Parameter; |
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | - * A Command Parameter. |
|
| 13 | - * |
|
| 14 | - * Subtype of Parameter that represents a command string |
|
| 15 | - * Command parameters must define an array of 'options'. |
|
| 16 | - * When interpreting command line arguments, commands will only be matched with command line input |
|
| 17 | - * when the command line argument matches one of the pre-defined 'options'. |
|
| 18 | - * |
|
| 19 | - * @author Benjamin Hough |
|
| 20 | - * |
|
| 21 | - * @api |
|
| 22 | - * |
|
| 23 | - * @since 1.0.0 |
|
| 24 | - */ |
|
| 12 | + * A Command Parameter. |
|
| 13 | + * |
|
| 14 | + * Subtype of Parameter that represents a command string |
|
| 15 | + * Command parameters must define an array of 'options'. |
|
| 16 | + * When interpreting command line arguments, commands will only be matched with command line input |
|
| 17 | + * when the command line argument matches one of the pre-defined 'options'. |
|
| 18 | + * |
|
| 19 | + * @author Benjamin Hough |
|
| 20 | + * |
|
| 21 | + * @api |
|
| 22 | + * |
|
| 23 | + * @since 1.0.0 |
|
| 24 | + */ |
|
| 25 | 25 | class CommandParameter extends Parameter |
| 26 | 26 | { |
| 27 | 27 | |
@@ -35,19 +35,19 @@ discard block |
||
| 35 | 35 | // |
| 36 | 36 | |
| 37 | 37 | /** |
| 38 | - * Construct a new CommandParameter. |
|
| 39 | - * |
|
| 40 | - * Overrides Parameter constructor to enforce required 'options' |
|
| 41 | - * |
|
| 42 | - * @since 1.0.0 |
|
| 43 | - * |
|
| 44 | - * @param string $name |
|
| 45 | - * @param string $flag |
|
| 46 | - * @param bool $required |
|
| 47 | - * @param string $default The default 'option' to use for this command, when none is specified on the command line |
|
| 48 | - * @param string $description |
|
| 49 | - * @param array $options |
|
| 50 | - */ |
|
| 38 | + * Construct a new CommandParameter. |
|
| 39 | + * |
|
| 40 | + * Overrides Parameter constructor to enforce required 'options' |
|
| 41 | + * |
|
| 42 | + * @since 1.0.0 |
|
| 43 | + * |
|
| 44 | + * @param string $name |
|
| 45 | + * @param string $flag |
|
| 46 | + * @param bool $required |
|
| 47 | + * @param string $default The default 'option' to use for this command, when none is specified on the command line |
|
| 48 | + * @param string $description |
|
| 49 | + * @param array $options |
|
| 50 | + */ |
|
| 51 | 51 | |
| 52 | 52 | public function __construct(string $name, string $flag=null, bool $required=FALSE, $default=null, string $description=null, array $options=array()) |
| 53 | 53 | { |
@@ -63,28 +63,28 @@ discard block |
||
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | /** |
| 66 | - * Returns ARGH_TYPE_COMMAND |
|
| 67 | - * |
|
| 68 | - * @since 1.0.0 |
|
| 69 | - * |
|
| 70 | - * @return int |
|
| 71 | - */ |
|
| 66 | + * Returns ARGH_TYPE_COMMAND |
|
| 67 | + * |
|
| 68 | + * @since 1.0.0 |
|
| 69 | + * |
|
| 70 | + * @return int |
|
| 71 | + */ |
|
| 72 | 72 | public function getParameterType(): int |
| 73 | 73 | { |
| 74 | 74 | return Parameter::ARGH_TYPE_COMMAND; |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | /** |
| 78 | - * Sets the string value of this Parameter. |
|
| 79 | - * |
|
| 80 | - * Checks that the given 'value' matches one of this Commands 'options' before settings its value. |
|
| 81 | - * |
|
| 82 | - * @since 1.0.0 |
|
| 83 | - * |
|
| 84 | - * @param string $value |
|
| 85 | - * |
|
| 86 | - * @throws ArghException When $value is not one of this CommandsParameter's 'options' |
|
| 87 | - */ |
|
| 78 | + * Sets the string value of this Parameter. |
|
| 79 | + * |
|
| 80 | + * Checks that the given 'value' matches one of this Commands 'options' before settings its value. |
|
| 81 | + * |
|
| 82 | + * @since 1.0.0 |
|
| 83 | + * |
|
| 84 | + * @param string $value |
|
| 85 | + * |
|
| 86 | + * @throws ArghException When $value is not one of this CommandsParameter's 'options' |
|
| 87 | + */ |
|
| 88 | 88 | public function setValue($value) |
| 89 | 89 | { |
| 90 | 90 | if(is_array($value)) |
@@ -49,11 +49,11 @@ discard block |
||
| 49 | 49 | * @param array $options |
| 50 | 50 | */ |
| 51 | 51 | |
| 52 | - public function __construct(string $name, string $flag=null, bool $required=FALSE, $default=null, string $description=null, array $options=array()) |
|
| 52 | + public function __construct(string $name, string $flag = null, bool $required = FALSE, $default = null, string $description = null, array $options = array()) |
|
| 53 | 53 | { |
| 54 | 54 | |
| 55 | 55 | // Required a non-empty 'options' |
| 56 | - if( count($options) < 1 ) |
|
| 56 | + if (count($options)<1) |
|
| 57 | 57 | { |
| 58 | 58 | throw(new ArghException('CommandParameter must have options')); |
| 59 | 59 | } |
@@ -87,12 +87,12 @@ discard block |
||
| 87 | 87 | */ |
| 88 | 88 | public function setValue($value) |
| 89 | 89 | { |
| 90 | - if(is_array($value)) |
|
| 90 | + if (is_array($value)) |
|
| 91 | 91 | { |
| 92 | 92 | throw(new ArghException('Command value cannot be set to an array')); |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | - if( ($this->hasOptions()) && (!$this->isOption($value)) ) |
|
| 95 | + if (($this->hasOptions()) && (!$this->isOption($value))) |
|
| 96 | 96 | { |
| 97 | 97 | throw(new ArghException('Not a valid option for \'' . $this->getName() . '\'')); |
| 98 | 98 | } |
@@ -1,8 +1,8 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * StringParameter.php |
|
| 5 | - */ |
|
| 4 | + * StringParameter.php |
|
| 5 | + */ |
|
| 6 | 6 | |
| 7 | 7 | namespace netfocusinc\argh; |
| 8 | 8 | |
@@ -11,16 +11,16 @@ discard block |
||
| 11 | 11 | |
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | - * String parameter |
|
| 15 | - * |
|
| 16 | - * Subtype of Parameter that represents a string value. |
|
| 17 | - * |
|
| 18 | - * @api |
|
| 19 | - * |
|
| 20 | - * @author Benjamin Hough |
|
| 21 | - * |
|
| 22 | - * @since 1.0.0 |
|
| 23 | - */ |
|
| 14 | + * String parameter |
|
| 15 | + * |
|
| 16 | + * Subtype of Parameter that represents a string value. |
|
| 17 | + * |
|
| 18 | + * @api |
|
| 19 | + * |
|
| 20 | + * @author Benjamin Hough |
|
| 21 | + * |
|
| 22 | + * @since 1.0.0 |
|
| 23 | + */ |
|
| 24 | 24 | class StringParameter extends Parameter |
| 25 | 25 | { |
| 26 | 26 | |
@@ -33,28 +33,28 @@ discard block |
||
| 33 | 33 | // |
| 34 | 34 | |
| 35 | 35 | /** |
| 36 | - * Returns ARGH_TYPE_STRING |
|
| 37 | - * |
|
| 38 | - * @since 1.0.0 |
|
| 39 | - * |
|
| 40 | - * @return int |
|
| 41 | - */ |
|
| 36 | + * Returns ARGH_TYPE_STRING |
|
| 37 | + * |
|
| 38 | + * @since 1.0.0 |
|
| 39 | + * |
|
| 40 | + * @return int |
|
| 41 | + */ |
|
| 42 | 42 | public function getParameterType(): int |
| 43 | 43 | { |
| 44 | 44 | return Parameter::ARGH_TYPE_STRING; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | /** |
| 48 | - * Sets the string value of this Parameter. |
|
| 49 | - * |
|
| 50 | - * Casts all values to string. |
|
| 51 | - * |
|
| 52 | - * @since 1.0.0 |
|
| 53 | - * |
|
| 54 | - * @param mixed $value |
|
| 55 | - * |
|
| 56 | - * @throws ArghExpception If $value is an array. |
|
| 57 | - */ |
|
| 48 | + * Sets the string value of this Parameter. |
|
| 49 | + * |
|
| 50 | + * Casts all values to string. |
|
| 51 | + * |
|
| 52 | + * @since 1.0.0 |
|
| 53 | + * |
|
| 54 | + * @param mixed $value |
|
| 55 | + * |
|
| 56 | + * @throws ArghExpception If $value is an array. |
|
| 57 | + */ |
|
| 58 | 58 | public function setValue($value) |
| 59 | 59 | { |
| 60 | 60 | if(is_array($value)) |
@@ -57,7 +57,7 @@ |
||
| 57 | 57 | */ |
| 58 | 58 | public function setValue($value) |
| 59 | 59 | { |
| 60 | - if(is_array($value)) |
|
| 60 | + if (is_array($value)) |
|
| 61 | 61 | { |
| 62 | 62 | throw(new ArghException('StringParameter values cannot be set to an array')); |
| 63 | 63 | } |
@@ -1,22 +1,22 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * ArgumentParser.php |
|
| 5 | - */ |
|
| 4 | + * ArgumentParser.php |
|
| 5 | + */ |
|
| 6 | 6 | |
| 7 | 7 | namespace netfocusinc\argh; |
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | - * Internal class that performs the work of parsing command line arguments. |
|
| 11 | - * |
|
| 12 | - * Uses the provided Language and ParameterCollection to interpret an array of command line arguments. |
|
| 13 | - * |
|
| 14 | - * @author Benjamin Hough |
|
| 15 | - * |
|
| 16 | - * @internal |
|
| 17 | - * |
|
| 18 | - * @since 1.0.0 |
|
| 19 | - */ |
|
| 10 | + * Internal class that performs the work of parsing command line arguments. |
|
| 11 | + * |
|
| 12 | + * Uses the provided Language and ParameterCollection to interpret an array of command line arguments. |
|
| 13 | + * |
|
| 14 | + * @author Benjamin Hough |
|
| 15 | + * |
|
| 16 | + * @internal |
|
| 17 | + * |
|
| 18 | + * @since 1.0.0 |
|
| 19 | + */ |
|
| 20 | 20 | class ArgumentParser |
| 21 | 21 | { |
| 22 | 22 | |
@@ -35,13 +35,13 @@ discard block |
||
| 35 | 35 | // |
| 36 | 36 | |
| 37 | 37 | /** |
| 38 | - * Constructs a new ArgumentParser |
|
| 39 | - * |
|
| 40 | - * Creates a new ArgumentParser instance with the specified Langugage and ParameterCollection |
|
| 41 | - * The resulting instance is ready for parsing an array of arguments. |
|
| 42 | - * |
|
| 43 | - * @since 1.0.0 |
|
| 44 | - */ |
|
| 38 | + * Constructs a new ArgumentParser |
|
| 39 | + * |
|
| 40 | + * Creates a new ArgumentParser instance with the specified Langugage and ParameterCollection |
|
| 41 | + * The resulting instance is ready for parsing an array of arguments. |
|
| 42 | + * |
|
| 43 | + * @since 1.0.0 |
|
| 44 | + */ |
|
| 45 | 45 | public function __construct(Language $language, ParameterCollection $parameterCollection) |
| 46 | 46 | { |
| 47 | 47 | // Init properties on this instance |
@@ -51,18 +51,18 @@ discard block |
||
| 51 | 51 | |
| 52 | 52 | |
| 53 | 53 | /** |
| 54 | - * Parse an array of command line arguments. |
|
| 55 | - * |
|
| 56 | - * Interprets an array of command line arguments using the Language and ParameterCollection |
|
| 57 | - * that was configured during construction. |
|
| 58 | - * When successful, this results in an array of Arguments (with key,value pairs). |
|
| 59 | - * |
|
| 60 | - * @param array $args A pre-processed $argv array |
|
| 61 | - * |
|
| 62 | - * @return Argument[] |
|
| 63 | - * |
|
| 64 | - * @throws ArgumentException |
|
| 65 | - */ |
|
| 54 | + * Parse an array of command line arguments. |
|
| 55 | + * |
|
| 56 | + * Interprets an array of command line arguments using the Language and ParameterCollection |
|
| 57 | + * that was configured during construction. |
|
| 58 | + * When successful, this results in an array of Arguments (with key,value pairs). |
|
| 59 | + * |
|
| 60 | + * @param array $args A pre-processed $argv array |
|
| 61 | + * |
|
| 62 | + * @return Argument[] |
|
| 63 | + * |
|
| 64 | + * @throws ArgumentException |
|
| 65 | + */ |
|
| 66 | 66 | public function parse(array $args): array |
| 67 | 67 | { |
| 68 | 68 | // Init an array of Arguments |
@@ -221,21 +221,21 @@ discard block |
||
| 221 | 221 | } // END: public static function parse() |
| 222 | 222 | |
| 223 | 223 | /** |
| 224 | - * Attempts to create Arguments given a Rule and matching tokens (from the command line arguments string) |
|
| 225 | - * |
|
| 226 | - * When a set of matching tokens (from a command line argument string) is matched with a Rule (by the parse method) |
|
| 227 | - * This function is used to determine if the matching tokens can yield an Argument from the matched Rule. |
|
| 228 | - * This requires checking that the SEMANTICS of the Rule correspond with the matched tokens |
|
| 229 | - * For example, if -xvf matches the Rule (hypenated multi flag), each character (of xvf) must also correspond to the flag of a |
|
| 230 | - * Parameter that was defined by the client. When no Argument is yielded, the parser can then attempt to match the tokens with another Rule. |
|
| 231 | - * |
|
| 232 | - * @internal |
|
| 233 | - * |
|
| 234 | - * @param $rule Rule |
|
| 235 | - * @param $tokens string[] |
|
| 236 | - * |
|
| 237 | - * @return Argument[] An array of Arguments |
|
| 238 | - */ |
|
| 224 | + * Attempts to create Arguments given a Rule and matching tokens (from the command line arguments string) |
|
| 225 | + * |
|
| 226 | + * When a set of matching tokens (from a command line argument string) is matched with a Rule (by the parse method) |
|
| 227 | + * This function is used to determine if the matching tokens can yield an Argument from the matched Rule. |
|
| 228 | + * This requires checking that the SEMANTICS of the Rule correspond with the matched tokens |
|
| 229 | + * For example, if -xvf matches the Rule (hypenated multi flag), each character (of xvf) must also correspond to the flag of a |
|
| 230 | + * Parameter that was defined by the client. When no Argument is yielded, the parser can then attempt to match the tokens with another Rule. |
|
| 231 | + * |
|
| 232 | + * @internal |
|
| 233 | + * |
|
| 234 | + * @param $rule Rule |
|
| 235 | + * @param $tokens string[] |
|
| 236 | + * |
|
| 237 | + * @return Argument[] An array of Arguments |
|
| 238 | + */ |
|
| 239 | 239 | public function yieldArgumentsFromRule(Rule $rule, array $tokens): array |
| 240 | 240 | { |
| 241 | 241 | |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | // Init an array of Arguments |
| 69 | 69 | $arguments = array(); |
| 70 | 70 | |
| 71 | - if(count($args) == 0) |
|
| 71 | + if (count($args) == 0) |
|
| 72 | 72 | { |
| 73 | 73 | // Nothing to parse |
| 74 | 74 | return $arguments; |
@@ -80,12 +80,12 @@ discard block |
||
| 80 | 80 | // Get all Parameters from ParameterCollection |
| 81 | 81 | $params = $this->parameterCollection->all(); |
| 82 | 82 | |
| 83 | - if( count($rules) == 0 ) |
|
| 83 | + if (count($rules) == 0) |
|
| 84 | 84 | { |
| 85 | 85 | throw new ArghException(__CLASS__ . ': Language needs at least one rule to parse arguments.'); |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | - if( count($params) == 0 ) |
|
| 88 | + if (count($params) == 0) |
|
| 89 | 89 | { |
| 90 | 90 | throw new ArghException(__CLASS__ . ': ParameterCollection needs at least one parameter to parse arguments.'); |
| 91 | 91 | } |
@@ -144,13 +144,13 @@ discard block |
||
| 144 | 144 | // END DEBUG |
| 145 | 145 | // |
| 146 | 146 | |
| 147 | - foreach($rules as $rule) |
|
| 147 | + foreach ($rules as $rule) |
|
| 148 | 148 | { |
| 149 | 149 | //echo "DEBUG: Checking for match with rule: " . $rule->name() . " (" . $rule->syntax() . ")" . "\n"; |
| 150 | 150 | |
| 151 | 151 | $tokens = array(); // init array to capture matching tokens from Rule->match() |
| 152 | 152 | |
| 153 | - if( $rule->match($argsS, $tokens) ) |
|
| 153 | + if ($rule->match($argsS, $tokens)) |
|
| 154 | 154 | { |
| 155 | 155 | // Count the number of arguments that were matched |
| 156 | 156 | $count = count($argsL); |
@@ -158,11 +158,11 @@ discard block |
||
| 158 | 158 | //echo "* MATCHED $count \$argv elements *\n"; |
| 159 | 159 | |
| 160 | 160 | // Empty $argsL; prevent this inner foreach loop from continuing |
| 161 | - for($i=0; $i<$count; $i++) array_shift($argsL); |
|
| 161 | + for ($i = 0; $i<$count; $i++) array_shift($argsL); |
|
| 162 | 162 | |
| 163 | 163 | // Remove (shift) matching elements from $args |
| 164 | 164 | // These arguments have been consumed by the parser and are no longer needed |
| 165 | - for($i=0; $i<$count; $i++) array_shift($args); |
|
| 165 | + for ($i = 0; $i<$count; $i++) array_shift($args); |
|
| 166 | 166 | |
| 167 | 167 | // |
| 168 | 168 | // Try yielding Arguments from this Rule |
@@ -171,12 +171,12 @@ discard block |
||
| 171 | 171 | |
| 172 | 172 | $yield = $this->yieldArgumentsFromRule($rule, $tokens); |
| 173 | 173 | |
| 174 | - if( count($yield) > 0 ) |
|
| 174 | + if (count($yield)>0) |
|
| 175 | 175 | { |
| 176 | 176 | //? TODO: Validate Arguments before adding them to the Arguments array? |
| 177 | 177 | |
| 178 | 178 | // Add the new Arguments yielded from this Rule |
| 179 | - foreach($yield as $y) $arguments[] = $y; |
|
| 179 | + foreach ($yield as $y) $arguments[] = $y; |
|
| 180 | 180 | |
| 181 | 181 | // !IMPORTANT! Stop checking Rules |
| 182 | 182 | break; |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | |
| 192 | 192 | } // END: foreach($rules as $rule) |
| 193 | 193 | |
| 194 | - if( count($tokens) == 0 ) |
|
| 194 | + if (count($tokens) == 0) |
|
| 195 | 195 | { |
| 196 | 196 | // $argsS did NOT match any rules |
| 197 | 197 | |
@@ -201,7 +201,7 @@ discard block |
||
| 201 | 201 | // Prepend popped elemented to beginning of $argsR |
| 202 | 202 | array_unshift($argsR, $arg); |
| 203 | 203 | |
| 204 | - if( count($argsL) == 0 ) |
|
| 204 | + if (count($argsL) == 0) |
|
| 205 | 205 | { |
| 206 | 206 | // There was no match, and there are no arguments left to pop from $argsL |
| 207 | 207 | throw new ArghException(__METHOD__ . ': Syntax Error: ' . $argsS); |
@@ -210,10 +210,10 @@ discard block |
||
| 210 | 210 | } // END: if( count($tokens) == 0 ) |
| 211 | 211 | |
| 212 | 212 | } // END do |
| 213 | - while( count($argsL) > 0 ); |
|
| 213 | + while (count($argsL)>0); |
|
| 214 | 214 | |
| 215 | 215 | } // END: do |
| 216 | - while( count($args) > 0 ); |
|
| 216 | + while (count($args)>0); |
|
| 217 | 217 | |
| 218 | 218 | // Return Arguments array |
| 219 | 219 | return $arguments; |
@@ -246,21 +246,21 @@ discard block |
||
| 246 | 246 | $argument = array(); |
| 247 | 247 | |
| 248 | 248 | // Loop through $tokens and define Argument(s) based on the current rules semantics |
| 249 | - for($i=1; $i<count($tokens); $i++) |
|
| 249 | + for ($i = 1; $i<count($tokens); $i++) |
|
| 250 | 250 | { |
| 251 | 251 | $token = $tokens[$i]; |
| 252 | 252 | $semantics = $rule->semantics()[$i-1]; |
| 253 | 253 | |
| 254 | 254 | //echo __METHOD__ . ": token: $token (" . Rule::semanticsToString($semantics) . ")\n"; |
| 255 | 255 | |
| 256 | - switch( $semantics ) |
|
| 256 | + switch ($semantics) |
|
| 257 | 257 | { |
| 258 | 258 | case ARGH_SEMANTICS_FLAG: |
| 259 | 259 | |
| 260 | - if( $this->parameterCollection->exists($token) ) |
|
| 260 | + if ($this->parameterCollection->exists($token)) |
|
| 261 | 261 | { |
| 262 | 262 | // This Rule will create a single Argument |
| 263 | - if(count($argument)==0) $argument[0] = new Argument($token); |
|
| 263 | + if (count($argument) == 0) $argument[0] = new Argument($token); |
|
| 264 | 264 | } |
| 265 | 265 | else |
| 266 | 266 | { |
@@ -274,15 +274,15 @@ discard block |
||
| 274 | 274 | case ARGH_SEMANTICS_FLAGS: |
| 275 | 275 | |
| 276 | 276 | // Check every character of this $token for a matching parameter 'flag' |
| 277 | - for($j=0; $j<strlen($token); $j++) |
|
| 277 | + for ($j = 0; $j<strlen($token); $j++) |
|
| 278 | 278 | { |
| 279 | - if( $this->parameterCollection->exists( $token{$j} ) ) |
|
| 279 | + if ($this->parameterCollection->exists($token{$j} )) |
|
| 280 | 280 | { |
| 281 | 281 | // This Rule can only apply to ARGH_TYPE_BOOLEAN Parameters |
| 282 | - if( ARGH_TYPE_BOOLEAN == $this->parameterCollection->get($token{$j})->getParameterType() ) |
|
| 282 | + if (ARGH_TYPE_BOOLEAN == $this->parameterCollection->get($token{$j})->getParameterType()) |
|
| 283 | 283 | { |
| 284 | 284 | // Create new Argument for each flag |
| 285 | - if( !array_key_exists($j, $argument) ) $argument[$j] = new Argument($token{$j}); |
|
| 285 | + if (!array_key_exists($j, $argument)) $argument[$j] = new Argument($token{$j}); |
|
| 286 | 286 | } |
| 287 | 287 | } |
| 288 | 288 | else |
@@ -301,10 +301,10 @@ discard block |
||
| 301 | 301 | |
| 302 | 302 | case ARGH_SEMANTICS_NAME: |
| 303 | 303 | |
| 304 | - if( $this->parameterCollection->exists($token) ) |
|
| 304 | + if ($this->parameterCollection->exists($token)) |
|
| 305 | 305 | { |
| 306 | 306 | // This Rule will create a single Argument |
| 307 | - if(count($argument)==0) $argument[0] = new Argument($token); |
|
| 307 | + if (count($argument) == 0) $argument[0] = new Argument($token); |
|
| 308 | 308 | } |
| 309 | 309 | else |
| 310 | 310 | { |
@@ -320,7 +320,7 @@ discard block |
||
| 320 | 320 | // Usually, the Argument will have already been created by another token in this Rule |
| 321 | 321 | |
| 322 | 322 | // If no new Argument created by this Rule yet, create one now |
| 323 | - if(count($argument)==0) $argument[0] = new Argument(); |
|
| 323 | + if (count($argument) == 0) $argument[0] = new Argument(); |
|
| 324 | 324 | |
| 325 | 325 | // The new Argument's 'key' should be set by another token in this Rule |
| 326 | 326 | $argument[0]->setValue($token); |
@@ -332,7 +332,7 @@ discard block |
||
| 332 | 332 | // Usually, the Argument will have already been created by another token in this Rule |
| 333 | 333 | |
| 334 | 334 | // If no new Argument created by this Rule yet, create one now |
| 335 | - if(count($argument)==0) $argument[0] = new Argument(); |
|
| 335 | + if (count($argument) == 0) $argument[0] = new Argument(); |
|
| 336 | 336 | |
| 337 | 337 | // Trim brackets from the $token (list) |
| 338 | 338 | $token = trim($token, "[]"); |
@@ -342,28 +342,28 @@ discard block |
||
| 342 | 342 | |
| 343 | 343 | // Use the $elements array as the 'value' for all new Argument created by this Rule |
| 344 | 344 | // Usually, this will only apply to a single Argument, unless this Rule contains ARGH_SEMANTICS_FLAGS |
| 345 | - foreach($argument as &$a) $a->setValue($elements); |
|
| 345 | + foreach ($argument as &$a) $a->setValue($elements); |
|
| 346 | 346 | |
| 347 | 347 | break; |
| 348 | 348 | |
| 349 | 349 | case ARGH_SEMANTICS_COMMAND: |
| 350 | 350 | |
| 351 | 351 | // Check if ParameterCollection contains any commands |
| 352 | - if($this->parameterCollection->hasCommand()) |
|
| 352 | + if ($this->parameterCollection->hasCommand()) |
|
| 353 | 353 | { |
| 354 | 354 | // Retrieve all ARGH_TYPE_COMMAND Parameters |
| 355 | 355 | $commands = $this->parameterCollection->getCommands(); |
| 356 | 356 | |
| 357 | - foreach($commands as $p) |
|
| 357 | + foreach ($commands as $p) |
|
| 358 | 358 | { |
| 359 | - if($p->hasOptions()) |
|
| 359 | + if ($p->hasOptions()) |
|
| 360 | 360 | { |
| 361 | - if( in_array($token, $p->getOptions()) ) |
|
| 361 | + if (in_array($token, $p->getOptions())) |
|
| 362 | 362 | { |
| 363 | 363 | // $token matches an option of this ARGH_TYPE_COMMAND Parameter |
| 364 | 364 | |
| 365 | 365 | // If no new Argument created by this Rule yet, create one now |
| 366 | - if(count($argument)==0) $argument[0] = new Argument($p->getName(), $token); |
|
| 366 | + if (count($argument) == 0) $argument[0] = new Argument($p->getName(), $token); |
|
| 367 | 367 | |
| 368 | 368 | // Stop searching this Parameters options |
| 369 | 369 | break; |
@@ -158,11 +158,15 @@ discard block |
||
| 158 | 158 | //echo "* MATCHED $count \$argv elements *\n"; |
| 159 | 159 | |
| 160 | 160 | // Empty $argsL; prevent this inner foreach loop from continuing |
| 161 | - for($i=0; $i<$count; $i++) array_shift($argsL); |
|
| 161 | + for($i=0; $i<$count; $i++) { |
|
| 162 | + array_shift($argsL); |
|
| 163 | + } |
|
| 162 | 164 | |
| 163 | 165 | // Remove (shift) matching elements from $args |
| 164 | 166 | // These arguments have been consumed by the parser and are no longer needed |
| 165 | - for($i=0; $i<$count; $i++) array_shift($args); |
|
| 167 | + for($i=0; $i<$count; $i++) { |
|
| 168 | + array_shift($args); |
|
| 169 | + } |
|
| 166 | 170 | |
| 167 | 171 | // |
| 168 | 172 | // Try yielding Arguments from this Rule |
@@ -176,7 +180,9 @@ discard block |
||
| 176 | 180 | //? TODO: Validate Arguments before adding them to the Arguments array? |
| 177 | 181 | |
| 178 | 182 | // Add the new Arguments yielded from this Rule |
| 179 | - foreach($yield as $y) $arguments[] = $y; |
|
| 183 | + foreach($yield as $y) { |
|
| 184 | + $arguments[] = $y; |
|
| 185 | + } |
|
| 180 | 186 | |
| 181 | 187 | // !IMPORTANT! Stop checking Rules |
| 182 | 188 | break; |
@@ -260,9 +266,10 @@ discard block |
||
| 260 | 266 | if( $this->parameterCollection->exists($token) ) |
| 261 | 267 | { |
| 262 | 268 | // This Rule will create a single Argument |
| 263 | - if(count($argument)==0) $argument[0] = new Argument($token); |
|
| 264 | - } |
|
| 265 | - else |
|
| 269 | + if(count($argument)==0) { |
|
| 270 | + $argument[0] = new Argument($token); |
|
| 271 | + } |
|
| 272 | + } else |
|
| 266 | 273 | { |
| 267 | 274 | // This token does NOT match the flag of any defined parameter |
| 268 | 275 | // This Rule will NOT yield any arguments |
@@ -282,10 +289,11 @@ discard block |
||
| 282 | 289 | if( ARGH_TYPE_BOOLEAN == $this->parameterCollection->get($token{$j})->getParameterType() ) |
| 283 | 290 | { |
| 284 | 291 | // Create new Argument for each flag |
| 285 | - if( !array_key_exists($j, $argument) ) $argument[$j] = new Argument($token{$j}); |
|
| 292 | + if( !array_key_exists($j, $argument) ) { |
|
| 293 | + $argument[$j] = new Argument($token{$j}); |
|
| 294 | + } |
|
| 286 | 295 | } |
| 287 | - } |
|
| 288 | - else |
|
| 296 | + } else |
|
| 289 | 297 | { |
| 290 | 298 | // A character in $token, does not match a defined Parameter flag |
| 291 | 299 | // This Rule will NOT yield any arguments |
@@ -304,9 +312,10 @@ discard block |
||
| 304 | 312 | if( $this->parameterCollection->exists($token) ) |
| 305 | 313 | { |
| 306 | 314 | // This Rule will create a single Argument |
| 307 | - if(count($argument)==0) $argument[0] = new Argument($token); |
|
| 308 | - } |
|
| 309 | - else |
|
| 315 | + if(count($argument)==0) { |
|
| 316 | + $argument[0] = new Argument($token); |
|
| 317 | + } |
|
| 318 | + } else |
|
| 310 | 319 | { |
| 311 | 320 | // This token does NOT match the flag of any defined parameter |
| 312 | 321 | // This Rule will NOT yield any arguments |
@@ -320,7 +329,9 @@ discard block |
||
| 320 | 329 | // Usually, the Argument will have already been created by another token in this Rule |
| 321 | 330 | |
| 322 | 331 | // If no new Argument created by this Rule yet, create one now |
| 323 | - if(count($argument)==0) $argument[0] = new Argument(); |
|
| 332 | + if(count($argument)==0) { |
|
| 333 | + $argument[0] = new Argument(); |
|
| 334 | + } |
|
| 324 | 335 | |
| 325 | 336 | // The new Argument's 'key' should be set by another token in this Rule |
| 326 | 337 | $argument[0]->setValue($token); |
@@ -332,7 +343,9 @@ discard block |
||
| 332 | 343 | // Usually, the Argument will have already been created by another token in this Rule |
| 333 | 344 | |
| 334 | 345 | // If no new Argument created by this Rule yet, create one now |
| 335 | - if(count($argument)==0) $argument[0] = new Argument(); |
|
| 346 | + if(count($argument)==0) { |
|
| 347 | + $argument[0] = new Argument(); |
|
| 348 | + } |
|
| 336 | 349 | |
| 337 | 350 | // Trim brackets from the $token (list) |
| 338 | 351 | $token = trim($token, "[]"); |
@@ -342,7 +355,9 @@ discard block |
||
| 342 | 355 | |
| 343 | 356 | // Use the $elements array as the 'value' for all new Argument created by this Rule |
| 344 | 357 | // Usually, this will only apply to a single Argument, unless this Rule contains ARGH_SEMANTICS_FLAGS |
| 345 | - foreach($argument as &$a) $a->setValue($elements); |
|
| 358 | + foreach($argument as &$a) { |
|
| 359 | + $a->setValue($elements); |
|
| 360 | + } |
|
| 346 | 361 | |
| 347 | 362 | break; |
| 348 | 363 | |
@@ -363,7 +378,9 @@ discard block |
||
| 363 | 378 | // $token matches an option of this ARGH_TYPE_COMMAND Parameter |
| 364 | 379 | |
| 365 | 380 | // If no new Argument created by this Rule yet, create one now |
| 366 | - if(count($argument)==0) $argument[0] = new Argument($p->getName(), $token); |
|
| 381 | + if(count($argument)==0) { |
|
| 382 | + $argument[0] = new Argument($p->getName(), $token); |
|
| 383 | + } |
|
| 367 | 384 | |
| 368 | 385 | // Stop searching this Parameters options |
| 369 | 386 | break; |
@@ -1,22 +1,22 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * Rule.php |
|
| 5 | - */ |
|
| 4 | + * Rule.php |
|
| 5 | + */ |
|
| 6 | 6 | |
| 7 | 7 | namespace netfocusinc\argh; |
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | - * Representation of a Rule used to interpret command line arguments |
|
| 11 | - * |
|
| 12 | - * Rules are a combination of syntax and semantics used to interpret command line arguments. |
|
| 13 | - * When a command line string matches the syntax of a rule, and its character content matches the sematics of the rule |
|
| 14 | - * this command line string can be used to create an Argument. |
|
| 15 | - * |
|
| 16 | - * @author Benjamin Hough |
|
| 17 | - * |
|
| 18 | - * @since 1.0.0 |
|
| 19 | - */ |
|
| 10 | + * Representation of a Rule used to interpret command line arguments |
|
| 11 | + * |
|
| 12 | + * Rules are a combination of syntax and semantics used to interpret command line arguments. |
|
| 13 | + * When a command line string matches the syntax of a rule, and its character content matches the sematics of the rule |
|
| 14 | + * this command line string can be used to create an Argument. |
|
| 15 | + * |
|
| 16 | + * @author Benjamin Hough |
|
| 17 | + * |
|
| 18 | + * @since 1.0.0 |
|
| 19 | + */ |
|
| 20 | 20 | class Rule |
| 21 | 21 | { |
| 22 | 22 | // |
@@ -64,14 +64,14 @@ discard block |
||
| 64 | 64 | // |
| 65 | 65 | |
| 66 | 66 | /** |
| 67 | - * Creates a new Rule with the specified $attributes |
|
| 68 | - * |
|
| 69 | - * Convenience method for creating new Rules with the specified $attributes |
|
| 70 | - * |
|
| 71 | - * @since 1.0.0 |
|
| 72 | - * |
|
| 73 | - * @returns Rule |
|
| 74 | - */ |
|
| 67 | + * Creates a new Rule with the specified $attributes |
|
| 68 | + * |
|
| 69 | + * Convenience method for creating new Rules with the specified $attributes |
|
| 70 | + * |
|
| 71 | + * @since 1.0.0 |
|
| 72 | + * |
|
| 73 | + * @returns Rule |
|
| 74 | + */ |
|
| 75 | 75 | public static function createWithAttributes(array $attributes): Rule |
| 76 | 76 | { |
| 77 | 77 | // Defaults |
@@ -90,14 +90,14 @@ discard block |
||
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | /** |
| 93 | - * Returns a (human friendly) string representation of a semantic int |
|
| 94 | - * |
|
| 95 | - * @since 1.0.0 |
|
| 96 | - * |
|
| 97 | - * @param int $semantics |
|
| 98 | - * |
|
| 99 | - * @returns string |
|
| 100 | - */ |
|
| 93 | + * Returns a (human friendly) string representation of a semantic int |
|
| 94 | + * |
|
| 95 | + * @since 1.0.0 |
|
| 96 | + * |
|
| 97 | + * @param int $semantics |
|
| 98 | + * |
|
| 99 | + * @returns string |
|
| 100 | + */ |
|
| 101 | 101 | public static function semanticsToString(int $semantics) |
| 102 | 102 | { |
| 103 | 103 | switch($semantics) |
@@ -118,19 +118,19 @@ discard block |
||
| 118 | 118 | // |
| 119 | 119 | |
| 120 | 120 | /** |
| 121 | - * Rule contructor. |
|
| 122 | - * |
|
| 123 | - * Constructs a new Rule. |
|
| 124 | - * |
|
| 125 | - * @since 1.0.0 |
|
| 126 | - * |
|
| 127 | - * @param string $name |
|
| 128 | - * @param string $example |
|
| 129 | - * @param string $syntax |
|
| 130 | - * @param array $semantics |
|
| 131 | - * |
|
| 132 | - * @throws ArghException |
|
| 133 | - */ |
|
| 121 | + * Rule contructor. |
|
| 122 | + * |
|
| 123 | + * Constructs a new Rule. |
|
| 124 | + * |
|
| 125 | + * @since 1.0.0 |
|
| 126 | + * |
|
| 127 | + * @param string $name |
|
| 128 | + * @param string $example |
|
| 129 | + * @param string $syntax |
|
| 130 | + * @param array $semantics |
|
| 131 | + * |
|
| 132 | + * @throws ArghException |
|
| 133 | + */ |
|
| 134 | 134 | public function __construct(string $name, string $example, string $syntax, array $semantics) |
| 135 | 135 | { |
| 136 | 136 | |
@@ -155,53 +155,53 @@ discard block |
||
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
| 158 | - * Gets the 'name' property of this Rule |
|
| 159 | - * |
|
| 160 | - * @since 1.0.0 |
|
| 161 | - * |
|
| 162 | - * @return string |
|
| 163 | - */ |
|
| 158 | + * Gets the 'name' property of this Rule |
|
| 159 | + * |
|
| 160 | + * @since 1.0.0 |
|
| 161 | + * |
|
| 162 | + * @return string |
|
| 163 | + */ |
|
| 164 | 164 | public function name(): string { return $this->name; } |
| 165 | 165 | |
| 166 | 166 | /** |
| 167 | - * Gets the 'syntax' property of this Rule |
|
| 168 | - * |
|
| 169 | - * @since 1.0.0 |
|
| 170 | - * |
|
| 171 | - * @return string |
|
| 172 | - */ |
|
| 167 | + * Gets the 'syntax' property of this Rule |
|
| 168 | + * |
|
| 169 | + * @since 1.0.0 |
|
| 170 | + * |
|
| 171 | + * @return string |
|
| 172 | + */ |
|
| 173 | 173 | public function syntax(): string { return $this->syntax; } |
| 174 | 174 | |
| 175 | 175 | /** |
| 176 | - * Gets the 'semantics' property of this Rule |
|
| 177 | - * |
|
| 178 | - * @since 1.0.0 |
|
| 179 | - * |
|
| 180 | - * @return array |
|
| 181 | - */ |
|
| 176 | + * Gets the 'semantics' property of this Rule |
|
| 177 | + * |
|
| 178 | + * @since 1.0.0 |
|
| 179 | + * |
|
| 180 | + * @return array |
|
| 181 | + */ |
|
| 182 | 182 | public function semantics(): array { return $this->semantics; } |
| 183 | 183 | |
| 184 | 184 | /** |
| 185 | - * Gets the 'sample' of this Rule |
|
| 186 | - * |
|
| 187 | - * @since 1.0.0 |
|
| 188 | - * |
|
| 189 | - * @return string |
|
| 190 | - */ |
|
| 185 | + * Gets the 'sample' of this Rule |
|
| 186 | + * |
|
| 187 | + * @since 1.0.0 |
|
| 188 | + * |
|
| 189 | + * @return string |
|
| 190 | + */ |
|
| 191 | 191 | public function example(): string { return $this->example; } |
| 192 | 192 | |
| 193 | 193 | /** |
| 194 | - * Does this Rule match a $string |
|
| 195 | - * |
|
| 196 | - * Note that matching a Rule does not guarantee the Rule will result |
|
| 197 | - * in a new Argument. Ultimately, the argument string may not meet all the |
|
| 198 | - * requirements of the Rule, e.g. matching defined parameter name|flag|options |
|
| 199 | - * |
|
| 200 | - * @param $string string A string to check for match with this Rule |
|
| 201 | - * @param $tokens array Reference to an array, on match will contain matching elements |
|
| 202 | - * |
|
| 203 | - * @return bool |
|
| 204 | - */ |
|
| 194 | + * Does this Rule match a $string |
|
| 195 | + * |
|
| 196 | + * Note that matching a Rule does not guarantee the Rule will result |
|
| 197 | + * in a new Argument. Ultimately, the argument string may not meet all the |
|
| 198 | + * requirements of the Rule, e.g. matching defined parameter name|flag|options |
|
| 199 | + * |
|
| 200 | + * @param $string string A string to check for match with this Rule |
|
| 201 | + * @param $tokens array Reference to an array, on match will contain matching elements |
|
| 202 | + * |
|
| 203 | + * @return bool |
|
| 204 | + */ |
|
| 205 | 205 | public function match($string, &$tokens=array()): bool |
| 206 | 206 | { |
| 207 | 207 | if( preg_match($this->syntax(), $string, $tokens) ) |
@@ -40,8 +40,8 @@ discard block |
||
| 40 | 40 | const ARGH_SEMANTICS_NAME = 3; |
| 41 | 41 | const ARGH_SEMANTICS_VALUE = 4; |
| 42 | 42 | const ARGH_SEMANTICS_LIST = 5; |
| 43 | - const ARGH_SEMANTICS_COMMAND = 6; |
|
| 44 | - const ARGH_SEMANTICS_VARIABLE = 7; |
|
| 43 | + const ARGH_SEMANTICS_COMMAND = 6; |
|
| 44 | + const ARGH_SEMANTICS_VARIABLE = 7; |
|
| 45 | 45 | |
| 46 | 46 | // |
| 47 | 47 | // PUBLIC PROPERTIES |
@@ -80,10 +80,10 @@ discard block |
||
| 80 | 80 | $syntax = null; |
| 81 | 81 | $semantics = null; |
| 82 | 82 | |
| 83 | - if( array_key_exists('name', $attributes) ) $name = $attributes['name']; |
|
| 84 | - if( array_key_exists('example', $attributes) ) $example = $attributes['example']; |
|
| 85 | - if( array_key_exists('syntax', $attributes) ) $syntax = $attributes['syntax']; |
|
| 86 | - if( array_key_exists('semantics', $attributes) ) $semantics = $attributes['semantics']; |
|
| 83 | + if (array_key_exists('name', $attributes)) $name = $attributes['name']; |
|
| 84 | + if (array_key_exists('example', $attributes)) $example = $attributes['example']; |
|
| 85 | + if (array_key_exists('syntax', $attributes)) $syntax = $attributes['syntax']; |
|
| 86 | + if (array_key_exists('semantics', $attributes)) $semantics = $attributes['semantics']; |
|
| 87 | 87 | |
| 88 | 88 | return new self($name, $example, $syntax, $semantics); |
| 89 | 89 | |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | */ |
| 101 | 101 | public static function semanticsToString(int $semantics) |
| 102 | 102 | { |
| 103 | - switch($semantics) |
|
| 103 | + switch ($semantics) |
|
| 104 | 104 | { |
| 105 | 105 | case self::ARGH_SEMANTICS_FLAG: return 'FLAG'; |
| 106 | 106 | case self::ARGH_SEMANTICS_FLAGS: return 'FLAGS'; |
@@ -136,13 +136,13 @@ discard block |
||
| 136 | 136 | |
| 137 | 137 | // Validate the syntax regular expression |
| 138 | 138 | // Suppress error messages |
| 139 | - if( @preg_match($syntax, '') === FALSE ) |
|
| 139 | + if (@preg_match($syntax, '') === FALSE) |
|
| 140 | 140 | { |
| 141 | - throw new ArghException('Rule \'' . $name . '\' syntax \'' . $syntax . '\' is not a valid regular expression'); |
|
| 141 | + throw new ArghException('Rule \'' . $name . '\' syntax \'' . $syntax . '\' is not a valid regular expression'); |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | // Confirm count(semantics) matches number of parenthesized subpatterns defined by the syntax regular expression |
| 145 | - if( substr_count($syntax, '(') != count($semantics) ) |
|
| 145 | + if (substr_count($syntax, '(') != count($semantics)) |
|
| 146 | 146 | { |
| 147 | 147 | throw new ArghException('Rule \'' . $name . '\' syntax defines ' . substr_count($syntax, '(') . ' sub-patterns, but semantics defines ' . count($semantics)); |
| 148 | 148 | } |
@@ -202,9 +202,9 @@ discard block |
||
| 202 | 202 | * |
| 203 | 203 | * @return bool |
| 204 | 204 | */ |
| 205 | - public function match($string, &$tokens=array()): bool |
|
| 205 | + public function match($string, &$tokens = array()): bool |
|
| 206 | 206 | { |
| 207 | - if( preg_match($this->syntax(), $string, $tokens) ) |
|
| 207 | + if (preg_match($this->syntax(), $string, $tokens)) |
|
| 208 | 208 | { |
| 209 | 209 | return TRUE; |
| 210 | 210 | } |
@@ -80,10 +80,18 @@ discard block |
||
| 80 | 80 | $syntax = null; |
| 81 | 81 | $semantics = null; |
| 82 | 82 | |
| 83 | - if( array_key_exists('name', $attributes) ) $name = $attributes['name']; |
|
| 84 | - if( array_key_exists('example', $attributes) ) $example = $attributes['example']; |
|
| 85 | - if( array_key_exists('syntax', $attributes) ) $syntax = $attributes['syntax']; |
|
| 86 | - if( array_key_exists('semantics', $attributes) ) $semantics = $attributes['semantics']; |
|
| 83 | + if( array_key_exists('name', $attributes) ) { |
|
| 84 | + $name = $attributes['name']; |
|
| 85 | + } |
|
| 86 | + if( array_key_exists('example', $attributes) ) { |
|
| 87 | + $example = $attributes['example']; |
|
| 88 | + } |
|
| 89 | + if( array_key_exists('syntax', $attributes) ) { |
|
| 90 | + $syntax = $attributes['syntax']; |
|
| 91 | + } |
|
| 92 | + if( array_key_exists('semantics', $attributes) ) { |
|
| 93 | + $semantics = $attributes['semantics']; |
|
| 94 | + } |
|
| 87 | 95 | |
| 88 | 96 | return new self($name, $example, $syntax, $semantics); |
| 89 | 97 | |
@@ -207,8 +215,7 @@ discard block |
||
| 207 | 215 | if( preg_match($this->syntax(), $string, $tokens) ) |
| 208 | 216 | { |
| 209 | 217 | return TRUE; |
| 210 | - } |
|
| 211 | - else |
|
| 218 | + } else |
|
| 212 | 219 | { |
| 213 | 220 | return FALSE; |
| 214 | 221 | } |
@@ -1,20 +1,20 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * ArgvPreprocessor.php |
|
| 5 | - */ |
|
| 4 | + * ArgvPreprocessor.php |
|
| 5 | + */ |
|
| 6 | 6 | |
| 7 | 7 | namespace netfocusinc\argh; |
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | - * Handles the preparation of PHP $argv arrays that is required before parsing |
|
| 11 | - * |
|
| 12 | - * @author Benjamin Hough |
|
| 13 | - * |
|
| 14 | - * @internal |
|
| 15 | - * |
|
| 16 | - * @since 1.0.0 |
|
| 17 | - */ |
|
| 10 | + * Handles the preparation of PHP $argv arrays that is required before parsing |
|
| 11 | + * |
|
| 12 | + * @author Benjamin Hough |
|
| 13 | + * |
|
| 14 | + * @internal |
|
| 15 | + * |
|
| 16 | + * @since 1.0.0 |
|
| 17 | + */ |
|
| 18 | 18 | class ArgvPreprocessor |
| 19 | 19 | { |
| 20 | 20 | |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | */ |
| 35 | 35 | public static function process(array $argv) |
| 36 | 36 | { |
| 37 | - if(count($argv) < 1) |
|
| 37 | + if (count($argv)<1) |
|
| 38 | 38 | { |
| 39 | 39 | throw(new ArghException(__CLASS__ . ': $argv is empty')); |
| 40 | 40 | } |
@@ -49,32 +49,32 @@ discard block |
||
| 49 | 49 | // Lists |
| 50 | 50 | // |
| 51 | 51 | |
| 52 | - for($i=0; $i<count($args); $i++) |
|
| 52 | + for ($i = 0; $i<count($args); $i++) |
|
| 53 | 53 | { |
| 54 | 54 | // Lists begin with an opening bracket '[' |
| 55 | - if( strpos($args[$i], '[') !== FALSE ) |
|
| 55 | + if (strpos($args[$i], '[') !== FALSE) |
|
| 56 | 56 | { |
| 57 | 57 | // Found the beginning of a list at $i |
| 58 | 58 | //echo "DEBUG: Found beginning of a list at $i\n"; |
| 59 | 59 | |
| 60 | 60 | // Search for the end of the list; starting at the current element |
| 61 | - for($j=$i; $j<count($args); $j++) |
|
| 61 | + for ($j = $i; $j<count($args); $j++) |
|
| 62 | 62 | { |
| 63 | - if( strpos($args[$j], ']') !== FALSE ) |
|
| 63 | + if (strpos($args[$j], ']') !== FALSE) |
|
| 64 | 64 | { |
| 65 | 65 | // Found the end of a list at $j |
| 66 | 66 | //echo "DEBUG: Found end of a list at $j\n"; |
| 67 | 67 | |
| 68 | - if($j != $i) |
|
| 68 | + if ($j != $i) |
|
| 69 | 69 | { |
| 70 | 70 | // List does NOT open and close in the same element; elements must be re-combined |
| 71 | 71 | |
| 72 | 72 | // List closing bracket should always be the last character of the element |
| 73 | - if( strpos($args[$j], ']') == (strlen($args[$j])-1) ) |
|
| 73 | + if (strpos($args[$j], ']') == (strlen($args[$j])-1)) |
|
| 74 | 74 | { |
| 75 | 75 | // Create a new string for $args $i thru $j |
| 76 | 76 | $list = ""; |
| 77 | - for($k=$i; $k<=$j; $k++) $list .= $args[$k]; |
|
| 77 | + for ($k = $i; $k<=$j; $k++) $list .= $args[$k]; |
|
| 78 | 78 | |
| 79 | 79 | //echo "DEBUG: Replace elements $i thru $j with: $list\n"; |
| 80 | 80 | |
@@ -108,13 +108,13 @@ discard block |
||
| 108 | 108 | // |
| 109 | 109 | |
| 110 | 110 | // Check for arguments with spaces, these were originally quoted on the command line |
| 111 | - for($i=0; $i<count($args); $i++) |
|
| 111 | + for ($i = 0; $i<count($args); $i++) |
|
| 112 | 112 | { |
| 113 | - if( strpos($args[$i], ' ') !== FALSE ) |
|
| 113 | + if (strpos($args[$i], ' ') !== FALSE) |
|
| 114 | 114 | { |
| 115 | 115 | |
| 116 | 116 | // Check if argument is a list |
| 117 | - if( strpos($args[$i], '[') !== FALSE ) |
|
| 117 | + if (strpos($args[$i], '[') !== FALSE) |
|
| 118 | 118 | { |
| 119 | 119 | |
| 120 | 120 | // |
@@ -128,7 +128,7 @@ discard block |
||
| 128 | 128 | //!TODO: handle --msg="Hello World"; We don't want "--msg=Hello World" |
| 129 | 129 | //! TODO: FIX. THIS IS BROKEN |
| 130 | 130 | |
| 131 | - if( strpos($args[$i], '=') === FALSE ) |
|
| 131 | + if (strpos($args[$i], '=') === FALSE) |
|
| 132 | 132 | { |
| 133 | 133 | // Wrap (space containing) argument in quotes |
| 134 | 134 | $args[$i] = "'" . $args[$i] . "'"; |
@@ -74,7 +74,9 @@ discard block |
||
| 74 | 74 | { |
| 75 | 75 | // Create a new string for $args $i thru $j |
| 76 | 76 | $list = ""; |
| 77 | - for($k=$i; $k<=$j; $k++) $list .= $args[$k]; |
|
| 77 | + for($k=$i; $k<=$j; $k++) { |
|
| 78 | + $list .= $args[$k]; |
|
| 79 | + } |
|
| 78 | 80 | |
| 79 | 81 | //echo "DEBUG: Replace elements $i thru $j with: $list\n"; |
| 80 | 82 | |
@@ -83,8 +85,7 @@ discard block |
||
| 83 | 85 | |
| 84 | 86 | // Stop searching for the end of the list |
| 85 | 87 | break; |
| 86 | - } |
|
| 87 | - else |
|
| 88 | + } else |
|
| 88 | 89 | { |
| 89 | 90 | throw new ArghException(__METHOD__ . ': Syntax Error: Invalid list.'); |
| 90 | 91 | } |
@@ -132,8 +133,7 @@ discard block |
||
| 132 | 133 | { |
| 133 | 134 | // Wrap (space containing) argument in quotes |
| 134 | 135 | $args[$i] = "'" . $args[$i] . "'"; |
| 135 | - } |
|
| 136 | - else |
|
| 136 | + } else |
|
| 137 | 137 | { |
| 138 | 138 | // Wrap (space containing) argument value - after '=' sign in quotes |
| 139 | 139 | |
@@ -1,8 +1,8 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * ParameterCollection.php |
|
| 5 | - */ |
|
| 4 | + * ParameterCollection.php |
|
| 5 | + */ |
|
| 6 | 6 | |
| 7 | 7 | namespace netfocusinc\argh; |
| 8 | 8 | |
@@ -10,19 +10,19 @@ discard block |
||
| 10 | 10 | use netfocusinc\argh\Parameter; |
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | - * Representation of a collection of Parameters |
|
| 14 | - * |
|
| 15 | - * A ParameterCollection maintains a list of the Parameters used to interpret command line arguments |
|
| 16 | - * In addition to maintaining the Parameter list, this class is also responsible for "merging" Arguments |
|
| 17 | - * with Parameters. This process involves using the Arguments (as parsed from the command line) to set |
|
| 18 | - * values of Parameters in the collection. |
|
| 19 | - * |
|
| 20 | - * @internal |
|
| 21 | - * |
|
| 22 | - * @author Benjamin Hough |
|
| 23 | - * |
|
| 24 | - * @since 1.0.0 |
|
| 25 | - */ |
|
| 13 | + * Representation of a collection of Parameters |
|
| 14 | + * |
|
| 15 | + * A ParameterCollection maintains a list of the Parameters used to interpret command line arguments |
|
| 16 | + * In addition to maintaining the Parameter list, this class is also responsible for "merging" Arguments |
|
| 17 | + * with Parameters. This process involves using the Arguments (as parsed from the command line) to set |
|
| 18 | + * values of Parameters in the collection. |
|
| 19 | + * |
|
| 20 | + * @internal |
|
| 21 | + * |
|
| 22 | + * @author Benjamin Hough |
|
| 23 | + * |
|
| 24 | + * @since 1.0.0 |
|
| 25 | + */ |
|
| 26 | 26 | class ParameterCollection |
| 27 | 27 | { |
| 28 | 28 | // |
@@ -40,12 +40,12 @@ discard block |
||
| 40 | 40 | // |
| 41 | 41 | |
| 42 | 42 | /** |
| 43 | - * Magic function returns a string representation of this ParameterCollection |
|
| 44 | - * |
|
| 45 | - * @since 1.0.0 |
|
| 46 | - * |
|
| 47 | - * @return string |
|
| 48 | - */ |
|
| 43 | + * Magic function returns a string representation of this ParameterCollection |
|
| 44 | + * |
|
| 45 | + * @since 1.0.0 |
|
| 46 | + * |
|
| 47 | + * @return string |
|
| 48 | + */ |
|
| 49 | 49 | public function __toString() |
| 50 | 50 | { |
| 51 | 51 | return $this->toString(); |
@@ -56,13 +56,13 @@ discard block |
||
| 56 | 56 | // |
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | - * ParameterCollection constructor |
|
| 60 | - * |
|
| 61 | - * Constructs a new ParameterCollection with an empty list of Parameters |
|
| 62 | - * and a "map" that can be used to reference Parameters in the collection by 'name' or 'tag' |
|
| 63 | - * |
|
| 64 | - * @since 1.0.0 |
|
| 65 | - */ |
|
| 59 | + * ParameterCollection constructor |
|
| 60 | + * |
|
| 61 | + * Constructs a new ParameterCollection with an empty list of Parameters |
|
| 62 | + * and a "map" that can be used to reference Parameters in the collection by 'name' or 'tag' |
|
| 63 | + * |
|
| 64 | + * @since 1.0.0 |
|
| 65 | + */ |
|
| 66 | 66 | public function __construct() |
| 67 | 67 | { |
| 68 | 68 | // Create a Parameter array |
@@ -73,14 +73,14 @@ discard block |
||
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
| 76 | - * Returns TRUE when the specified $key matches the 'name' or 'flag' of a Parameter in the collection |
|
| 77 | - * |
|
| 78 | - * @since 1.0.0 |
|
| 79 | - * |
|
| 80 | - * @param string $key |
|
| 81 | - * |
|
| 82 | - * @return bool Returns TRUE when the specified $key matches a Parameter in the collection; FALSE otherwise. |
|
| 83 | - */ |
|
| 76 | + * Returns TRUE when the specified $key matches the 'name' or 'flag' of a Parameter in the collection |
|
| 77 | + * |
|
| 78 | + * @since 1.0.0 |
|
| 79 | + * |
|
| 80 | + * @param string $key |
|
| 81 | + * |
|
| 82 | + * @return bool Returns TRUE when the specified $key matches a Parameter in the collection; FALSE otherwise. |
|
| 83 | + */ |
|
| 84 | 84 | public function exists(string $key): bool |
| 85 | 85 | { |
| 86 | 86 | if( array_key_exists($key, $this->map) ) |
@@ -92,12 +92,12 @@ discard block |
||
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | - * Returns a boolean indicating if this collection contains a CommandParameter |
|
| 96 | - * |
|
| 97 | - * @since 1.0.0 |
|
| 98 | - * |
|
| 99 | - * @return bool Returns TRUE when the collection contains a CommandParameter |
|
| 100 | - */ |
|
| 95 | + * Returns a boolean indicating if this collection contains a CommandParameter |
|
| 96 | + * |
|
| 97 | + * @since 1.0.0 |
|
| 98 | + * |
|
| 99 | + * @return bool Returns TRUE when the collection contains a CommandParameter |
|
| 100 | + */ |
|
| 101 | 101 | public function hasCommand(): bool |
| 102 | 102 | { |
| 103 | 103 | foreach($this->parameters as $p) |
@@ -111,12 +111,12 @@ discard block |
||
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | /** |
| 114 | - * Returns a boolean indicating if this collection contains a VariableParameter |
|
| 115 | - * |
|
| 116 | - * @since 1.0.0 |
|
| 117 | - * |
|
| 118 | - * @return bool Returns TRUE when the collection contains a VariableParameter |
|
| 119 | - */ |
|
| 114 | + * Returns a boolean indicating if this collection contains a VariableParameter |
|
| 115 | + * |
|
| 116 | + * @since 1.0.0 |
|
| 117 | + * |
|
| 118 | + * @return bool Returns TRUE when the collection contains a VariableParameter |
|
| 119 | + */ |
|
| 120 | 120 | public function hasVariable(): bool |
| 121 | 121 | { |
| 122 | 122 | foreach($this->parameters as $p) |
@@ -130,15 +130,15 @@ discard block |
||
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | /** |
| 133 | - * Retrieves a Parameter in this collection by 'name' or 'flag' |
|
| 134 | - * |
|
| 135 | - * @since 1.0.0 |
|
| 136 | - * |
|
| 137 | - * @param string $key The 'name' or 'flag' of a Parameter |
|
| 138 | - * |
|
| 139 | - * @return Parameter |
|
| 140 | - * @throws ArghException When there is no Parameter in the collection matching the specified $key |
|
| 141 | - */ |
|
| 133 | + * Retrieves a Parameter in this collection by 'name' or 'flag' |
|
| 134 | + * |
|
| 135 | + * @since 1.0.0 |
|
| 136 | + * |
|
| 137 | + * @param string $key The 'name' or 'flag' of a Parameter |
|
| 138 | + * |
|
| 139 | + * @return Parameter |
|
| 140 | + * @throws ArghException When there is no Parameter in the collection matching the specified $key |
|
| 141 | + */ |
|
| 142 | 142 | public function get(string $key): Parameter |
| 143 | 143 | { |
| 144 | 144 | if($this->exists($key)) |
@@ -154,15 +154,15 @@ discard block |
||
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | /** |
| 157 | - * Returns an array of command strings |
|
| 158 | - * |
|
| 159 | - * If this collection contains any CommandParameters, |
|
| 160 | - * this method will return an array of the values defined by these commands. |
|
| 161 | - * |
|
| 162 | - * @since 1.0.0 |
|
| 163 | - * |
|
| 164 | - * @return array |
|
| 165 | - */ |
|
| 157 | + * Returns an array of command strings |
|
| 158 | + * |
|
| 159 | + * If this collection contains any CommandParameters, |
|
| 160 | + * this method will return an array of the values defined by these commands. |
|
| 161 | + * |
|
| 162 | + * @since 1.0.0 |
|
| 163 | + * |
|
| 164 | + * @return array |
|
| 165 | + */ |
|
| 166 | 166 | public function getCommands(): array |
| 167 | 167 | { |
| 168 | 168 | $commands = array(); |
@@ -179,14 +179,14 @@ discard block |
||
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | /** |
| 182 | - * Adds a Parameter to the array of Parameters maintained by this collection. |
|
| 183 | - * |
|
| 184 | - * @since 1.0.0 |
|
| 185 | - * |
|
| 186 | - * @param Parameter $param |
|
| 187 | - * |
|
| 188 | - * @throws ArghException If a Parameter with the same 'name' already exists |
|
| 189 | - */ |
|
| 182 | + * Adds a Parameter to the array of Parameters maintained by this collection. |
|
| 183 | + * |
|
| 184 | + * @since 1.0.0 |
|
| 185 | + * |
|
| 186 | + * @param Parameter $param |
|
| 187 | + * |
|
| 188 | + * @throws ArghException If a Parameter with the same 'name' already exists |
|
| 189 | + */ |
|
| 190 | 190 | public function addParameter(Parameter $param) |
| 191 | 191 | { |
| 192 | 192 | if( !$this->exists($param->getName()) ) |
@@ -207,15 +207,15 @@ discard block |
||
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | /** |
| 210 | - * Given an array of Arguments, this method sets the 'value' of Parameters in the collection |
|
| 211 | - * with the 'value' of its corresponding Argument. |
|
| 212 | - * |
|
| 213 | - * @since 1.0.0 |
|
| 214 | - * |
|
| 215 | - * @param array $arguments An array of Arguments |
|
| 216 | - * |
|
| 217 | - * @throws ArgumentException |
|
| 218 | - */ |
|
| 210 | + * Given an array of Arguments, this method sets the 'value' of Parameters in the collection |
|
| 211 | + * with the 'value' of its corresponding Argument. |
|
| 212 | + * |
|
| 213 | + * @since 1.0.0 |
|
| 214 | + * |
|
| 215 | + * @param array $arguments An array of Arguments |
|
| 216 | + * |
|
| 217 | + * @throws ArgumentException |
|
| 218 | + */ |
|
| 219 | 219 | public function mergeArguments(array $arguments): void |
| 220 | 220 | { |
| 221 | 221 | |
@@ -265,24 +265,24 @@ discard block |
||
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | /** |
| 268 | - * Returns the array of Parameters in this collection. |
|
| 269 | - * |
|
| 270 | - * @since 1.0.0 |
|
| 271 | - * |
|
| 272 | - * @return array Array of Parameters in this collection. |
|
| 273 | - */ |
|
| 268 | + * Returns the array of Parameters in this collection. |
|
| 269 | + * |
|
| 270 | + * @since 1.0.0 |
|
| 271 | + * |
|
| 272 | + * @return array Array of Parameters in this collection. |
|
| 273 | + */ |
|
| 274 | 274 | public function all() |
| 275 | 275 | { |
| 276 | 276 | return $this->parameters; |
| 277 | 277 | } |
| 278 | 278 | |
| 279 | 279 | /** |
| 280 | - * Returns a string representation of this ParameterCollection. |
|
| 281 | - * |
|
| 282 | - * @since 1.0.0 |
|
| 283 | - * |
|
| 284 | - * @return string Returns |
|
| 285 | - */ |
|
| 280 | + * Returns a string representation of this ParameterCollection. |
|
| 281 | + * |
|
| 282 | + * @since 1.0.0 |
|
| 283 | + * |
|
| 284 | + * @return string Returns |
|
| 285 | + */ |
|
| 286 | 286 | public function toString() |
| 287 | 287 | { |
| 288 | 288 | return print_r($this->parameters, TRUE); |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | */ |
| 84 | 84 | public function exists(string $key): bool |
| 85 | 85 | { |
| 86 | - if( array_key_exists($key, $this->map) ) |
|
| 86 | + if (array_key_exists($key, $this->map)) |
|
| 87 | 87 | { |
| 88 | 88 | return true; |
| 89 | 89 | } |
@@ -100,9 +100,9 @@ discard block |
||
| 100 | 100 | */ |
| 101 | 101 | public function hasCommand(): bool |
| 102 | 102 | { |
| 103 | - foreach($this->parameters as $p) |
|
| 103 | + foreach ($this->parameters as $p) |
|
| 104 | 104 | { |
| 105 | - if( Parameter::ARGH_TYPE_COMMAND == $p->getParameterType() ) |
|
| 105 | + if (Parameter::ARGH_TYPE_COMMAND == $p->getParameterType()) |
|
| 106 | 106 | { |
| 107 | 107 | return true; |
| 108 | 108 | } |
@@ -119,9 +119,9 @@ discard block |
||
| 119 | 119 | */ |
| 120 | 120 | public function hasVariable(): bool |
| 121 | 121 | { |
| 122 | - foreach($this->parameters as $p) |
|
| 122 | + foreach ($this->parameters as $p) |
|
| 123 | 123 | { |
| 124 | - if( Parameter::ARGH_TYPE_VARIABLE == $p->getParameterType() ) |
|
| 124 | + if (Parameter::ARGH_TYPE_VARIABLE == $p->getParameterType()) |
|
| 125 | 125 | { |
| 126 | 126 | return true; |
| 127 | 127 | } |
@@ -141,7 +141,7 @@ discard block |
||
| 141 | 141 | */ |
| 142 | 142 | public function get(string $key): Parameter |
| 143 | 143 | { |
| 144 | - if($this->exists($key)) |
|
| 144 | + if ($this->exists($key)) |
|
| 145 | 145 | { |
| 146 | 146 | $index = $this->map[$key]; |
| 147 | 147 | |
@@ -167,9 +167,9 @@ discard block |
||
| 167 | 167 | { |
| 168 | 168 | $commands = array(); |
| 169 | 169 | |
| 170 | - foreach($this->parameters as $p) |
|
| 170 | + foreach ($this->parameters as $p) |
|
| 171 | 171 | { |
| 172 | - if( Parameter::ARGH_TYPE_COMMAND == $p->getParameterType() ) |
|
| 172 | + if (Parameter::ARGH_TYPE_COMMAND == $p->getParameterType()) |
|
| 173 | 173 | { |
| 174 | 174 | $commands[] = $p; |
| 175 | 175 | } |
@@ -189,7 +189,7 @@ discard block |
||
| 189 | 189 | */ |
| 190 | 190 | public function addParameter(Parameter $param) |
| 191 | 191 | { |
| 192 | - if( !$this->exists($param->getName()) ) |
|
| 192 | + if (!$this->exists($param->getName())) |
|
| 193 | 193 | { |
| 194 | 194 | // Add $param to $parameters array |
| 195 | 195 | $this->parameters[] = $param; |
@@ -198,7 +198,7 @@ discard block |
||
| 198 | 198 | $this->map[$param->getName()] = count($this->parameters)-1; |
| 199 | 199 | |
| 200 | 200 | // Map the new parameter's 'flag' to its corresponding index in the $parameters array |
| 201 | - if(!empty($param->getFlag())) $this->map[$param->getFlag()] = count($this->parameters)-1; |
|
| 201 | + if (!empty($param->getFlag())) $this->map[$param->getFlag()] = count($this->parameters)-1; |
|
| 202 | 202 | } |
| 203 | 203 | else |
| 204 | 204 | { |
@@ -219,21 +219,21 @@ discard block |
||
| 219 | 219 | public function mergeArguments(array $arguments): void |
| 220 | 220 | { |
| 221 | 221 | |
| 222 | - foreach($arguments as $a) |
|
| 222 | + foreach ($arguments as $a) |
|
| 223 | 223 | { |
| 224 | 224 | // Check for a Parameter with this Arguments key |
| 225 | - if( $this->exists($a->getKey()) ) |
|
| 225 | + if ($this->exists($a->getKey())) |
|
| 226 | 226 | { |
| 227 | 227 | // Enforce limitations of Parameters |
| 228 | 228 | // 1. Do NOT allow value to be redefined |
| 229 | 229 | // 2. ARGH_TYPE_VARIABLE (VariableParameter) can have values appended |
| 230 | 230 | |
| 231 | - if( Parameter::ARGH_TYPE_VARIABLE == $this->parameters[$this->map[$a->getKey()]]->getParameterType() ) |
|
| 231 | + if (Parameter::ARGH_TYPE_VARIABLE == $this->parameters[$this->map[$a->getKey()]]->getParameterType()) |
|
| 232 | 232 | { |
| 233 | 233 | // Call VariableParameters::addValue() method |
| 234 | 234 | $this->parameters[$this->map[$a->getKey()]]->addValue($a->getValue()); |
| 235 | 235 | } |
| 236 | - else if( null !== $this->parameters[$this->map[$a->getKey()]]->getValue() ) |
|
| 236 | + else if (null !== $this->parameters[$this->map[$a->getKey()]]->getValue()) |
|
| 237 | 237 | { |
| 238 | 238 | // |
| 239 | 239 | // Do NOT allow a Parameter's value to be redefined |
@@ -255,11 +255,11 @@ discard block |
||
| 255 | 255 | } // END: foreach($arguments as $a) |
| 256 | 256 | |
| 257 | 257 | // Check for REQUIRED Parameters without any value |
| 258 | - foreach($this->parameters as $p) |
|
| 258 | + foreach ($this->parameters as $p) |
|
| 259 | 259 | { |
| 260 | - if( ($p->isRequired() ) && (null == $p->getValue()) ) |
|
| 260 | + if (($p->isRequired()) && (null == $p->getValue())) |
|
| 261 | 261 | { |
| 262 | - throw(new ArghException(__CLASS__ . ': Missing required parameter \'' . $p->getName() . '\'.')); |
|
| 262 | + throw(new ArghException(__CLASS__ . ': Missing required parameter \'' . $p->getName() . '\'.')); |
|
| 263 | 263 | } |
| 264 | 264 | } |
| 265 | 265 | } |
@@ -146,8 +146,7 @@ discard block |
||
| 146 | 146 | $index = $this->map[$key]; |
| 147 | 147 | |
| 148 | 148 | return $this->parameters[$index]; |
| 149 | - } |
|
| 150 | - else |
|
| 149 | + } else |
|
| 151 | 150 | { |
| 152 | 151 | throw new ArghException('Parameter \'' . $key . '\' not in collection'); |
| 153 | 152 | } |
@@ -198,9 +197,10 @@ discard block |
||
| 198 | 197 | $this->map[$param->getName()] = count($this->parameters)-1; |
| 199 | 198 | |
| 200 | 199 | // Map the new parameter's 'flag' to its corresponding index in the $parameters array |
| 201 | - if(!empty($param->getFlag())) $this->map[$param->getFlag()] = count($this->parameters)-1; |
|
| 202 | - } |
|
| 203 | - else |
|
| 200 | + if(!empty($param->getFlag())) { |
|
| 201 | + $this->map[$param->getFlag()] = count($this->parameters)-1; |
|
| 202 | + } |
|
| 203 | + } else |
|
| 204 | 204 | { |
| 205 | 205 | throw(new ArghException(__CLASS__ . ': Parameter \'' . $param->name() . '\' cannot be redefined.')); |
| 206 | 206 | } |
@@ -232,22 +232,19 @@ discard block |
||
| 232 | 232 | { |
| 233 | 233 | // Call VariableParameters::addValue() method |
| 234 | 234 | $this->parameters[$this->map[$a->getKey()]]->addValue($a->getValue()); |
| 235 | - } |
|
| 236 | - else if( null !== $this->parameters[$this->map[$a->getKey()]]->getValue() ) |
|
| 235 | + } else if( null !== $this->parameters[$this->map[$a->getKey()]]->getValue() ) |
|
| 237 | 236 | { |
| 238 | 237 | // |
| 239 | 238 | // Do NOT allow a Parameter's value to be redefined |
| 240 | 239 | // |
| 241 | 240 | |
| 242 | 241 | throw(new ArghException(__CLASS__ . ': Parameter \'' . $a->getKey() . '\' value cannot be redefined.')); |
| 243 | - } |
|
| 244 | - else |
|
| 242 | + } else |
|
| 245 | 243 | { |
| 246 | 244 | // Set Parameter value |
| 247 | 245 | $this->parameters[$this->map[$a->getKey()]]->setValue($a->getValue()); |
| 248 | 246 | } |
| 249 | - } |
|
| 250 | - else |
|
| 247 | + } else |
|
| 251 | 248 | { |
| 252 | 249 | throw(new ArghException(__CLASS__ . ': Cannot merge Argument \'' . $a->getKey() . '\'. Parameter not defined.')); |
| 253 | 250 | } |
@@ -1,23 +1,23 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * Parameter.php |
|
| 5 | - */ |
|
| 4 | + * Parameter.php |
|
| 5 | + */ |
|
| 6 | 6 | |
| 7 | 7 | namespace netfocusinc\argh; |
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | - * Parameters define the arguments your CLI application can recieve. |
|
| 11 | - * |
|
| 12 | - * Parameters are pre-configured arguments that your CLI application can retrieve |
|
| 13 | - * from command line arguments. |
|
| 14 | - * |
|
| 15 | - * @api |
|
| 16 | - * |
|
| 17 | - * @author Benjamin Hough |
|
| 18 | - * |
|
| 19 | - * @since 1.0.0 |
|
| 20 | - */ |
|
| 10 | + * Parameters define the arguments your CLI application can recieve. |
|
| 11 | + * |
|
| 12 | + * Parameters are pre-configured arguments that your CLI application can retrieve |
|
| 13 | + * from command line arguments. |
|
| 14 | + * |
|
| 15 | + * @api |
|
| 16 | + * |
|
| 17 | + * @author Benjamin Hough |
|
| 18 | + * |
|
| 19 | + * @since 1.0.0 |
|
| 20 | + */ |
|
| 21 | 21 | abstract class Parameter |
| 22 | 22 | { |
| 23 | 23 | |
@@ -59,12 +59,12 @@ discard block |
||
| 59 | 59 | private $options; |
| 60 | 60 | |
| 61 | 61 | /** |
| 62 | - * @var mixed The value of this parameter. |
|
| 63 | - * |
|
| 64 | - * Null indicates no Argument was supplied on the command line. |
|
| 65 | - * |
|
| 66 | - * Each Parameter may have its own type of value (e.g. int, string, array) |
|
| 67 | - */ |
|
| 62 | + * @var mixed The value of this parameter. |
|
| 63 | + * |
|
| 64 | + * Null indicates no Argument was supplied on the command line. |
|
| 65 | + * |
|
| 66 | + * Each Parameter may have its own type of value (e.g. int, string, array) |
|
| 67 | + */ |
|
| 68 | 68 | protected $value; |
| 69 | 69 | |
| 70 | 70 | // |
@@ -72,20 +72,20 @@ discard block |
||
| 72 | 72 | // |
| 73 | 73 | |
| 74 | 74 | /** |
| 75 | - * Creates a new Parameter (sub-type) using the provided attributes |
|
| 76 | - * |
|
| 77 | - * This function is called statically on the subtypes of Parameter (e.g. BooleanParameter) |
|
| 78 | - * It uses the supplied attributes to construct a new Parameter. |
|
| 79 | - * |
|
| 80 | - * @api |
|
| 81 | - * |
|
| 82 | - * @since 1.0.0 |
|
| 83 | - * |
|
| 84 | - * @param array $attributes |
|
| 85 | - * |
|
| 86 | - * @return Parameter |
|
| 87 | - * @throws ArghException |
|
| 88 | - */ |
|
| 75 | + * Creates a new Parameter (sub-type) using the provided attributes |
|
| 76 | + * |
|
| 77 | + * This function is called statically on the subtypes of Parameter (e.g. BooleanParameter) |
|
| 78 | + * It uses the supplied attributes to construct a new Parameter. |
|
| 79 | + * |
|
| 80 | + * @api |
|
| 81 | + * |
|
| 82 | + * @since 1.0.0 |
|
| 83 | + * |
|
| 84 | + * @param array $attributes |
|
| 85 | + * |
|
| 86 | + * @return Parameter |
|
| 87 | + * @throws ArghException |
|
| 88 | + */ |
|
| 89 | 89 | public static function createWithAttributes(array $attributes): Parameter |
| 90 | 90 | { |
| 91 | 91 | // Init default attributes for a new Parameter |
@@ -116,24 +116,24 @@ discard block |
||
| 116 | 116 | // |
| 117 | 117 | |
| 118 | 118 | /** |
| 119 | - * Parameter contructor. |
|
| 120 | - * |
|
| 121 | - * This function defines a constructor that is leveraged by Parameter sub-types. |
|
| 122 | - * Normally, Parameter (sub-types, e.g. BooleanParameter) are creating using the static Parameter:createWithAttributes() function. |
|
| 123 | - * Parameter is an abstract class, and as such cannot be instantiated directly. |
|
| 124 | - * |
|
| 125 | - * @since 1.0.0 |
|
| 126 | - * |
|
| 127 | - * @param string $name |
|
| 128 | - * @param string $flag |
|
| 129 | - * @param bool $required |
|
| 130 | - * @param mixed $default |
|
| 131 | - * @param string $description |
|
| 132 | - * @param array $options |
|
| 133 | - * |
|
| 134 | - * @return Parameter |
|
| 135 | - * @throws ArghException |
|
| 136 | - */ |
|
| 119 | + * Parameter contructor. |
|
| 120 | + * |
|
| 121 | + * This function defines a constructor that is leveraged by Parameter sub-types. |
|
| 122 | + * Normally, Parameter (sub-types, e.g. BooleanParameter) are creating using the static Parameter:createWithAttributes() function. |
|
| 123 | + * Parameter is an abstract class, and as such cannot be instantiated directly. |
|
| 124 | + * |
|
| 125 | + * @since 1.0.0 |
|
| 126 | + * |
|
| 127 | + * @param string $name |
|
| 128 | + * @param string $flag |
|
| 129 | + * @param bool $required |
|
| 130 | + * @param mixed $default |
|
| 131 | + * @param string $description |
|
| 132 | + * @param array $options |
|
| 133 | + * |
|
| 134 | + * @return Parameter |
|
| 135 | + * @throws ArghException |
|
| 136 | + */ |
|
| 137 | 137 | public function __construct(string $name, string $flag=null, bool $required=FALSE, $default=null, string $description=null, array $options=array()) |
| 138 | 138 | { |
| 139 | 139 | // Required a non-empty 'name' |
@@ -159,66 +159,66 @@ discard block |
||
| 159 | 159 | // |
| 160 | 160 | |
| 161 | 161 | /** |
| 162 | - * Returns the text 'name' of this Parameter |
|
| 163 | - * |
|
| 164 | - * @since 1.0.0 |
|
| 165 | - * |
|
| 166 | - * @return string |
|
| 167 | - */ |
|
| 162 | + * Returns the text 'name' of this Parameter |
|
| 163 | + * |
|
| 164 | + * @since 1.0.0 |
|
| 165 | + * |
|
| 166 | + * @return string |
|
| 167 | + */ |
|
| 168 | 168 | public function getName(): string { return $this->name; } |
| 169 | 169 | |
| 170 | 170 | /** |
| 171 | - * Returns the character 'flag' of this Parameter |
|
| 172 | - * |
|
| 173 | - * @since 1.0.0 |
|
| 174 | - * |
|
| 175 | - * @return string |
|
| 176 | - */ |
|
| 171 | + * Returns the character 'flag' of this Parameter |
|
| 172 | + * |
|
| 173 | + * @since 1.0.0 |
|
| 174 | + * |
|
| 175 | + * @return string |
|
| 176 | + */ |
|
| 177 | 177 | public function getFlag() { return $this->flag; } |
| 178 | 178 | |
| 179 | 179 | /** |
| 180 | - * Returns the a boolean value indicating if this Parameter is required |
|
| 181 | - * |
|
| 182 | - * @since 1.0.0 |
|
| 183 | - * |
|
| 184 | - * @return boolean |
|
| 185 | - */ |
|
| 180 | + * Returns the a boolean value indicating if this Parameter is required |
|
| 181 | + * |
|
| 182 | + * @since 1.0.0 |
|
| 183 | + * |
|
| 184 | + * @return boolean |
|
| 185 | + */ |
|
| 186 | 186 | public function isRequired(): bool { return $this->required; } |
| 187 | 187 | |
| 188 | 188 | /** |
| 189 | - * Returns the 'default' value of this Parmater |
|
| 190 | - * |
|
| 191 | - * @since 1.0.0 |
|
| 192 | - * |
|
| 193 | - * @return mixed |
|
| 194 | - */ |
|
| 189 | + * Returns the 'default' value of this Parmater |
|
| 190 | + * |
|
| 191 | + * @since 1.0.0 |
|
| 192 | + * |
|
| 193 | + * @return mixed |
|
| 194 | + */ |
|
| 195 | 195 | public function getDefault() { return $this->default; } |
| 196 | 196 | |
| 197 | 197 | /** |
| 198 | - * Returns the text 'description' of this Parameter |
|
| 199 | - * |
|
| 200 | - * @since 1.0.0 |
|
| 201 | - * |
|
| 202 | - * @return string |
|
| 203 | - */ |
|
| 198 | + * Returns the text 'description' of this Parameter |
|
| 199 | + * |
|
| 200 | + * @since 1.0.0 |
|
| 201 | + * |
|
| 202 | + * @return string |
|
| 203 | + */ |
|
| 204 | 204 | public function getDescription() { return $this->description; } |
| 205 | 205 | |
| 206 | 206 | /** |
| 207 | - * Returns an array of 'options' that are legal for this Parameters 'value' |
|
| 208 | - * |
|
| 209 | - * @since 1.0.0 |
|
| 210 | - * |
|
| 211 | - * @return array |
|
| 212 | - */ |
|
| 207 | + * Returns an array of 'options' that are legal for this Parameters 'value' |
|
| 208 | + * |
|
| 209 | + * @since 1.0.0 |
|
| 210 | + * |
|
| 211 | + * @return array |
|
| 212 | + */ |
|
| 213 | 213 | public function getOptions(): array { return $this->options; } |
| 214 | 214 | |
| 215 | 215 | /** |
| 216 | - * Returns a boolean indicating if this Parameter has any defined 'options' |
|
| 217 | - * |
|
| 218 | - * @since 1.0.0 |
|
| 219 | - * |
|
| 220 | - * @return bool |
|
| 221 | - */ |
|
| 216 | + * Returns a boolean indicating if this Parameter has any defined 'options' |
|
| 217 | + * |
|
| 218 | + * @since 1.0.0 |
|
| 219 | + * |
|
| 220 | + * @return bool |
|
| 221 | + */ |
|
| 222 | 222 | public function hasOptions(): bool |
| 223 | 223 | { |
| 224 | 224 | if(count($this->options) > 0) |
@@ -232,14 +232,14 @@ discard block |
||
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | /** |
| 235 | - * Returns a boolean indicating if the specified $value is a permissible 'option' of this Parameter |
|
| 236 | - * |
|
| 237 | - * @since 1.0.0 |
|
| 238 | - * |
|
| 239 | - * @param mixed $value |
|
| 240 | - * |
|
| 241 | - * @return bool |
|
| 242 | - */ |
|
| 235 | + * Returns a boolean indicating if the specified $value is a permissible 'option' of this Parameter |
|
| 236 | + * |
|
| 237 | + * @since 1.0.0 |
|
| 238 | + * |
|
| 239 | + * @param mixed $value |
|
| 240 | + * |
|
| 241 | + * @return bool |
|
| 242 | + */ |
|
| 243 | 243 | public function isOption($value): bool |
| 244 | 244 | { |
| 245 | 245 | if( in_array($value, $this->options) ) |
@@ -253,12 +253,12 @@ discard block |
||
| 253 | 253 | } |
| 254 | 254 | |
| 255 | 255 | /** |
| 256 | - * Returns the 'value' of this Parameter |
|
| 257 | - * |
|
| 258 | - * @since 1.0.0 |
|
| 259 | - * |
|
| 260 | - * @return mixed |
|
| 261 | - */ |
|
| 256 | + * Returns the 'value' of this Parameter |
|
| 257 | + * |
|
| 258 | + * @since 1.0.0 |
|
| 259 | + * |
|
| 260 | + * @return mixed |
|
| 261 | + */ |
|
| 262 | 262 | public function getValue() |
| 263 | 263 | { |
| 264 | 264 | return $this->value; |
@@ -269,21 +269,21 @@ discard block |
||
| 269 | 269 | // |
| 270 | 270 | |
| 271 | 271 | /** |
| 272 | - * Returns an int corresponding to this Parameters type |
|
| 273 | - * |
|
| 274 | - * @since 1.0.0 |
|
| 275 | - * |
|
| 276 | - * @return int |
|
| 277 | - */ |
|
| 272 | + * Returns an int corresponding to this Parameters type |
|
| 273 | + * |
|
| 274 | + * @since 1.0.0 |
|
| 275 | + * |
|
| 276 | + * @return int |
|
| 277 | + */ |
|
| 278 | 278 | abstract public function getParameterType(): int; |
| 279 | 279 | |
| 280 | 280 | /** |
| 281 | - * Sets the 'value' of this Paramter |
|
| 282 | - * |
|
| 283 | - * @since 1.0.0 |
|
| 284 | - * |
|
| 285 | - * @param mixed $value |
|
| 286 | - */ |
|
| 281 | + * Sets the 'value' of this Paramter |
|
| 282 | + * |
|
| 283 | + * @since 1.0.0 |
|
| 284 | + * |
|
| 285 | + * @param mixed $value |
|
| 286 | + */ |
|
| 287 | 287 | abstract public function setValue($value); |
| 288 | 288 | |
| 289 | 289 | } |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | // Parameter Data Types |
| 32 | 32 | const ARGH_TYPE_BOOLEAN = 1; |
| 33 | 33 | const ARGH_TYPE_INT = 2; |
| 34 | - const ARGH_TYPE_STRING = 3; |
|
| 34 | + const ARGH_TYPE_STRING = 3; |
|
| 35 | 35 | const ARGH_TYPE_LIST = 4; |
| 36 | 36 | const ARGH_TYPE_COMMAND = 5; |
| 37 | 37 | const ARGH_TYPE_VARIABLE = 6; |
@@ -97,12 +97,12 @@ discard block |
||
| 97 | 97 | $options = array(); |
| 98 | 98 | |
| 99 | 99 | // Extract parameter attributes from array |
| 100 | - if( array_key_exists('name', $attributes) ) $name = $attributes['name']; |
|
| 101 | - if( array_key_exists('flag', $attributes) ) $flag = $attributes['flag']; |
|
| 102 | - if( array_key_exists('required', $attributes) ) $required = $attributes['required']; |
|
| 103 | - if( array_key_exists('default', $attributes) ) $default = $attributes['default']; |
|
| 104 | - if( array_key_exists('description', $attributes) ) $description = $attributes['description']; |
|
| 105 | - if( array_key_exists('options', $attributes) ) $options = $attributes['options']; |
|
| 100 | + if (array_key_exists('name', $attributes)) $name = $attributes['name']; |
|
| 101 | + if (array_key_exists('flag', $attributes)) $flag = $attributes['flag']; |
|
| 102 | + if (array_key_exists('required', $attributes)) $required = $attributes['required']; |
|
| 103 | + if (array_key_exists('default', $attributes)) $default = $attributes['default']; |
|
| 104 | + if (array_key_exists('description', $attributes)) $description = $attributes['description']; |
|
| 105 | + if (array_key_exists('options', $attributes)) $options = $attributes['options']; |
|
| 106 | 106 | |
| 107 | 107 | // Construct a new Parameter instance |
| 108 | 108 | // Late static binding results in new instance of (calling) subclass |
@@ -134,10 +134,10 @@ discard block |
||
| 134 | 134 | * @return Parameter |
| 135 | 135 | * @throws ArghException |
| 136 | 136 | */ |
| 137 | - public function __construct(string $name, string $flag=null, bool $required=FALSE, $default=null, string $description=null, array $options=array()) |
|
| 137 | + public function __construct(string $name, string $flag = null, bool $required = FALSE, $default = null, string $description = null, array $options = array()) |
|
| 138 | 138 | { |
| 139 | 139 | // Required a non-empty 'name' |
| 140 | - if(empty($name)) |
|
| 140 | + if (empty($name)) |
|
| 141 | 141 | { |
| 142 | 142 | throw(new ArghException('Parameter must have a name')); |
| 143 | 143 | } |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | */ |
| 222 | 222 | public function hasOptions(): bool |
| 223 | 223 | { |
| 224 | - if(count($this->options) > 0) |
|
| 224 | + if (count($this->options)>0) |
|
| 225 | 225 | { |
| 226 | 226 | return TRUE; |
| 227 | 227 | } |
@@ -242,7 +242,7 @@ discard block |
||
| 242 | 242 | */ |
| 243 | 243 | public function isOption($value): bool |
| 244 | 244 | { |
| 245 | - if( in_array($value, $this->options) ) |
|
| 245 | + if (in_array($value, $this->options)) |
|
| 246 | 246 | { |
| 247 | 247 | return TRUE; |
| 248 | 248 | } |
@@ -97,12 +97,24 @@ discard block |
||
| 97 | 97 | $options = array(); |
| 98 | 98 | |
| 99 | 99 | // Extract parameter attributes from array |
| 100 | - if( array_key_exists('name', $attributes) ) $name = $attributes['name']; |
|
| 101 | - if( array_key_exists('flag', $attributes) ) $flag = $attributes['flag']; |
|
| 102 | - if( array_key_exists('required', $attributes) ) $required = $attributes['required']; |
|
| 103 | - if( array_key_exists('default', $attributes) ) $default = $attributes['default']; |
|
| 104 | - if( array_key_exists('description', $attributes) ) $description = $attributes['description']; |
|
| 105 | - if( array_key_exists('options', $attributes) ) $options = $attributes['options']; |
|
| 100 | + if( array_key_exists('name', $attributes) ) { |
|
| 101 | + $name = $attributes['name']; |
|
| 102 | + } |
|
| 103 | + if( array_key_exists('flag', $attributes) ) { |
|
| 104 | + $flag = $attributes['flag']; |
|
| 105 | + } |
|
| 106 | + if( array_key_exists('required', $attributes) ) { |
|
| 107 | + $required = $attributes['required']; |
|
| 108 | + } |
|
| 109 | + if( array_key_exists('default', $attributes) ) { |
|
| 110 | + $default = $attributes['default']; |
|
| 111 | + } |
|
| 112 | + if( array_key_exists('description', $attributes) ) { |
|
| 113 | + $description = $attributes['description']; |
|
| 114 | + } |
|
| 115 | + if( array_key_exists('options', $attributes) ) { |
|
| 116 | + $options = $attributes['options']; |
|
| 117 | + } |
|
| 106 | 118 | |
| 107 | 119 | // Construct a new Parameter instance |
| 108 | 120 | // Late static binding results in new instance of (calling) subclass |
@@ -224,8 +236,7 @@ discard block |
||
| 224 | 236 | if(count($this->options) > 0) |
| 225 | 237 | { |
| 226 | 238 | return TRUE; |
| 227 | - } |
|
| 228 | - else |
|
| 239 | + } else |
|
| 229 | 240 | { |
| 230 | 241 | return FALSE; |
| 231 | 242 | } |
@@ -245,8 +256,7 @@ discard block |
||
| 245 | 256 | if( in_array($value, $this->options) ) |
| 246 | 257 | { |
| 247 | 258 | return TRUE; |
| 248 | - } |
|
| 249 | - else |
|
| 259 | + } else |
|
| 250 | 260 | { |
| 251 | 261 | return FALSE; |
| 252 | 262 | } |
@@ -1,25 +1,25 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * BooleanParameter.php |
|
| 5 | - */ |
|
| 4 | + * BooleanParameter.php |
|
| 5 | + */ |
|
| 6 | 6 | |
| 7 | 7 | namespace netfocusinc\argh; |
| 8 | 8 | |
| 9 | 9 | use netfocusinc\argh\Parameter; |
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | - * Boolean parameter. |
|
| 13 | - * |
|
| 14 | - * Subtype of Parameter that represents a boolean value. |
|
| 15 | - * Boolean parameters values are restricted to boolean literal values. |
|
| 16 | - * |
|
| 17 | - * @api |
|
| 18 | - * |
|
| 19 | - * @author Benjamin Hough |
|
| 20 | - * |
|
| 21 | - * @since 1.0.0 |
|
| 22 | - */ |
|
| 12 | + * Boolean parameter. |
|
| 13 | + * |
|
| 14 | + * Subtype of Parameter that represents a boolean value. |
|
| 15 | + * Boolean parameters values are restricted to boolean literal values. |
|
| 16 | + * |
|
| 17 | + * @api |
|
| 18 | + * |
|
| 19 | + * @author Benjamin Hough |
|
| 20 | + * |
|
| 21 | + * @since 1.0.0 |
|
| 22 | + */ |
|
| 23 | 23 | class BooleanParameter extends Parameter |
| 24 | 24 | { |
| 25 | 25 | |
@@ -33,14 +33,14 @@ discard block |
||
| 33 | 33 | // |
| 34 | 34 | |
| 35 | 35 | /** |
| 36 | - * Returns a boolean default value for this Parameter. |
|
| 37 | - * |
|
| 38 | - * Overrides Parameter::getDefault() to ensure a boolean return value |
|
| 39 | - * |
|
| 40 | - * @since 1.0.0 |
|
| 41 | - * |
|
| 42 | - * @return bool |
|
| 43 | - */ |
|
| 36 | + * Returns a boolean default value for this Parameter. |
|
| 37 | + * |
|
| 38 | + * Overrides Parameter::getDefault() to ensure a boolean return value |
|
| 39 | + * |
|
| 40 | + * @since 1.0.0 |
|
| 41 | + * |
|
| 42 | + * @return bool |
|
| 43 | + */ |
|
| 44 | 44 | public function getDefault() |
| 45 | 45 | { |
| 46 | 46 | // Interpret any non-empty value as TRUE |
@@ -56,26 +56,26 @@ discard block |
||
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | - * Returns ARGH_TYPE_BOOLEAN |
|
| 60 | - * |
|
| 61 | - * @since 1.0.0 |
|
| 62 | - * |
|
| 63 | - * @return int |
|
| 64 | - */ |
|
| 59 | + * Returns ARGH_TYPE_BOOLEAN |
|
| 60 | + * |
|
| 61 | + * @since 1.0.0 |
|
| 62 | + * |
|
| 63 | + * @return int |
|
| 64 | + */ |
|
| 65 | 65 | public function getParameterType(): int |
| 66 | 66 | { |
| 67 | 67 | return Parameter::ARGH_TYPE_BOOLEAN; |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | - * Sets the boolean value of this Parameter. |
|
| 72 | - * |
|
| 73 | - * Translates any non boolean values to their boolean equivalents. |
|
| 74 | - * |
|
| 75 | - * @since 1.0.0 |
|
| 76 | - * |
|
| 77 | - * @return int |
|
| 78 | - */ |
|
| 71 | + * Sets the boolean value of this Parameter. |
|
| 72 | + * |
|
| 73 | + * Translates any non boolean values to their boolean equivalents. |
|
| 74 | + * |
|
| 75 | + * @since 1.0.0 |
|
| 76 | + * |
|
| 77 | + * @return int |
|
| 78 | + */ |
|
| 79 | 79 | public function setValue($value) |
| 80 | 80 | { |
| 81 | 81 | |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | { |
| 46 | 46 | // Interpret any non-empty value as TRUE |
| 47 | 47 | // Note that FALSE is considered to be empty() |
| 48 | - if( !empty( parent::getDefault() ) ) |
|
| 48 | + if (!empty(parent::getDefault())) |
|
| 49 | 49 | { |
| 50 | 50 | return TRUE; |
| 51 | 51 | } |
@@ -79,17 +79,17 @@ discard block |
||
| 79 | 79 | public function setValue($value) |
| 80 | 80 | { |
| 81 | 81 | |
| 82 | - if( null === $value ) |
|
| 82 | + if (null === $value) |
|
| 83 | 83 | { |
| 84 | 84 | // null (no value) considered TRUE (this is how flags work; e.g. the presence of a boolean flag -x without value means TRUE) |
| 85 | 85 | $this->value = TRUE; |
| 86 | 86 | } |
| 87 | - else if( FALSE == $value ) |
|
| 87 | + else if (FALSE == $value) |
|
| 88 | 88 | { |
| 89 | 89 | // 'Falsey' (boolean) FALSE, (int) 0, (float 0.0), (string) '0', (string) '', NULL |
| 90 | 90 | $this->value = FALSE; |
| 91 | 91 | } |
| 92 | - else if( in_array($value, array('0', 'false', 'False', 'FALSE', 'off', 'Off', 'OFF')) ) |
|
| 92 | + else if (in_array($value, array('0', 'false', 'False', 'FALSE', 'off', 'Off', 'OFF'))) |
|
| 93 | 93 | { |
| 94 | 94 | // Certain character values should be considered to mean FALSE |
| 95 | 95 | $this->value = FALSE; |
@@ -48,8 +48,7 @@ discard block |
||
| 48 | 48 | if( !empty( parent::getDefault() ) ) |
| 49 | 49 | { |
| 50 | 50 | return TRUE; |
| 51 | - } |
|
| 52 | - else |
|
| 51 | + } else |
|
| 53 | 52 | { |
| 54 | 53 | return FALSE; |
| 55 | 54 | } |
@@ -83,18 +82,15 @@ discard block |
||
| 83 | 82 | { |
| 84 | 83 | // null (no value) considered TRUE (this is how flags work; e.g. the presence of a boolean flag -x without value means TRUE) |
| 85 | 84 | $this->value = TRUE; |
| 86 | - } |
|
| 87 | - else if( FALSE == $value ) |
|
| 85 | + } else if( FALSE == $value ) |
|
| 88 | 86 | { |
| 89 | 87 | // 'Falsey' (boolean) FALSE, (int) 0, (float 0.0), (string) '0', (string) '', NULL |
| 90 | 88 | $this->value = FALSE; |
| 91 | - } |
|
| 92 | - else if( in_array($value, array('0', 'false', 'False', 'FALSE', 'off', 'Off', 'OFF')) ) |
|
| 89 | + } else if( in_array($value, array('0', 'false', 'False', 'FALSE', 'off', 'Off', 'OFF')) ) |
|
| 93 | 90 | { |
| 94 | 91 | // Certain character values should be considered to mean FALSE |
| 95 | 92 | $this->value = FALSE; |
| 96 | - } |
|
| 97 | - else |
|
| 93 | + } else |
|
| 98 | 94 | { |
| 99 | 95 | // Everything else considered TRUE |
| 100 | 96 | $this->value = TRUE; |
@@ -1,8 +1,8 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * ListParameter.php |
|
| 5 | - */ |
|
| 4 | + * ListParameter.php |
|
| 5 | + */ |
|
| 6 | 6 | |
| 7 | 7 | namespace netfocusinc\argh; |
| 8 | 8 | |
@@ -11,16 +11,16 @@ discard block |
||
| 11 | 11 | |
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | - * A List Parameter. |
|
| 15 | - * |
|
| 16 | - * Subtype of Parameter that represents a list of text values. |
|
| 17 | - * |
|
| 18 | - * @api |
|
| 19 | - * |
|
| 20 | - * @author Benjamin Hough |
|
| 21 | - * |
|
| 22 | - * @since 1.0.0 |
|
| 23 | - */ |
|
| 14 | + * A List Parameter. |
|
| 15 | + * |
|
| 16 | + * Subtype of Parameter that represents a list of text values. |
|
| 17 | + * |
|
| 18 | + * @api |
|
| 19 | + * |
|
| 20 | + * @author Benjamin Hough |
|
| 21 | + * |
|
| 22 | + * @since 1.0.0 |
|
| 23 | + */ |
|
| 24 | 24 | class ListParameter extends Parameter |
| 25 | 25 | { |
| 26 | 26 | |
@@ -33,26 +33,26 @@ discard block |
||
| 33 | 33 | // |
| 34 | 34 | |
| 35 | 35 | /** |
| 36 | - * ReturnsParameter::ARGH_TYPE_LIST |
|
| 37 | - * |
|
| 38 | - * Identifies this Parameter subtype as a ListParameter by returning constant ARGH_TYPE_LIST |
|
| 39 | - * |
|
| 40 | - * @since 1.0.0 |
|
| 41 | - * |
|
| 42 | - * @return int |
|
| 43 | - */ |
|
| 36 | + * ReturnsParameter::ARGH_TYPE_LIST |
|
| 37 | + * |
|
| 38 | + * Identifies this Parameter subtype as a ListParameter by returning constant ARGH_TYPE_LIST |
|
| 39 | + * |
|
| 40 | + * @since 1.0.0 |
|
| 41 | + * |
|
| 42 | + * @return int |
|
| 43 | + */ |
|
| 44 | 44 | public function getParameterType(): int |
| 45 | 45 | { |
| 46 | 46 | return Parameter::ARGH_TYPE_LIST; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | /** |
| 50 | - * Sets the array value of this Parameter. |
|
| 51 | - * |
|
| 52 | - * Forces all values into an array |
|
| 53 | - * |
|
| 54 | - * @since 1.0.0 |
|
| 55 | - */ |
|
| 50 | + * Sets the array value of this Parameter. |
|
| 51 | + * |
|
| 52 | + * Forces all values into an array |
|
| 53 | + * |
|
| 54 | + * @since 1.0.0 |
|
| 55 | + */ |
|
| 56 | 56 | public function setValue($value) |
| 57 | 57 | { |
| 58 | 58 | if(is_array($value)) |
@@ -66,14 +66,14 @@ discard block |
||
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
| 69 | - * Adds an element to the value of this Parameter |
|
| 70 | - * |
|
| 71 | - * Forces all values into an array |
|
| 72 | - * |
|
| 73 | - * @since 1.0.0 |
|
| 74 | - * |
|
| 75 | - * @return int |
|
| 76 | - */ |
|
| 69 | + * Adds an element to the value of this Parameter |
|
| 70 | + * |
|
| 71 | + * Forces all values into an array |
|
| 72 | + * |
|
| 73 | + * @since 1.0.0 |
|
| 74 | + * |
|
| 75 | + * @return int |
|
| 76 | + */ |
|
| 77 | 77 | public function addValue($value) |
| 78 | 78 | { |
| 79 | 79 | // Check if this Parameter has a previously set value |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | */ |
| 56 | 56 | public function setValue($value) |
| 57 | 57 | { |
| 58 | - if(is_array($value)) |
|
| 58 | + if (is_array($value)) |
|
| 59 | 59 | { |
| 60 | 60 | $this->value = $value; |
| 61 | 61 | } |
@@ -77,14 +77,14 @@ discard block |
||
| 77 | 77 | public function addValue($value) |
| 78 | 78 | { |
| 79 | 79 | // Check if this Parameter has a previously set value |
| 80 | - if($this->value === null) |
|
| 80 | + if ($this->value === null) |
|
| 81 | 81 | { |
| 82 | 82 | // Initialize this Parameters value to a new array |
| 83 | 83 | $this->value = array(); |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | // Check if the new value is an array |
| 87 | - if(!is_array($value)) |
|
| 87 | + if (!is_array($value)) |
|
| 88 | 88 | { |
| 89 | 89 | // Append new single value to this Parameters value array |
| 90 | 90 | $this->value[] = $value; |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | else |
| 93 | 93 | { |
| 94 | 94 | // Append every new value to this Parameters value array |
| 95 | - foreach($value as $v) $this->value[] = $v; |
|
| 95 | + foreach ($value as $v) $this->value[] = $v; |
|
| 96 | 96 | } |
| 97 | 97 | } |
| 98 | 98 | |
@@ -58,8 +58,7 @@ discard block |
||
| 58 | 58 | if(is_array($value)) |
| 59 | 59 | { |
| 60 | 60 | $this->value = $value; |
| 61 | - } |
|
| 62 | - else |
|
| 61 | + } else |
|
| 63 | 62 | { |
| 64 | 63 | $this->value = array($value); |
| 65 | 64 | } |
@@ -88,11 +87,12 @@ discard block |
||
| 88 | 87 | { |
| 89 | 88 | // Append new single value to this Parameters value array |
| 90 | 89 | $this->value[] = $value; |
| 91 | - } |
|
| 92 | - else |
|
| 90 | + } else |
|
| 93 | 91 | { |
| 94 | 92 | // Append every new value to this Parameters value array |
| 95 | - foreach($value as $v) $this->value[] = $v; |
|
| 93 | + foreach($value as $v) { |
|
| 94 | + $this->value[] = $v; |
|
| 95 | + } |
|
| 96 | 96 | } |
| 97 | 97 | } |
| 98 | 98 | |