CLI
CLI
- Full name:
\Hazaar\Application\Request\CLI
- Parent class:
\Hazaar\Application\Request
Properties
options
private array $options
commands
private array $commands
opt
private static array $opt
- This property is static.
Methods
init
Returns the request as a string.
public init(string[] $args): string
Parameters:
Parameter | Type | Description |
---|---|---|
$args | string[] |
setCommands
Sets the available commands.
public setCommands(array $commands): void
Format of $commands
is an array where the key is the name of the command and:
- Index 0 is the help description of the command.
- Index 1 is a string or array of optional parameters.
Example:
$cli->SetCommands([
'test' => ['Execute a test', ['when']],
'exit' => ['Exit the CLI']
]);
If a command is specified on the CLI when executing the program it will be returned with $cli->getCommand()
.
Parameters:
Parameter | Type | Description |
---|---|---|
$commands | array |
setOptions
Sets the available options.
public setOptions(array $options): void
Format of $options
is an array where the key is the name of the option and:
- Index 0 is the 'short name' of the option. See: getopts().
- Index 1 is the 'long name' of the option. See: getopts().
- Index 2 is the name of an optional parameter.
- Index 3 is the help description displayed on the help page.
- Index 4 is the name of a command that the options is limited to. Optional.
Example:
$cli->setOptions([
'help' => ['h', 'help', null, 'Display this help message.'],
'timeout' => ['t', 'timeout', 'seconds', 'Enable test mode.'],
'force' => ['f', 'force', null, 'Force things to happen.', 'command'],
])
Options are then available using $cli->getOptions()
which will return an array of the options specified on the CLI. If they have a parameter, that parameter will be the value, otherwise the value will be TRUE.
Parameters:
Parameter | Type | Description |
---|---|---|
$options | array |
getCommand
Gets the command that was used on the command line if it exists.
public getCommand(string[]& $args = null): string
Parameters:
Parameter | Type | Description |
---|---|---|
$args | string[] | an array of arguments that were specified after the command |
Return Value:
the name of the command
getOptions
Returns the currently applied options from the ARGV command line options.
public getOptions(null|mixed& $posArgs = null): array
Parameters:
Parameter | Type | Description |
---|---|---|
$posArgs | null|mixed |
showHelp
Shows a help page on the CLI for the options and commands that have been configured.
public showHelp(): int
Inherited methods
__construct
public __construct(mixed $args): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$args | mixed |
__get
Magic method to get the value of a property.
public __get(string $key): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | the name of the property to get |
Return Value:
the value of the property
__unset
Unsets a value from the request object.
public __unset(string $key): void
This method removes a value from the request object using the specified key.
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | the key of the value to unset |
getPath
Return the request path.
public getPath(bool $stripFilename = false): string
Parameters:
Parameter | Type | Description |
---|---|---|
$stripFilename | bool | If true, this will cause the function to return anything before the last '/' (including the '/') which is the full directory path name. (Similar to dirname()). |
Return Value:
The path suffix of the request URI
setPath
Sets the path of the request.
public setPath(string $path): void
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string | the path of the request |
get
Retrieve a request value.
public get(string $key, mixed $default = null): mixed
These values can be sent in a number of ways.
- In a query string. eg: http://youhost.com/controller?key=value
- As form POST data.
- As JSON encoded request body.
Only JSON encoded request bodies support data typing. All other request values will be strings.
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | The data key to retrieve |
$default | mixed | if the value is not set, use this default value |
Return Value:
most of the time this will return a string, unless data-typing is available when using JSON requests
getInt
Retrieve an integer value from the request.
public getInt(string $key, int $default = null): ?int
The most common requests will not provide data typing and data value will always be a string. This method will automatically return the requested value as an integer unless it is NULL or not set. In which case either NULL or the default value will be returned.
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | the key of the request value to return |
$default | int | a default value to use if the value is NULL or not set |
getFloat
Retrieve an float value from the request.
public getFloat(string $key, float $default = null): float
The most common requests will not provide data typing and data value will always be a string. This method will automatically return the requested value as an float unless it is NULL or not set. In which case either NULL or the default value will be returned.
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | the key of the request value to return |
$default | float | a default value to use if the value is NULL or not set |
getBool
Retrieve an boolean value from the request.
public getBool(string $key, bool $default = null): bool
The most common requests will not provide data typing and data value will always be a string. This method will automatically return the requested value as an boolean unless it is NULL or not set. In which case either NULL or the default value will be returned.
This internally uses the boolify() function so the usual bool strings are supported (t, f, true, false, 0, 1, on, off, etc).
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | the key of the request value to return |
$default | bool | a default value to use if the value is NULL or not set |
has
Check to see if a request value has been set.
public has(string[]|string $keys, bool $check_any = false): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$keys | string[]|string | the key of the request value to check for |
$check_any | bool | The check type when $key is an array. TRUE means that ANY key must exist. FALSE means ALL keys must exist. |
Return Value:
true if the value is set, False otherwise
set
Set a request value.
public set(string $key, mixed $value): void
This would not normally be used and has no internal implications on how the application will function as this data is not processed in any way. However setting request data may be useful in your application when reusing/repurposing controller actions so that they may be called from somewhere else in your application.
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | the key value to set |
$value | mixed | the new value |
remove
Removes a parameter from the request.
public remove(string $key): void
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | the key of the parameter to remove |
getParams
Return an array of request parameters as key/value pairs.
public getParams(string[] $filter_in = null, string[] $filter_out = null): array
Parameters:
Parameter | Type | Description |
---|---|---|
$filter_in | string[] | only include parameters with keys specified in this filter |
$filter_out | string[] | exclude parameters with keys specified in this filter |
Return Value:
the request parameters
hasParams
Check if the request has any parameters.
public hasParams(): bool
Return Value:
returns true if the request has parameters, false otherwise
setParams
Sets the parameters of the request.
public setParams(array $array): void
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | The array of parameters to set |
count
Returns the number of parameters in the request.
public count(): int
Return Value:
the number of parameters in the request
getMethod
public getMethod(): string
Return Value:
The request method. Usually one of GET, POST, PUT or DELETE.
init
Returns the request as a string.
protected init(array $args): string
Parameters:
Parameter | Type | Description |
---|---|---|
$args | array |
Automatically generated on 2024-11-14