Skip to content

8 postalcode vo #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 16, 2017
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
113 changes: 113 additions & 0 deletions src/PhpValueObject/Geography/Address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* This software was built by:
* Daniel Tomé Fernández <danieltomefer@gmail.com>
* GitHub: danitome24
*/

namespace PhpValueObject\Geography;

use PhpValueObject\Geography\Exception\InvalidAddressNumberException;
use PhpValueObject\ValueObject;

final class Address implements ValueObject
{
/**
* @var string
*/
private $street;

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

/**
* @var int
*/
private $number;

/**
* Address constructor.
* @param string $street
* @param string $city
* @param int $number
*/
private function __construct(string $street, string $city, int $number)
{
$this->street = $street;
$this->city = $city;
$this->changeNumber($number);
}

/**
* @param string $street
* @param string $city
* @param int $number
* @return static
*/
public static function build(string $street, string $city, int $number)
{
return new static($street, $city, $number);
}

/**
* @return string
*/
public function street(): string
{
return $this->street;
}

/**
* @return string
*/
public function city(): string
{
return $this->city;
}

/**
* @return int
*/
public function number(): int
{
return $this->number;
}

/**
* Compare a value object with another one.
*
* @param static|ValueObject $valueObjectToCompare
* @return bool
*/
public function equals(ValueObject $valueObjectToCompare): bool
{
return ($this->street() === $valueObjectToCompare->street()
&& $this->city() === $valueObjectToCompare->city()
&& $this->number() === $valueObjectToCompare->number());
}

/**
* @param int $number
* @throws \PhpValueObject\Geography\Exception\InvalidAddressNumberException
*/
private function changeNumber(int $number)
{
$this->checkIsValidNumber($number);
$this->number = $number;
}

/**
* Check if address number is valid
*
* @param int $number
* @throws \PhpValueObject\Geography\Exception\InvalidAddressNumberException
*/
private function checkIsValidNumber(int $number): void
{
if ($number <= 0) {
throw new InvalidAddressNumberException('Address number ' . $number . ' is not valid');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* This software was built by:
* Daniel Tomé Fernández <danieltomefer@gmail.com>
* GitHub: danitome24
*/

namespace PhpValueObject\Geography\Exception;

use Throwable;

class InvalidAddressNumberException extends \Exception
{
public function __construct($message = "", $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
55 changes: 55 additions & 0 deletions src/PhpValueObject/Geography/PostalCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* This software was built by:
* Daniel Tomé Fernández <danieltomefer@gmail.com>
* GitHub: danitome24
*/

namespace PhpValueObject\Geography;

use PhpValueObject\ValueObject;

final class PostalCode implements ValueObject
{
/**
* @var string
*/
private $code;

/**
* PostalCode constructor.
* @param string $code
*/
private function __construct(string $code)
{
$this->code = $code;
}

/**
* @param string $code
* @return static
*/
public static function fromCode(string $code)
{
return new static($code);
}

/**
* @return string
*/
public function code(): string
{
return $this->code;
}

/**
* Compare a value object with another one.
*
* @param static|ValueObject $valueObjectToCompare
* @return bool
*/
public function equals(ValueObject $valueObjectToCompare): bool
{
return ($this->code() === $valueObjectToCompare->code());
}
}
52 changes: 52 additions & 0 deletions tests/Geography/AddressTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* This software was built by:
* Daniel Tomé Fernández <danieltomefer@gmail.com>
* GitHub: danitome24
*/

namespace Geography;

use PhpValueObject\Geography\Address;
use PHPUnit\Framework\TestCase;
use PhpValueObject\Geography\Exception\InvalidAddressNumberException;

class AddressTest extends TestCase
{
public function testAddressInstanceIsBuild()
{
$street = 'c/pastelon';
$city = 'Lerico';
$number = 22;

$address = Address::build($street, $city, $number);
$this->assertEquals($street, $address->street());
$this->assertEquals($city, $address->city());
$this->assertEquals($number, $address->number());
}

public function testNumberIsInvalid()
{
$this->expectException(InvalidAddressNumberException::class);
Address::build('Av. perico', 'San Fiz do Seo', -3);
}

/**
* @dataProvider addresses
* @param Address $address
* @param Address $addressToCompare
* @param bool $isEquals
*/
public function testEqualsMethod(Address $address, Address $addressToCompare, bool $isEquals)
{
$this->assertEquals($isEquals, $address->equals($addressToCompare));
}

public function addresses()
{
return [
[Address::build('a', 'b', 22), Address::build('a', 'b', 22), true],
[Address::build('a', 'b', 22), Address::build('a2', 'b2', 22), false]
];
}
}
40 changes: 40 additions & 0 deletions tests/Geography/PostalCodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* This software was built by:
* Daniel Tomé Fernández <danieltomefer@gmail.com>
* GitHub: danitome24
*/

namespace Geography;

use PhpValueObject\Geography\PostalCode;
use PHPUnit\Framework\TestCase;

class PostalCodeTest extends TestCase
{
public function testPostalCodeIsBuilt()
{
$pc = 'CE-1234';
$postalCode = PostalCode::fromCode('CE-1234');
$this->assertEquals($pc, $postalCode->code());
}

/**
* @dataProvider postalCodes
* @param PostalCode $postalCode
* @param PostalCode $postalCodeToCompare
* @param bool $isEquals
*/
public function testEqualsMethod(PostalCode $postalCode, PostalCode $postalCodeToCompare, bool $isEquals)
{
$this->assertEquals($isEquals, $postalCode->equals($postalCodeToCompare));
}

public function postalCodes()
{
return [
[PostalCode::fromCode(1231), PostalCode::fromCode('CE-123'), false],
[PostalCode::fromCode('CE-123'), PostalCode::fromCode('CE-123'), true]
];
}
}