diff --git a/composer.json b/composer.json index 6e7e81d..38cae8c 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "sysmocom/shopware6-address-latin", - "description": "latin character address enforcement", + "description": "latin character only address + address length enforcement", "version": "0.0.1", "type": "shopware-platform-plugin", "license": "AGPL-3.0-or-later", diff --git a/src/Core/Checkout/Cart/Error/ShippingAddressLengthError.php b/src/Core/Checkout/Cart/Error/ShippingAddressLengthError.php new file mode 100644 index 0000000..16c4ccc --- /dev/null +++ b/src/Core/Checkout/Cart/Error/ShippingAddressLengthError.php @@ -0,0 +1,45 @@ +erroneousFieldName = $erroneousFieldName; + $this->maxLength = $maxLength; + parent::__construct(); + } + + public function getId(): string + { + return $this->erroneousFieldName; + } + + public function getMessageKey(): string + { + return self::KEY; + } + + public function getLevel(): int + { + return self::LEVEL_ERROR; + } + + public function blockOrder(): bool + { + return true; + } + + public function getParameters(): array + { + return [ 'erroneousFieldName' => $this->erroneousFieldName, 'maxLength' => $this->maxLength ]; + } +} diff --git a/src/Core/Checkout/Cart/SmcCartValidator.php b/src/Core/Checkout/Cart/SmcCartValidator.php index 1e848b6..b0932d1 100644 --- a/src/Core/Checkout/Cart/SmcCartValidator.php +++ b/src/Core/Checkout/Cart/SmcCartValidator.php @@ -8,6 +8,7 @@ use Shopware\Core\Checkout\Cart\CartValidatorInterface; use Shopware\Core\Checkout\Cart\Error\ErrorCollection; use Shopware\Core\System\SalesChannel\SalesChannelContext; use SmcAddressLatin\Core\Checkout\Cart\Error\ShippingAddressInvalidCharactersError; +use SmcAddressLatin\Core\Checkout\Cart\Error\ShippingAddressLengthError; class SmcCartValidator implements CartValidatorInterface { @@ -21,6 +22,16 @@ class SmcCartValidator implements CartValidatorInterface return false; } + private static function is_short_enough(?string $input, int $maxlen): bool { + if ($input === null) { + return true; + } + if (strlen($input) <= $maxlen) { + return true; + } + return false; + } + public function validate(Cart $cart, ErrorCollection $errors, SalesChannelContext $context): void { $validateShipping = $cart->getLineItems()->count() === 0 @@ -60,5 +71,32 @@ class SmcCartValidator implements CartValidatorInterface if (!$this->contains_only_latin($ship_addr->getAdditionalAddressLine2())) { $errors->add(new ShippingAddressInvalidCharactersError('Additional Address Line 2')); } + + // https://developers.shipcloud.io/carriers/ups.html + if (!$this->is_short_enough($ship_addr->getCompany(), 35)) { + $errors->add(new ShippingAddressLengthError('Company', 35)); + } + if (!$this->is_short_enough($ship_addr->getStreet(), 35)) { + $errors->add(new ShippingAddressLengthError('Street', 35)); + } + if (!$this->is_short_enough($ship_addr->getAdditionalAddressLine1(), 35)) { + $errors->add(new ShippingAddressLengthError('Additional Address Line 1', 35)); + } + if (!$this->is_short_enough($ship_addr->getAdditionalAddressLine2(), 35)) { + $errors->add(new ShippingAddressLengthError('Additional Address Line 2', 35)); + } + if (!$this->is_short_enough($ship_addr->getCity(), 30)) { + $errors->add(new ShippingAddressLengthError('City', 30)); + } + if (!$this->is_short_enough($ship_addr->getZipcode(), 9)) { + $errors->add(new ShippingAddressLengthError('Zip Code', 9)); + } + if (!$this->is_short_enough($ship_addr->getPhoneNumber(), 15)) { + $errors->add(new ShippingAddressLengthError('Phone Number', 15)); + } + $name = $ship_addr->getFirstName() . " " . $ship_addr->getLastName(); + if (!$this->is_short_enough($name, 35)) { + $errors->add(new ShippingAddressLengthError('First and Last Name', 35)); + } } } diff --git a/src/Resources/snippet/en_GB/error.en-GB.json b/src/Resources/snippet/en_GB/error.en-GB.json index 2d0590e..46ac5a5 100644 --- a/src/Resources/snippet/en_GB/error.en-GB.json +++ b/src/Resources/snippet/en_GB/error.en-GB.json @@ -1,8 +1,10 @@ { "checkout": { - "address-invalid-characters": "Shipping Address (%erroneousFieldName%) contains invalid characters." + "address-invalid-characters": "Shipping Address (%erroneousFieldName%) contains invalid characters.", + "address-invalid-length": "Shipping Address (%erroneousFieldName%) contains too many characters. Maximum Permitted: %maxLength%" }, "error": { - "address-invalid-characters": "Shipping Address (%erroneousFieldName%) contains invalid characters." + "address-invalid-characters": "Shipping Address (%erroneousFieldName%) contains invalid characters.", + "address-invalid-length": "Shipping Address (%erroneousFieldName%) contains too many characters. Maximum Permitted: %maxLength%" } }