Skip to content

Commit 1fa853d

Browse files
committed
#8 - Added postalcode vo
1 parent 79c8686 commit 1fa853d

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
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/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)