-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from Setono/fix-address-validation-during-chec…
…kout Added: NL/DE address validation during checkout/address creation/update
- Loading branch information
Showing
12 changed files
with
266 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusQuickpayPlugin\Checker; | ||
|
||
use Sylius\Component\Addressing\Model\AddressInterface; | ||
use VIISON\AddressSplitter\AddressSplitter; | ||
use VIISON\AddressSplitter\Exceptions\SplittingException; | ||
use Webmozart\Assert\Assert; | ||
|
||
class StreetEligibilityChecker implements StreetEligibilityCheckerInterface | ||
{ | ||
public function isEligible(AddressInterface $address): bool | ||
{ | ||
$countryCode = $address->getCountryCode(); | ||
Assert::notNull($countryCode); | ||
|
||
$street = $address->getStreet(); | ||
if (null === $street) { | ||
return false; | ||
} | ||
|
||
try { | ||
switch (mb_strtoupper($countryCode)) { | ||
case 'DE': | ||
$splittedStreet = AddressSplitter::splitAddress($street); | ||
if ('' === $splittedStreet['houseNumber']) { | ||
return false; | ||
} | ||
|
||
break; | ||
case 'NL': | ||
$splittedStreet = AddressSplitter::splitAddress($street); | ||
if ('' === $splittedStreet['houseNumberParts']['base']) { | ||
return false; | ||
} | ||
|
||
if ('' === $splittedStreet['houseNumberParts']['extension']) { | ||
return false; | ||
} | ||
} | ||
} catch (SplittingException $e) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusQuickpayPlugin\Checker; | ||
|
||
use Sylius\Component\Addressing\Model\AddressInterface; | ||
|
||
interface StreetEligibilityCheckerInterface | ||
{ | ||
public function isEligible(AddressInterface $address): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusQuickpayPlugin\Validator; | ||
|
||
use Setono\SyliusQuickpayPlugin\Checker\StreetEligibilityCheckerInterface; | ||
use Setono\SyliusQuickpayPlugin\Validator\Constraints\AddressStreetEligibility; | ||
use Sylius\Component\Addressing\Model\AddressInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class AddressStreetEligibilityValidator extends ConstraintValidator | ||
{ | ||
/** @var StreetEligibilityCheckerInterface */ | ||
private $streetEligibilityChecker; | ||
|
||
public function __construct(StreetEligibilityCheckerInterface $streetEligibilityChecker) | ||
{ | ||
$this->streetEligibilityChecker = $streetEligibilityChecker; | ||
} | ||
|
||
public function validate($address, Constraint $constraint): void | ||
{ | ||
Assert::isInstanceOf($address, AddressInterface::class); | ||
Assert::isInstanceOf($constraint, AddressStreetEligibility::class); | ||
|
||
if (!$this->streetEligibilityChecker->isEligible($address)) { | ||
$this->context | ||
->buildViolation($constraint->message) | ||
->atPath('street') | ||
->addViolation() | ||
; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusQuickpayPlugin\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
final class AddressStreetEligibility extends Constraint | ||
{ | ||
/** @var string */ | ||
public $message = 'setono_sylius_quickpay.address.street_eligibility'; | ||
|
||
public function validatedBy(): string | ||
{ | ||
return 'setono_sylius_quickpay_address_street_eligibility_validator'; | ||
} | ||
|
||
public function getTargets(): string | ||
{ | ||
return self::CLASS_CONSTRAINT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd"> | ||
<class name="Sylius\Component\Addressing\Model\Address"> | ||
<constraint name="Setono\SyliusQuickpayPlugin\Validator\Constraints\AddressStreetEligibility"> | ||
<option name="message">setono_sylius_quickpay.address.street_eligibility</option> | ||
<option name="groups"> | ||
<value>sylius_shipping_address_update</value> | ||
<value>sylius_checkout_complete</value> | ||
<value>sylius</value> | ||
</option> | ||
</constraint> | ||
</class> | ||
</constraint-mapping> |