check for maximum address field lengths as per shipcloud UPS API spec
This commit is contained in:
parent
09598ca70e
commit
8acf49b14e
|
@ -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",
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace SmcAddressLatin\Core\Checkout\Cart\Error;
|
||||
|
||||
use Shopware\Core\Checkout\Cart\Error\Error;
|
||||
|
||||
class ShippingAddressLengthError extends Error
|
||||
{
|
||||
private const KEY = 'address-invalid-length';
|
||||
|
||||
private string $erroneousFieldName;
|
||||
private int $maxLength;
|
||||
|
||||
public function __construct(string $erroneousFieldName, int $maxLength)
|
||||
{
|
||||
$this->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 ];
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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%"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue