Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions Checker/AddressUnderCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace Markup\AddressingBundle\Checker;

use Markup\Addressing\AddressInterface;

class AddressUnderCheck implements AddressInterface
{
/**
* @var string
*/
private $postalCode;

/**
* @var string
*/
private $country;

/**
* @param string $postalCode
* @param string $country
*/
public function __construct($postalCode, $country)
{
$this->postalCode = $postalCode;
$this->country = $country;
}

/**
* Gets a name for the recipient at this address. Returns null if one is not specified.
*
* @return string|null
**/
public function getRecipient()
{
return null;
}

/**
* Gets whether the address has a recipient defined.
*
* @return bool
**/
public function hasRecipient()
{
return false;// TODO: Implement hasRecipient() method.
}

/**
* Gets the numbered address line, counting from one. If there is no address line for provided number, return null.
*
* @param int $lineNumber
* @return string|null
**/
public function getStreetAddressLine($lineNumber)
{
return null;
}

/**
* Gets the address lines that are part of the street address - i.e. everything up until the postal town.
*
* @return array
**/
public function getStreetAddressLines()
{
return [];
}

/**
* Gets the locality for this address. This field is often referred to as a "town" or a "city".
*
* @return string
**/
public function getLocality()
{
return '';
}

/**
* Gets the region for this address. This field is often referred to as a "county", "state" or "province".
* If no region is indicated as part of the address information, returns an empty string.
*
* @return string
**/
public function getRegion()
{
return '';
}

/**
* Gets the postal code for this address.
* If no postal code is indicated as part of the address information, returns an empty string.
*
* @return string
**/
public function getPostalCode()
{
return $this->postalCode;
}

/**
* Gets the ISO-3166-2 (alpha-2) representation of the country indicated for this address.
* i.e. 'GB' for United Kingdom (*not* 'UK'), 'US' for United States.
*
* @return string
**/
public function getCountry()
{
return $this->country;
}
}
47 changes: 47 additions & 0 deletions Checker/PostalCodeChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Markup\AddressingBundle\Checker;

use Markup\Addressing\Canonicalizer\PostalCodeCanonicalizer;
use Symfony\Component\Validator\Validator\ValidatorInterface;

/**
* A checker service that can take a postal code and a country, and check whether the postal code is recognisable as one.
*/
class PostalCodeChecker
{
/**
* @var ValidatorInterface
*/
private $validator;

/**
* @param ValidatorInterface $validator
*/
public function __construct(ValidatorInterface $validator)
{
$this->validator = $validator;
}

/**
* @param string $postalCode
* @param string $country
* @return bool
*/
public function check($postalCode, $country)
{
$violationList = $this->validator->validate(
new AddressUnderCheck(
$this->canonicalize($postalCode, $country),
$country
)
);

return count($violationList) === 0;
}

private function canonicalize($postalCode, $country)
{
return (new PostalCodeCanonicalizer())->canonicalizeForCountry($postalCode, $country);
}
}
4 changes: 4 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,7 @@ services:
- '%markup_addressing.require_strict_regions%'
tags:
- { name: validator.constraint_validator, alias: addressing_region }
markup_addressing.postal_code_checker:
class: 'Markup\AddressingBundle\Checker\PostalCodeChecker'
arguments:
- '@validator'
7 changes: 7 additions & 0 deletions Resources/config/validation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Markup\AddressingBundle\Checker\AddressUnderCheck:
properties:
country:
- Regex:
pattern: '/[A-Z]{2}/'
constraints:
- Markup\AddressingBundle\Validator\PostalCodeValidator: ~
56 changes: 56 additions & 0 deletions Tests/Checker/PostalCodeCheckerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Markup\AddressingBundle\Tests\Checker;

use Markup\AddressingBundle\Checker\AddressUnderCheck;
use Markup\AddressingBundle\Checker\PostalCodeChecker;
use Mockery as m;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class PostalCodeCheckerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ValidatorInterface
*/
private $validator;

/**
* @var PostalCodeChecker
*/
private $checker;

protected function setUp()
{
$this->validator = m::mock(ValidatorInterface::class);
$this->checker = new PostalCodeChecker($this->validator);
}

protected function tearDown()
{
m::close();
}

public function testPostalCodeCheckerTrueIfValidates()
{
$emptyViolationList = new ConstraintViolationList();
$this->validator
->shouldReceive('validate')
->with(m::type(AddressUnderCheck::class))
->once()
->andReturn($emptyViolationList);
$this->assertTrue($this->checker->check('sw1a1aa', 'GB'));
}

public function testPostalCodeCheckerFalseIfDoesNotValidate()
{
$nonEmptyViolationList = new ConstraintViolationList([m::mock(ConstraintViolationInterface::class)]);
$this->validator
->shouldReceive('validate')
->with(m::type(AddressUnderCheck::class))
->once()
->andReturn($nonEmptyViolationList);
$this->assertFalse($this->checker->check('123456', 'SE'));
}
}