From dc47c3526333546f1eceab36ae38670e0a5f0375 Mon Sep 17 00:00:00 2001 From: Abban Dunne Date: Wed, 17 Jun 2020 10:19:21 +0200 Subject: [PATCH] Add regex check on first and last name --- src/Validators/AddressValidator.php | 36 ++++++++++++++----- .../Unit/Validators/AddressValidatorTest.php | 33 +++++++++++------ 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/Validators/AddressValidator.php b/src/Validators/AddressValidator.php index a6f3ef5..db4ca42 100644 --- a/src/Validators/AddressValidator.php +++ b/src/Validators/AddressValidator.php @@ -14,6 +14,7 @@ class AddressValidator { private const VIOLATION_MISSING = 'missing'; + private const VIOLATION_PATTERN_MATCH = 'does-not-match-pattern'; private const VIOLATION_NOT_POSTCODE = 'not-postcode'; private const VIOLATION_WRONG_LENGTH = 'wrong-length'; @@ -40,9 +41,11 @@ class AddressValidator { ]; private $countriesPostcodePatterns; + private $addressPatterns; - public function __construct( array $countriesPostcodePatterns ) { + public function __construct( array $countriesPostcodePatterns, array $addressPatterns ) { $this->countriesPostcodePatterns = $countriesPostcodePatterns; + $this->addressPatterns = $addressPatterns; } public function validatePostalAddress( string $streetAddress, string $postalCode, string $city, string $countryCode ): ValidationResult { @@ -63,7 +66,7 @@ public function validatePostalAddress( string $streetAddress, string $postalCode } else { $postalCodeLengthViolation = $this->validateFieldLength( $postalCode, self::SOURCE_POSTAL_CODE ); if ( $postalCodeLengthViolation === null ) { - $violations[] = $this->validatePostalCode( '/^.+$/', $postalCode ); + $violations[] = $this->validatePostalCode( $this->addressPatterns['postcode'], $postalCode ); } else { $violations[] = $postalCodeLengthViolation; } @@ -93,12 +96,17 @@ public function validatePostalAddress( string $streetAddress, string $postalCode } private function validatePostalCode( string $pattern, string $postalCode ): ?ConstraintViolation { - if ( !preg_match( $pattern, $postalCode ) ) { - return new ConstraintViolation( - $postalCode, - self::VIOLATION_NOT_POSTCODE, - self::SOURCE_POSTAL_CODE - ); + return $this->validateMatchPattern( + $pattern, + $postalCode, + self::VIOLATION_NOT_POSTCODE, + self::SOURCE_POSTAL_CODE + ); + } + + private function validateMatchPattern( string $pattern, string $value, string $messageIdentifier, string $source ): ?ConstraintViolation { + if ( !preg_match( $pattern, $value ) ) { + return new ConstraintViolation( $value, $messageIdentifier, $source ); } return null; } @@ -126,6 +134,12 @@ public function validatePersonName( string $salutation, string $title, string $f ); } else { $violations[] = $this->validateFieldLength( $firstname, self::SOURCE_FIRST_NAME ); + $violations[] = $this->validateMatchPattern( + $this->addressPatterns['firstName'], + $firstname, + self::VIOLATION_PATTERN_MATCH, + self::SOURCE_FIRST_NAME + ); } if ( $lastname === '' ) { @@ -136,6 +150,12 @@ public function validatePersonName( string $salutation, string $title, string $f ); } else { $violations[] = $this->validateFieldLength( $lastname, self::SOURCE_LAST_NAME ); + $violations[] = $this->validateMatchPattern( + $this->addressPatterns['lastName'], + $lastname, + self::VIOLATION_PATTERN_MATCH, + self::SOURCE_LAST_NAME + ); } return new ValidationResult( ...array_filter( $violations ) ); } diff --git a/tests/Unit/Validators/AddressValidatorTest.php b/tests/Unit/Validators/AddressValidatorTest.php index 8279eff..2f04987 100644 --- a/tests/Unit/Validators/AddressValidatorTest.php +++ b/tests/Unit/Validators/AddressValidatorTest.php @@ -21,26 +21,32 @@ class AddressValidatorTest extends \PHPUnit\Framework\TestCase { 'LU' => '/^[0-9]{4}$/', ]; + private const ADDRESS_PATTERNS = [ + 'firstName' => "/^[A-Za-z\u00C0-\u00D6\u00D8-\u00f6\u00f8-\u00ff\\s\\-\\.\\']+$/", + 'lastName' => "/^[A-Za-z\u00C0-\u00D6\u00D8-\u00f6\u00f8-\u00ff\\s\\-\\.\\']+$/", + 'postcode' => '/^.+$/', + ]; + public function testGivenValidPostalAddress_noViolationsAreReturned(): void { - $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS ); + $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS, self::ADDRESS_PATTERNS ); $validationResult = $validator->validatePostalAddress( 'Test 1234', '12345', 'Test City', 'Germany' ); $this->assertTrue( $validationResult->isSuccessful() ); } public function testGivenValidPersonName_noViolationsAreReturned(): void { - $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS ); + $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS, self::ADDRESS_PATTERNS ); $validationResult = $validator->validatePersonName( 'Herr', 'Prof. Dr.', 'Tester', 'Testing' ); $this->assertTrue( $validationResult->isSuccessful() ); } public function testGivenValidCompany_noViolationsAreReturned(): void { - $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS ); + $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS, self::ADDRESS_PATTERNS ); $validationResult = $validator->validateCompanyName( 'Test Company GmbH & Co. KG' ); $this->assertTrue( $validationResult->isSuccessful() ); } public function testGivenTooLongPostalValues_correctViolationsAreReturned(): void { - $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS ); + $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS, self::ADDRESS_PATTERNS ); $validationResult = $validator->validatePostalAddress( str_repeat( 'a', 101 ), str_repeat( '1', 17 ), @@ -56,7 +62,7 @@ public function testGivenTooLongPostalValues_correctViolationsAreReturned(): voi } public function testGivenTooLongNameValues_correctViolationsAreReturned(): void { - $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS ); + $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS, self::ADDRESS_PATTERNS ); $validationResult = $validator->validatePersonName( str_repeat( 'a', 17 ), str_repeat( 'a', 17 ), @@ -72,7 +78,7 @@ public function testGivenTooLongNameValues_correctViolationsAreReturned(): void } public function testGivenTooLongCompanyValues_correctViolationsAreReturned(): void { - $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS ); + $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS, self::ADDRESS_PATTERNS ); $validationResult = $validator->validateCompanyName( str_repeat( 'a', 101 ) ); @@ -82,7 +88,7 @@ public function testGivenTooLongCompanyValues_correctViolationsAreReturned(): vo } public function testGivenEmptyPostalValues_correctViolationsAreReturned(): void { - $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS ); + $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS, self::ADDRESS_PATTERNS ); $validationResult = $validator->validatePostalAddress( '', '', @@ -99,7 +105,7 @@ public function testGivenEmptyPostalValues_correctViolationsAreReturned(): void } public function testGivenEmptyNameValues_correctViolationsAreReturned(): void { - $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS ); + $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS, self::ADDRESS_PATTERNS ); $validationResult = $validator->validatePersonName( '', '', @@ -115,7 +121,7 @@ public function testGivenEmptyNameValues_correctViolationsAreReturned(): void { } public function testGivenEmptyCompanyValues_correctViolationsAreReturned(): void { - $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS ); + $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS, self::ADDRESS_PATTERNS ); $validationResult = $validator->validateCompanyName( '' ); $this->assertFalse( $validationResult->isSuccessful() ); $this->assertCount( 1, $validationResult->getViolations() ); @@ -123,8 +129,15 @@ public function testGivenEmptyCompanyValues_correctViolationsAreReturned(): void } public function testGivenBadPostcodeForCountry_correctViolationsAreReturned(): void { - $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS ); + $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS, self::ADDRESS_PATTERNS ); $validationResult = $validator->validatePostalAddress( 'Test 1234', '123', 'Test City', 'DE' ); $this->assertSame( 'postcode', $validationResult->getViolations()[0]->getSource() ); } + + public function testGivenBadFirstAndLastName_correctViolationsAreReturned(): void { + $validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS, self::ADDRESS_PATTERNS ); + $validationResult = $validator->validatePersonName( 'Herr', '', '£$%^&*()', '£$%^&*()' ); + $this->assertSame( 'firstName', $validationResult->getViolations()[0]->getSource() ); + $this->assertSame( 'lastName', $validationResult->getViolations()[1]->getSource() ); + } }