Download the PHP package maplephp/prompts without Composer
On this page you can find all versions of the php package maplephp/prompts. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download maplephp/prompts
More information about maplephp/prompts
Files in maplephp/prompts
Package prompts
Short Description PHP Prompts is a pretty, interactive, lightweight, and user-friendly CLI (Command Line Interface) PHP library designed for seamless use across Linux, Unix, Darwin, and Windows.
License Apache-2.0
Homepage https://wazabii.se
Rated 5.00 based on 1 reviews
Informations about the package prompts
MaplePHP Prompts
PHP Prompts is a pretty, interactive, lightweight, and user-friendly CLI (Command Line Interface) PHP library designed for seamless use across Linux, Unix, Darwin, and Windows. It allows you to create and manage user prompts in a command-line environment, enabling the collection of various types of input with built-in validation and feedback.
Note that the prompt may not look as polished on Windows as on Unix, but it will work just as well.
Table of Contents
- Installation
- Usage Example
- Available Options
- Available Prompt Types
- Command instance
- Progress Bar
- Validation
Installation
To install PHP Prompts, use Composer:
Usage Example
Below is an example demonstrating how to use the PHP Prompts library to create interactive prompts and validate user input.
Step-by-Step Guide
-
Initialize
-
Set the Title and Description for the Prompts
This step is optional but provides context for the user.
-
Define the Prompts
Define the prompts and their respective settings, including type, message, validation rules, and error messages.
-
Execute the Prompt
Prompt the user for input based on the defined prompts above. The collected user input will be saved in the
$prompt
variable.
Available Options
-
type
- Description: Specifies the type of the prompt (e.g.,
text
,password
,toggle
,select
,list
,continue
,message
,confirm
). - Example:
"type" => "text"
- Description: Specifies the type of the prompt (e.g.,
-
message
- Description: The message or question displayed to the user.
- Example:
"message" => "Enter your full name"
-
default
- Description: A default value for the prompt, used if the user does not provide input.
- Example:
"default" => "Your default value"
-
items
- Description: An array of items to choose from, used with
select
type prompts. - Example:
"items" => ["Option 1", "Option 2", "Option 3"]
- Description: An array of items to choose from, used with
-
validate
- Description: Validation rules for the input. Can be an array of rules or a custom function.
- Examples:
- Length validation:
"validate" => ["length" => [1, 200]]
- Custom function validation:
- Length validation:
-
error
- Description: Error message or function to display when validation fails.
- Examples:
- Static message:
"error" => "Input is required"
- Custom function error:
- Static message:
- confirm
- Description: Message to display upon user confirmation, used with
confirm
type prompts. - Example:
"confirm" => "Are you sure you want to continue?"
- Description: Message to display upon user confirmation, used with
Available Prompt Types
Summary
- text: Prompts for regular text input.
- password: Prompts for masked password input.
- toggle: Asks a yes/no question.
- select: Provides a list of options for selection.
- list: Prompts for a comma-separated list of values.
- continue: Continue with input dependent on previous value
- message: Displays a message (no input).
- confirm: Asks for confirmation before proceeding.
Details
-
text
- Description: Prompts the user for regular text input.
- Usage Example:
-
password
- Description: Prompts the user for password input, masking the characters as they are typed.
- Usage Example:
-
toggle
- Description: Asks the user a yes/no question.
- Usage Example:
-
select
- Description: Provides a list of options for the user to choose from.
- Usage Example:
-
list
- Description: Prompts the user for a comma-separated list of values.
- Usage Example:
-
continue
- Description: Continue with input dependent on previous value.
- Usage Example:
-
message
- Description: Displays a message to the user. This type does not collect input and will be excluded from the end result array.
- Usage Example:
- confirm
- Description: Asks the user for confirmation before proceeding. Can display a custom confirmation message.
- Usage Example:
These prompt types enable a variety of user interactions in a CLI environment, making it easy to collect and validate different kinds of input using the PHP Prompts library.
Command instance
You also execute some of the prompt above command directly with the Command class.
Progress Bar
The progress
method of the Command
class allows displaying a progress bar with customizable sleep intervals to indicate ongoing operations.
Validation and Error Handling
Each prompt can have validation rules and custom error messages. Validation can be defined using built-in rules (e.g., length, email) or custom functions. Errors can be specified as static messages or dynamic functions based on the error type.
Validation List
-
required
- Description: Checks if the value is not empty (e.g., not
""
,0
,NULL
). - Usage:
"required" => []
- Description: Checks if the value is not empty (e.g., not
-
length
- Description: Checks if the string length is between a specified start and end length.
- Usage:
"length" => [1, 200]
-
email
- Description: Validates email addresses.
- Usage:
"email" => []
-
number
- Description: Checks if the value is numeric.
- Usage:
"number" => []
-
min
- Description: Checks if the value is greater than or equal to a specified minimum.
- Usage:
"min" => [10]
-
max
- Description: Checks if the value is less than or equal to a specified maximum.
- Usage:
"max" => [100]
-
url
- Description: Checks if the value is a valid URL (http|https is required).
- Usage:
"url" => []
-
phone
- Description: Validates phone numbers.
- Usage:
"phone" => []
-
date
- Description: Checks if the value is a valid date with the specified format.
- Usage:
"date" => ["Y-m-d"]
-
dateTime
- Description: Checks if the value is a valid date and time with the specified format.
- Usage:
"dateTime" => ["Y-m-d H:i"]
-
bool
- Description: Checks if the value is a boolean.
- Usage:
"bool" => []
-
oneOf
- Description: Validates if one of the provided conditions is met.
- Usage:
"oneOf" => [["length", [1, 200]], "email"]
-
allOf
- Description: Validates if all of the provided conditions are met.
- Usage:
"allOf" => [["length", [1, 200]], "email"]
-
float
- Description: Checks if the value is a float.
- Usage:
"float" => []
-
int
- Description: Checks if the value is an integer.
- Usage:
"int" => []
-
positive
- Description: Checks if the value is a positive number.
- Usage:
"positive" => []
-
negative
- Description: Checks if the value is a negative number.
- Usage:
"negative" => []
-
validVersion
- Description: Checks if the value is a valid version number.
- Usage:
"validVersion" => [true]
-
versionCompare
- Description: Validates and compares if a version is equal/more/equalMore/less... e.g., than withVersion.
- Usage:
"versionCompare" => ["1.0.0", ">="]
-
zip
- Description: Validates ZIP codes within a specified length range.
- Usage:
"zip" => [5, 9]
-
hex
- Description: Checks if the value is a valid hex color code.
- Usage:
"hex" => []
-
age
- Description: Checks if the value represents an age equal to or greater than the specified minimum.
- Usage:
"age" => [18]
-
domain
- Description: Checks if the value is a valid domain.
- Usage:
"domain" => [true]
-
dns
- Description: Checks if the host/domain has a valid DNS record (A, AAAA, MX).
- Usage:
"dns" => []
-
matchDNS
- Description: Matches DNS records by searching for a specific type and value.
- Usage:
"matchDNS" => [DNS_A]
-
equal
- Description: Checks if the value is equal to a specified value.
- Usage:
"equal" => ["someValue"]
-
notEqual
- Description: Checks if the value is not equal to a specified value.
- Usage:
"notEqual" => ["someValue"]
-
string
- Description: Checks if the value is a string.
- Usage:
"string" => []
-
equalLength
- Description: Checks if the string length is equal to a specified length.
- Usage:
"equalLength" => [10]
-
lossyPassword
- Description: Validates password with allowed characters
[a-zA-Z\d$@$!%*?&]
and a minimum length. - Usage:
"lossyPassword" => [8]
- Description: Validates password with allowed characters
-
strictPassword
- Description: Validates strict password with specific character requirements and a minimum length.
- Usage:
"strictPassword" => [8]
-
pregMatch
- Description: Validates if the value matches a given regular expression pattern.
- Usage:
"pregMatch" => ["a-zA-Z"]
-
atoZ
- Description: Checks if the value consists of characters between
a-z
orA-Z
. - Usage:
"atoZ" => []
- Description: Checks if the value consists of characters between
-
lowerAtoZ
- Description: Checks if the value consists of lowercase characters between
a-z
. - Usage:
"lowerAtoZ" => []
- Description: Checks if the value consists of lowercase characters between
-
upperAtoZ
- Description: Checks if the value consists of uppercase characters between
A-Z
. - Usage:
"upperAtoZ" => []
- Description: Checks if the value consists of uppercase characters between
-
isArray
- Description: Checks if the value is an array.
- Usage:
"isArray" => []
-
isObject
- Description: Checks if the value is an object.
- Usage:
"isObject" => []
- boolVal
- Description: Checks if the value is a boolean-like value (e.g., "on", "yes", "1", "true").
- Usage:
"boolVal" => []
Usage Example
Here is an example of how to use the validation functions in the prompt library:
Conclusion
PHP Prompts provides a straightforward way to create interactive command-line interfaces in PHP. By defining prompts, validation rules, and error messages, developers can easily gather and validate user input in a CLI environment.
All versions of prompts with dependencies
maplephp/dto Version ^2.0
maplephp/http Version ^1.0
maplephp/validate Version ^1.0