From 55624f9b67c28b515df89f9741f039edf2da47d1 Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Thu, 26 Dec 2024 12:40:39 +0300 Subject: [PATCH] select builder prompt add --- src/builders/SelectBuilder.php | 22 ++++++++++-- src/traits/CreateOption.php | 62 ++++++++++++++++++++++++++++------ 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/src/builders/SelectBuilder.php b/src/builders/SelectBuilder.php index 2f9a475..596af6d 100755 --- a/src/builders/SelectBuilder.php +++ b/src/builders/SelectBuilder.php @@ -8,11 +8,29 @@ use itguild\forms\inputs\Select; class SelectBuilder { - public static function build(string $name, array $params = []) +// public static function build(string $name, array $params = []) +// { +// $value = $params['value'] ?? null; +// unset($params['value']); +// $options = $params['options'] ?? []; +// unset($params['options']); +// +// return new Select(name: $name, options: $options, value: $value, paramsArray: $params); +// } + + public static function build(string $name, array $params = []): Select { $value = $params['value'] ?? null; unset($params['value']); - $options = $params['options'] ?? []; + if (isset($params['prompt'])) { + $options['prompt'] = $params['prompt'] ?? null; + foreach ($params['options'] as $key => $val) { + $options[$key] = $val; + } + } else { + $options = $params['options'] ?? []; + } + unset($params['options']); return new Select(name: $name, options: $options, value: $value, paramsArray: $params); diff --git a/src/traits/CreateOption.php b/src/traits/CreateOption.php index d0b5190..21d7c3d 100755 --- a/src/traits/CreateOption.php +++ b/src/traits/CreateOption.php @@ -2,6 +2,8 @@ namespace itguild\forms\traits; +use itguild\forms\debug\Debug; + trait CreateOption { /** @@ -9,22 +11,62 @@ trait CreateOption * @param $value * @return string */ +// public function createOption(array $options, $value = null): string +// { +// $optionsString = ""; +// if (is_array($value)) { +// foreach ($options as $val => $title) { +// $selected = in_array($title, $value) ? "selected" : ""; +// $optionsString .= ""; +// } +// } else { +// foreach ($options as $val => $title) { +// if (is_numeric($value)) { +// $selected = (int)$value === $val ? "selected" : ""; +// } else { +// $selected = $value === $val ? "selected" : ""; +// } +// $optionsString .= ""; +// } +// } +// +// return $optionsString; +// } + public function createOption(array $options, $value = null): string { $optionsString = ""; - if (is_array($value)) { - foreach ($options as $val => $title) { - $selected = in_array($title, $value) ? "selected" : ""; - $optionsString .= ""; + if ($value) { + if (isset($options['prompt'])) { + $prompt = $options['prompt']; + $optionsString .= ""; + } + + if (is_array($value)) { + foreach ($options as $val => $title) { + if ($val === 'prompt') continue; + $selected = in_array($title, $value) ? "selected" : ""; + $optionsString .= ""; + } + } else { + foreach ($options as $val => $title) { + if ($val === 'prompt') continue; + if (is_numeric($value)) { + $selected = (int)$value === $val ? "selected" : ""; + } else { + $selected = $value === $val ? "selected" : ""; + } + $optionsString .= ""; + } } } else { + if (isset($options['prompt'])) { + $prompt = $options['prompt']; + $optionsString .= ""; + } foreach ($options as $val => $title) { - if (is_numeric($value)) { - $selected = (int)$value === $val ? "selected" : ""; - } else { - $selected = $value === $val ? "selected" : ""; - } - $optionsString .= ""; + if ($val === 'prompt') continue; + $optionsString .= ""; } }