PHP code example of maplephp / prompts
1. Go to this page and download the library: Download maplephp/prompts library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
maplephp / prompts example snippets
$prompt = new MaplePHP\Prompts\Prompt();
$prompt->setTitle("Hello there!");
$prompt->setDescription("We need your contact information to stay in touch, thank you.");
$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"
],
],
"contactByPhone" => [
"type" => "continue",
"items" => function($prevVal) {
if($prevVal === "phone") {
return [
"phone" => [
"type" => "text",
"message" => "Phone",
"validate" => [
"length" => [1,30],
"phone" => []
]
]
];
}
return false;
}
],
"confirm" => [
"type" => "confirm",
"message" => "Do you wish to continue?"
]
]);
// Execute the prompt
$prompt = $prompt->prompt();
// Print out the user inputs
print_r($prompt);
"validate" => function($input) {
return strlen($input) >= 3;
}
"error" => function($errorType, $input, $row) {
if ($errorType === "length") {
return "Is
"firstname" => [
"type" => "text",
"message" => "First name"
]
"password" => [
"type" => "password",
"message" => "Password"
]
"ssl" => [
"type" => "toggle",
"message" => "Do you want SSL?"
]
"select" => [
"type" => "select",
"message" => "Select an item below",
"items" => [
"Lorem 1",
"Lorem 2",
"Lorem 3"
]
]
"keyword" => [
"type" => "list",
"message" => "Keywords"
]
"continue" => [
"type" => "continue",
"items" => function($prevVal) {
if($prevVal === "phone") {
return [
"phone" => [
"type" => "text",
"message" => "Phone",
"validate" => [
"length" => [1,30],
"phone" => []
]
]
];
}
return false;
}
]
"message" => [
"type" => "message",
"message" => "Lorem ipsum dolor"
]
"confirm" => [
"type" => "confirm",
"message" => "Do you wish to continue?"
]
$command = new MaplePHP\Prompts\Command();
// Print messages and return input
$input = $command->message("Text field", true);
$input = $command->mask("Masked input (password)");
$input = $command->toggle("Toggle yes/no");
$input = $command->select("Choose a value", [
//...
]);
$input = $command->list("Comma seperated list");
$input = $command->confirm("Confirm");
// Print messages
$command->message("Print message");
$command->title("Print bolded message");
$command->statusMsg("Print blue status message");
$command->approve("Print green approve message");
$command->approve("Print red error message");
$command = new MaplePHP\Prompts\Command();
$command->progress(1, 100, function($i, $length) {
return 20;
});
$inp->set([
"firstname" => [
"type" => "text",
"message" => "First name",
"validate" => [
"length" => [1, 200],
"" => [1, 200],
"email" => []
]
],
"age" => [
"type" => "text",
"message" => "Age",
"validate" => [
"number" => [],
"min" => [18]
]
]
]);