From 7d7d7aa2b6d444e3f5d299158ba431c4e686df59 Mon Sep 17 00:00:00 2001 From: Basil Baumgartner Date: Thu, 27 Oct 2022 11:56:59 +0200 Subject: [PATCH] feat: trimmed text option values and removed empty inputs --- src/EventListener/AddToCartListener.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/EventListener/AddToCartListener.php b/src/EventListener/AddToCartListener.php index 4b585f20..aa40cd72 100644 --- a/src/EventListener/AddToCartListener.php +++ b/src/EventListener/AddToCartListener.php @@ -122,6 +122,8 @@ public function getCustomerOptionsFromRequest(Request $request): array return []; } + $customerOptions = $addToCart['customer_options']; + // Date options need a little extra attention // We transform the date fields into a single date string foreach ($addToCart['customer_options'] as $code => $value) { @@ -129,11 +131,20 @@ public function getCustomerOptionsFromRequest(Request $request): array Assert::notNull($customerOption); switch ($customerOption->getType()) { + case CustomerOptionTypeEnum::TEXT: + // remove empty entries like " " + if (trim($value) == "") { + unset($customerOptions[$code]); + } else { + $customerOptions[$code] = trim($value); + } + + break; case CustomerOptionTypeEnum::DATE: - $day = $value['day']; - $month = $value['month']; - $year = $value['year']; - $addToCart['customer_options'][$code] = sprintf('%d-%d-%d', $year, $month, $day); + $day = $value['day']; + $month = $value['month']; + $year = $value['year']; + $customerOptions[$code] = sprintf('%d-%d-%d', $year, $month, $day); break; case CustomerOptionTypeEnum::DATETIME: @@ -146,12 +157,12 @@ public function getCustomerOptionsFromRequest(Request $request): array $hour = $time['hour'] ?? 0; $minute = $time['minute'] ?? 0; - $addToCart['customer_options'][$code] = sprintf('%d-%d-%d %d:%d', $year, $month, $day, $hour, $minute); + $customerOptions[$code] = sprintf('%d-%d-%d %d:%d', $year, $month, $day, $hour, $minute); break; } } - return $addToCart['customer_options']; + return $customerOptions; } }