Skip to content

Commit 84c46ae

Browse files
committed
#18 - Age vo
1 parent 1838462 commit 84c46ae

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

src/PhpValueObject/Person/Age.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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\Person;
9+
10+
use PhpValueObject\Person\Exception\InvalidAgeException;
11+
use PhpValueObject\ValueObject;
12+
13+
final class Age implements ValueObject
14+
{
15+
private const MIN_AGE = 0;
16+
private const MAX_AGE = 150;
17+
18+
/**
19+
* @var int
20+
*/
21+
private $age;
22+
23+
/**
24+
* Age constructor.
25+
* @param int $age
26+
*/
27+
private function __construct(int $age)
28+
{
29+
$this->checkIsValidAge($age);
30+
$this->age = $age;
31+
}
32+
33+
/**
34+
* @param int $age
35+
* @return static
36+
*/
37+
public static function from(int $age)
38+
{
39+
return new static($age);
40+
}
41+
42+
/**
43+
* @return int
44+
*/
45+
public function age(): int
46+
{
47+
return $this->age;
48+
}
49+
50+
/**
51+
* @param int $age
52+
* @throws \PhpValueObject\Person\Exception\InvalidAgeException
53+
*/
54+
private function checkIsValidAge(int $age)
55+
{
56+
if ($age < self::MIN_AGE || $age > self::MAX_AGE) {
57+
throw new InvalidAgeException('Age ' . $age . ' is invalid');
58+
}
59+
}
60+
61+
/**
62+
* Compare a value object with another one.
63+
*
64+
* @param static|ValueObject $valueObjectToCompare
65+
* @return bool
66+
*/
67+
public function equals(ValueObject $valueObjectToCompare): bool
68+
{
69+
return ($this->age() === $valueObjectToCompare->age());
70+
}
71+
}
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\Person\Exception;
9+
10+
use Throwable;
11+
12+
class InvalidAgeException extends \Exception
13+
{
14+
public function __construct($message = "", $code = 0, Throwable $previous = null)
15+
{
16+
parent::__construct($message, $code, $previous);
17+
}
18+
}

tests/Person/AgeTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* This software was built by:
4+
* Daniel Tomé Fernández <danieltomefer@gmail.com>
5+
* GitHub: danitome24
6+
*/
7+
8+
namespace Person;
9+
10+
use PhpValueObject\Person\Age;
11+
use PHPUnit\Framework\TestCase;
12+
use PhpValueObject\Person\Exception\InvalidAgeException;
13+
14+
class AgeTest extends TestCase
15+
{
16+
public function testAgeIsValid()
17+
{
18+
$this->assertEquals(5, Age::from(5)->age());
19+
}
20+
21+
public function testInvalidAgeExceptionIsThrown()
22+
{
23+
$this->expectException(InvalidAgeException::class);
24+
Age::from(-5);
25+
}
26+
27+
/**
28+
* @dataProvider ages
29+
* @param Age $age
30+
* @param Age $ageToCompare
31+
* @param bool $isEquals
32+
*/
33+
public function testEqualsMethod(Age $age, Age $ageToCompare, bool $isEquals)
34+
{
35+
$this->assertEquals($isEquals, $age->equals($ageToCompare));
36+
}
37+
38+
public function ages()
39+
{
40+
return [
41+
[Age::from(14), Age::from(14), true],
42+
[Age::from(12), Age::from(11), false]
43+
];
44+
}
45+
}

0 commit comments

Comments
 (0)