Skip to content

Commit 26bbdf1

Browse files
committed
#6 - Added height to VO
1 parent 6e7feaa commit 26bbdf1

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed
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 <[email protected]>
5+
* GitHub: danitome24
6+
*/
7+
8+
namespace PhpValueObject\Person\Exception;
9+
10+
use Throwable;
11+
12+
class HeightLengthWrongException extends \Exception
13+
{
14+
public function __construct($message = "", $code = 0, Throwable $previous = null)
15+
{
16+
parent::__construct($message, $code, $previous);
17+
}
18+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* This software was built by:
4+
* Daniel Tomé Fernández <[email protected]>
5+
* GitHub: danitome24
6+
*/
7+
8+
namespace PhpValueObject\Person;
9+
10+
use PhpValueObject\Person\Exception\HeightLengthWrongException;
11+
use PhpValueObject\ValueObject;
12+
13+
final class Height implements ValueObject
14+
{
15+
const MAX_HEIGHT = 210;
16+
const MIN_HEIGHT = 10;
17+
18+
/**
19+
* Height in cm
20+
*
21+
* @var int
22+
*/
23+
private $height;
24+
25+
/**
26+
* Height constructor.
27+
* @param int $height
28+
* @throws \PhpValueObject\Person\Exception\HeightLengthWrongException
29+
*/
30+
private function __construct(int $height)
31+
{
32+
$this->changeHeight($height);
33+
}
34+
35+
/**
36+
* @param int $height
37+
* @return Height
38+
* @throws \PhpValueObject\Person\Exception\HeightLengthWrongException
39+
*/
40+
public static function build(int $height): self
41+
{
42+
return new static($height);
43+
}
44+
45+
/**
46+
* Height in centimeters
47+
*
48+
* @return int
49+
*/
50+
public function inCentimeters(): int
51+
{
52+
return $this->height;
53+
}
54+
55+
/**
56+
* Height in meters
57+
*
58+
* @return float
59+
*/
60+
public function inMeters(): float
61+
{
62+
return $this->height / 100;
63+
}
64+
65+
/**
66+
* Compare a value object with another one.
67+
*
68+
* @param self|ValueObject $valueObjectToCompare
69+
* @return bool
70+
*/
71+
public function equals(ValueObject $valueObjectToCompare): bool
72+
{
73+
return ($this->inMeters() === $valueObjectToCompare->inMeters());
74+
}
75+
76+
/**
77+
* Change height and check if is valid before is added
78+
*
79+
* @param int $height
80+
* @throws HeightLengthWrongException
81+
*/
82+
private function changeHeight(int $height)
83+
{
84+
if ($height > self::MAX_HEIGHT || $height < self::MIN_HEIGHT) {
85+
throw new HeightLengthWrongException();
86+
}
87+
88+
$this->height = $height;
89+
}
90+
}

tests/Person/HeightTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* This software was built by:
4+
* Daniel Tomé Fernández <[email protected]>
5+
* GitHub: danitome24
6+
*/
7+
8+
namespace Test\Person;
9+
10+
use PHPUnit\Framework\TestCase;
11+
use PhpValueObject\Person\Exception\HeightLengthWrongException;
12+
use PhpValueObject\Person\Height;
13+
14+
class HeightTest extends TestCase
15+
{
16+
public function testHeightIsLessThanMinimumException()
17+
{
18+
$this->expectException(HeightLengthWrongException::class);
19+
20+
$height = Height::build(2);
21+
}
22+
23+
public function testHeightIsMoreThanMinimumException()
24+
{
25+
$this->expectException(HeightLengthWrongException::class);
26+
27+
$height = Height::build(25000);
28+
}
29+
30+
public function testHeightInstanceValues()
31+
{
32+
$height = Height::build(193);
33+
$this->assertEquals(1.93, $height->inMeters());
34+
$this->assertEquals(193, $height->inCentimeters());
35+
}
36+
37+
/**
38+
* @dataProvider heights
39+
* @param Height $height
40+
* @param Height $heightToCompare
41+
* @param bool $isEquals
42+
*/
43+
public function testHeightEqualsMethod(Height $height, Height $heightToCompare, bool $isEquals)
44+
{
45+
$this->assertEquals($isEquals, $height->equals($heightToCompare));
46+
}
47+
48+
/**
49+
* @return array
50+
*/
51+
public function heights(): array
52+
{
53+
return [
54+
[Height::build(190), Height::build(180), false],
55+
[Height::build(190), Height::build(190), true]
56+
];
57+
}
58+
}

0 commit comments

Comments
 (0)