Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions Prompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ public function add(string $name, array $data): self
* Prompt line, directly prompt line without data
*
* @param array $row
* @return string|array|bool|int
* @return mixed
* @throws PromptException
* @throws Exception
*/
public function promptLine(array $row): string|array|bool|int
public function promptLine(array $row): mixed
{
$input = false;
$default = $row['default'] ?? "";
Expand All @@ -113,7 +113,7 @@ public function promptLine(array $row): string|array|bool|int
$rowType = $row['type'] ?? "text";
$confirm = $row['confirm'] ?? false;

if (isset($default) && is_string($default)) {
if (isset($default) && is_string($default) && strlen($default)) {
$message .= " ($default)";
}

Expand Down Expand Up @@ -177,10 +177,7 @@ public function promptLine(array $row): string|array|bool|int
if ($this->isEmpty($input)) {
$input = $default;
}

if (!(is_array($input) || is_string($input))) {
throw new InvalidArgumentException("The input item is wrong input data type", 1);
}

if (!(is_array($validate) || is_callable($validate))) {
throw new InvalidArgumentException("The validate item is wrong input data type", 1);
}
Expand Down Expand Up @@ -270,15 +267,14 @@ protected function isEmpty(mixed $input): bool
* Validate a set of items
*
* @param array|callable $validate
* @param string|array $input
* @param array|null $error
* @param-out null|string $error Error message or method that caused validation failure.
* @param mixed $input
* @param string|null $error
* @return bool
* @throws ErrorException
*/
protected function validateItems(array|callable $validate, string|array $input, ?array &$error = []): bool
protected function validateItems(array|callable $validate, mixed $input, ?string &$error = ""): bool
{
$input = is_string($input) ? [$input] : $input;
$input = (!is_array($input)) ? [$input] : $input;
foreach ($input as $value) {
if (is_callable($validate)) {
$isValid = $validate($value);
Expand Down
15 changes: 2 additions & 13 deletions examples/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,7 @@
use MaplePHP\Prompts\Prompt;
use MaplePHP\Prompts\Command;
use MaplePHP\Prompts\SttyWrapper;
// Define ANSI escape sequences for colors
$limeGreenBackground = "\033[102m"; // Bright green background
$grayText = "\e[90m"; // Bright black (gray) text
$reset = "\e[0m"; // Reset colors

// Custom message with padding
$message = " PASS ";

// Display the styled output in lime green with gray text
echo "{$limeGreenBackground}{$grayText}{$message}{$reset} Tests\\Unit\\ExampleTest\n";

die;
$command = new Command();
$inp = new Prompt();

Expand Down Expand Up @@ -86,7 +75,7 @@
}
],
"message" => [
"type" => "message", // Will be exclude form the end result array!
"type" => "message", // Will be excluded form the end result array!
"message" => "Lorem ipsum dolor",
],
"select" => [
Expand Down Expand Up @@ -123,7 +112,7 @@
"confirm" => [
"type" => "confirm",
"message" => "Do you wish to continue?",
"confirm" => "Continuing.."
"confirm" => "Continuing..."
]
]);

Expand Down