initial checking of first attempt

This commit is contained in:
Harald Welte 2023-12-02 17:29:27 +01:00
commit 8b19e5480f
5 changed files with 156 additions and 0 deletions

31
composer.json Normal file
View File

@ -0,0 +1,31 @@
{
"name": "sysmocom/shopware6-address-latin",
"description": "latin character address enforcement",
"version": "0.0.1",
"type": "shopware-platform-plugin",
"license": "AGPL-3.0-or-later",
"authors": [
{
"name": "Harald Welte",
"email": "hwelte@sysmocom.de",
"role": "main developer"
}
],
"minimum-stability": "dev",
"repositories": [
],
"require": {
},
"autoload": {
"psr-4": {
"SmcAddressLatin\\": "src/"
}
},
"extra": {
"shopware-plugin-class": "SmcAddressLatin\\SmcAddressLatin",
"label": {
"de-DE": "shipcloud latin character address validation",
"en-GB": "shipcloud latin character address validation"
}
}
}

View File

@ -0,0 +1,41 @@
namespace SmcAddressLatin\Core\Checkout\Cart\Error;
use Shopware\Core\Checkout\Cart\Error\Error;
class ShippingCardInvalidCharactersError extends Error
{
private const KEY = 'address-invalid-characters';
private string $erroneousFieldName;
public function __construct(string $erroneousFieldName)
{
$this->erroneousFieldName = $erroneousFieldName;
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 ];
}
}

View File

@ -0,0 +1,64 @@
// <plugin root>/src/Core/Checkout/Cart/SmcCartValidator.php
<?php declare(strict_types=1);
namespace SmcAddressLatin\Core\Checkout\Cart;
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;
class SmcCartValidator implements CartValidatorInterface
{
public function validate(Cart $cart, ErrorCollection $errorCollection, SalesChannelContext $salesChannelContext): void
{
$validateShipping = $cart->getLineItems()->count() === 0
|| $cart->getLineItems()->hasLineItemWithState(State::IS_PHYSICAL);
if (!$validateShipping) {
return;
}
private static function contains_only_latin(string $input): bool {
if ($input === null) {
return true;
}
if (preg_match('/[^\p{Common}\p{Latin}]/u', $input)) {
return true;
}
return false;
}
$ship_addr = $context->getShippingLocation()->getAddress();
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'));
}
if (!$this->contains_only_latin($ship_addr->getSalutation()) {
$errors->add(new ShippingAddressInvalidCharactersError('Salutation'));
}
}
}

View File

@ -0,0 +1,12 @@
// <plugin root>/src/Resources/config/services.xml
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="SmcAddressLatin\Core\Checkout\Cart\SmcCartValidator">
<tag name="shopware.cart.validator"/>
</service>
</services>
</container>

View File

@ -0,0 +1,8 @@
{
"checkout": {
"address-invalid-characters": "Shipping Address contains invalid characters."
},
"error": {
"address-invalid-characters": "Shipping Address contains invalid characters."
}
}