PHP Prompts is an interactive, lightweight, and easy-to-use CLI (Command Line Interface) PHP library. It provides a way to create and manage user prompts in a command-line environment, enabling the collection of various types of user input with validation and feedback.
To install PHP Prompts, use Composer:
composer require maplephp/prompts
Below is an example demonstrating how to use the PHP Prompts library to create interactive prompts and validate user input.
-
Initialize
$prompt = new MaplePHP\Prompts\Prompt();
-
Set the Title and Description for the Prompts
This step is optional but provides context for the user.
$prompt->setTitle("Hello there!"); $prompt->setDescription("We need your contact information to stay in touch, thank you.");
-
Define the Prompts
Define the prompts and their respective settings, including type, message, validation rules, and error messages.
$prompt->set([ "name" => [ "type" => "text", "message" => "Full Name", "validate" => [ "length" => [1,200] ] ], "email" => [ "type" => "text", "message" => "Email", "validate" => [ "length" => [1,200], "email" => [] ], "error" => "The email address entered is not valid." ], "newsletter" => [ "type" => "toggle", "message" => "Receive newsletter?", ], "method" => [ "type" => "select", "message" => "Preferred contact method?", "items" => [ "email" => "Email", "phone" => "Phone", "text" => "Text", "mail" => "Mail" ], ], "phone" => [ "type" => "text", "message" => "Phone", "validate" => [ "length" => [1,30], "phone" => [] ] ], "confirm" => [ "type" => "confirm", "message" => "Do you wish to continue?" ] ]);
-
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.// Execute the prompt $prompt = $prompt->prompt(); // Print out the user inputs print_r($prompt);
-
type
- Description: Specifies the type of the prompt (e.g.,
text
,password
,toggle
,select
,list
,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:
"validate" => function($input) { return strlen($input) >= 3; }
- Length validation:
-
error
- Description: Error message or function to display when validation fails.
- Examples:
- Static message:
"error" => "Input is required"
- Custom function error:
"error" => function($errorType, $input, $row) { if ($errorType === "length") { return "Is required"; } return "Must be a number"; }
- 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
- 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.
- message: Displays a message (no input).
- confirm: Asks for confirmation before proceeding.
-
text
- Description: Prompts the user for regular text input.
- Usage Example:
"firstname" => [ "type" => "text", "message" => "First name" ]
-
password
- Description: Prompts the user for password input, masking the characters as they are typed.
- Usage Example:
"password" => [ "type" => "password", "message" => "Password" ]
-
toggle
- Description: Asks the user a yes/no question.
- Usage Example:
"ssl" => [ "type" => "toggle", "message" => "Do you want SSL?" ]
-
select
- Description: Provides a list of options for the user to choose from.
- Usage Example:
"select" => [ "type" => "select", "message" => "Select an item below", "items" => [ "Lorem 1", "Lorem 2", "Lorem 3" ] ]
-
list
- Description: Prompts the user for a comma-separated list of values.
- Usage Example:
"keyword" => [ "type" => "list", "message" => "Keywords" ]
-
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:
"message" => [ "type" => "message", "message" => "Lorem ipsum dolor" ]
-
confirm
- Description: Asks the user for confirmation before proceeding. Can display a custom confirmation message.
- Usage Example:
"confirm" => [ "type" => "confirm", "message" => "Do you wish to continue?" ]
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.
The progress
method of the Command
class allows displaying a progress bar with customizable sleep intervals to indicate ongoing operations.
$command = new MaplePHP\Prompts\Command();
$command->progress(1, 100, function($i, $length) {
return 20;
});
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.
-
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" => []
Here is an example of how to use the validation functions in the prompt library:
$inp->set([
"firstname" => [
"type" => "text",
"message" => "First name",
"validate" => [
"length" => [1, 200],
"required" => []
],
"error" => "Required"
],
"email" => [
"type" => "text",
"message" => "Email",
"validate" => [
"length" => [1, 200],
"email" => []
]
],
"age" => [
"type" => "text",
"message" => "Age",
"validate" => [
"number" => [],
"min" => [18]
]
]
]);
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.