SmcAddressLatin/src/Core/Checkout/Cart/SmcCartValidator.php

103 lines
4.3 KiB
PHP

<?php declare(strict_types=1);
namespace SmcAddressLatin\Core\Checkout\Cart;
use Shopware\Core\Content\Product\State;
use Shopware\Core\Checkout\Cart\Cart;
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
{
private static function contains_only_latin(?string $input): bool {
if ($input === null) {
return true;
}
if (preg_match('/[^\p{Common}\p{Latin}]/u', $input) === 0) {
return true;
}
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
|| $cart->getLineItems()->hasLineItemWithState(State::IS_PHYSICAL);
if (!$validateShipping) {
return;
}
$ship_addr = $context->getShippingLocation()->getAddress();
if (!$ship_addr) {
return;
}
if (!$this->contains_only_latin($ship_addr->getFirstName())) {
$errors->add(new ShippingAddressInvalidCharactersError('First Name'));
}
if (!$this->contains_only_latin($ship_addr->getLastName())) {
$errors->add(new ShippingAddressInvalidCharactersError('Last Name'));
}
if (!$this->contains_only_latin($ship_addr->getCity())) {
$errors->add(new ShippingAddressInvalidCharactersError('City'));
}
if (!$this->contains_only_latin($ship_addr->getCompany())) {
$errors->add(new ShippingAddressInvalidCharactersError('Company'));
}
if (!$this->contains_only_latin($ship_addr->getDepartment())) {
$errors->add(new ShippingAddressInvalidCharactersError('Department'));
}
if (!$this->contains_only_latin($ship_addr->getTitle())) {
$errors->add(new ShippingAddressInvalidCharactersError('Title'));
}
if (!$this->contains_only_latin($ship_addr->getStreet())) {
$errors->add(new ShippingAddressInvalidCharactersError('Street'));
}
if (!$this->contains_only_latin($ship_addr->getAdditionalAddressLine1())) {
$errors->add(new ShippingAddressInvalidCharactersError('Additional Address Line 1'));
}
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));
}
}
}