Skip to content

Commit cb58996

Browse files
authored
Merge pull request #16 from danitome24/8-postalcode-vo
8 postalcode vo
2 parents f9c2c6a + 1fa853d commit cb58996

File tree

5 files changed

+278
-0
lines changed

5 files changed

+278
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* This software was built by:
4+
* Daniel Tomé Fernández <danieltomefer@gmail.com>
5+
* GitHub: danitome24
6+
*/
7+
8+
namespace PhpValueObject\Geography;
9+
10+
use PhpValueObject\Geography\Exception\InvalidAddressNumberException;
11+
use PhpValueObject\ValueObject;
12+
13+
final class Address implements ValueObject
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private $street;
19+
20+
/**
21+
* @var string
22+
*/
23+
private $city;
24+
25+
/**
26+
* @var int
27+
*/
28+
private $number;
29+
30+
/**
31+
* Address constructor.
32+
* @param string $street
33+
* @param string $city
34+
* @param int $number
35+
*/
36+
private function __construct(string $street, string $city, int $number)
37+
{
38+
$this->street = $street;
39+
$this->city = $city;
40+
$this->changeNumber($number);
41+
}
42+
43+
/**
44+
* @param string $street
45+
* @param string $city
46+
* @param int $number
47+
* @return static
48+
*/
49+
public static function build(string $street, string $city, int $number)
50+
{
51+
return new static($street, $city, $number);
52+
}
53+
54+
/**
55+
* @return string
56+
*/
57+
public function street(): string
58+
{
59+
return $this->street;
60+
}
61+
62+
/**
63+
* @return string
64+
*/
65+
public function city(): string
66+
{
67+
return $this->city;
68+
}
69+
70+
/**
71+
* @return int
72+
*/
73+
public function number(): int
74+
{
75+
return $this->number;
76+
}
77+
78+
/**
79+
* Compare a value object with another one.
80+
*
81+
* @param static|ValueObject $valueObjectToCompare
82+
* @return bool
83+
*/
84+
public function equals(ValueObject $valueObjectToCompare): bool
85+
{
86+
return ($this->street() === $valueObjectToCompare->street()
87+
&& $this->city() === $valueObjectToCompare->city()
88+
&& $this->number() === $valueObjectToCompare->number());
89+
}
90+
91+
/**
92+
* @param int $number
93+
* @throws \PhpValueObject\Geography\Exception\InvalidAddressNumberException
94+
*/
95+
private function changeNumber(int $number)
96+
{
97+
$this->checkIsValidNumber($number);
98+
$this->number = $number;
99+
}
100+
101+
/**
102+
* Check if address number is valid
103+
*
104+
* @param int $number
105+
* @throws \PhpValueObject\Geography\Exception\InvalidAddressNumberException
106+
*/
107+
private function checkIsValidNumber(int $number): void
108+
{
109+
if ($number <= 0) {
110+
throw new InvalidAddressNumberException('Address number ' . $number . ' is not valid');
111+
}
112+
}
113+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* This software was built by:
4+
* Daniel Tomé Fernández <danieltomefer@gmail.com>
5+
* GitHub: danitome24
6+
*/
7+
8+
namespace PhpValueObject\Geography\Exception;
9+
10+
use Throwable;
11+
12+
class InvalidAddressNumberException extends \Exception
13+
{
14+
public function __construct($message = "", $code = 0, Throwable $previous = null)
15+
{
16+
parent::__construct($message, $code, $previous);
17+
}
18+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* This software was built by:
4+
* Daniel Tomé Fernández <danieltomefer@gmail.com>
5+
* GitHub: danitome24
6+
*/
7+
8+
namespace PhpValueObject\Geography;
9+
10+
use PhpValueObject\ValueObject;
11+
12+
final class PostalCode implements ValueObject
13+
{
14+
/**
15+
* @var string
16+
*/
17+
private $code;
18+
19+
/**
20+
* PostalCode constructor.
21+
* @param string $code
22+
*/
23+
private function __construct(string $code)
24+
{
25+
$this->code = $code;
26+
}
27+
28+
/**
29+
* @param string $code
30+
* @return static
31+
*/
32+
public static function fromCode(string $code)
33+
{
34+
return new static($code);
35+
}
36+
37+
/**
38+
* @return string
39+
*/
40+
public function code(): string
41+
{
42+
return $this->code;
43+
}
44+
45+
/**
46+
* Compare a value object with another one.
47+
*
48+
* @param static|ValueObject $valueObjectToCompare
49+
* @return bool
50+
*/
51+
public function equals(ValueObject $valueObjectToCompare): bool
52+
{
53+
return ($this->code() === $valueObjectToCompare->code());
54+
}
55+
}

tests/Geography/AddressTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* This software was built by:
4+
* Daniel Tomé Fernández <danieltomefer@gmail.com>
5+
* GitHub: danitome24
6+
*/
7+
8+
namespace Geography;
9+
10+
use PhpValueObject\Geography\Address;
11+
use PHPUnit\Framework\TestCase;
12+
use PhpValueObject\Geography\Exception\InvalidAddressNumberException;
13+
14+
class AddressTest extends TestCase
15+
{
16+
public function testAddressInstanceIsBuild()
17+
{
18+
$street = 'c/pastelon';
19+
$city = 'Lerico';
20+
$number = 22;
21+
22+
$address = Address::build($street, $city, $number);
23+
$this->assertEquals($street, $address->street());
24+
$this->assertEquals($city, $address->city());
25+
$this->assertEquals($number, $address->number());
26+
}
27+
28+
public function testNumberIsInvalid()
29+
{
30+
$this->expectException(InvalidAddressNumberException::class);
31+
Address::build('Av. perico', 'San Fiz do Seo', -3);
32+
}
33+
34+
/**
35+
* @dataProvider addresses
36+
* @param Address $address
37+
* @param Address $addressToCompare
38+
* @param bool $isEquals
39+
*/
40+
public function testEqualsMethod(Address $address, Address $addressToCompare, bool $isEquals)
41+
{
42+
$this->assertEquals($isEquals, $address->equals($addressToCompare));
43+
}
44+
45+
public function addresses()
46+
{
47+
return [
48+
[Address::build('a', 'b', 22), Address::build('a', 'b', 22), true],
49+
[Address::build('a', 'b', 22), Address::build('a2', 'b2', 22), false]
50+
];
51+
}
52+
}

tests/Geography/PostalCodeTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* This software was built by:
4+
* Daniel Tomé Fernández <danieltomefer@gmail.com>
5+
* GitHub: danitome24
6+
*/
7+
8+
namespace Geography;
9+
10+
use PhpValueObject\Geography\PostalCode;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class PostalCodeTest extends TestCase
14+
{
15+
public function testPostalCodeIsBuilt()
16+
{
17+
$pc = 'CE-1234';
18+
$postalCode = PostalCode::fromCode('CE-1234');
19+
$this->assertEquals($pc, $postalCode->code());
20+
}
21+
22+
/**
23+
* @dataProvider postalCodes
24+
* @param PostalCode $postalCode
25+
* @param PostalCode $postalCodeToCompare
26+
* @param bool $isEquals
27+
*/
28+
public function testEqualsMethod(PostalCode $postalCode, PostalCode $postalCodeToCompare, bool $isEquals)
29+
{
30+
$this->assertEquals($isEquals, $postalCode->equals($postalCodeToCompare));
31+
}
32+
33+
public function postalCodes()
34+
{
35+
return [
36+
[PostalCode::fromCode(1231), PostalCode::fromCode('CE-123'), false],
37+
[PostalCode::fromCode('CE-123'), PostalCode::fromCode('CE-123'), true]
38+
];
39+
}
40+
}

0 commit comments

Comments
 (0)