From 6b7f4cc7cdc21b8e3076cc429a0975dc1acc05bf Mon Sep 17 00:00:00 2001 From: Daniel Ronkainen Date: Sun, 29 Sep 2024 16:30:35 +0200 Subject: [PATCH] Fix data type --- Prompt.php | 20 ++++++++------------ examples/example.php | 15 ++------------- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/Prompt.php b/Prompt.php index 026bb64..5d4a608 100755 --- a/Prompt.php +++ b/Prompt.php @@ -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'] ?? ""; @@ -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)"; } @@ -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); } @@ -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); diff --git a/examples/example.php b/examples/example.php index 5027d99..b2f673d 100755 --- a/examples/example.php +++ b/examples/example.php @@ -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(); @@ -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" => [ @@ -123,7 +112,7 @@ "confirm" => [ "type" => "confirm", "message" => "Do you wish to continue?", - "confirm" => "Continuing.." + "confirm" => "Continuing..." ] ]);