Skip to content

Commit 5dcdb20

Browse files
authored
Merge pull request #10 from danitome24/7-weight-vo
#7 - Added weight VO
2 parents f17d840 + 7ebc167 commit 5dcdb20

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 <danieltomefer@gmail.com>
5+
* GitHub: danitome24
6+
*/
7+
8+
namespace PhpValueObject\Person\Exception;
9+
10+
use Throwable;
11+
12+
class WeightNotValidException extends \Exception
13+
{
14+
public function __construct($message = "", $code = 0, Throwable $previous = null)
15+
{
16+
parent::__construct($message, $code, $previous);
17+
}
18+
}

src/PhpValueObject/Person/Weight.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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\WeightNotValidException;
11+
use PhpValueObject\ValueObject;
12+
13+
final class Weight implements ValueObject
14+
{
15+
const MIN_KG = 10;
16+
const MAX_KG = 200;
17+
18+
/**
19+
* Weight in kg
20+
*
21+
* @var float
22+
*/
23+
private $weight;
24+
25+
/**
26+
* Weight constructor.
27+
* @param float $weight
28+
* @throws \PhpValueObject\Person\Exception\WeightNotValidException
29+
*/
30+
private function __construct(float $weight)
31+
{
32+
$this->changeWeight($weight);
33+
}
34+
35+
/**
36+
* Named constructor
37+
*
38+
* @param float $weight
39+
* @return Weight
40+
* @throws \PhpValueObject\Person\Exception\WeightNotValidException
41+
*/
42+
public static function fromKg(float $weight): self
43+
{
44+
return new static($weight);
45+
}
46+
47+
/**
48+
* @return float
49+
*/
50+
public function inKilograms(): float
51+
{
52+
return $this->weight;
53+
}
54+
55+
/**
56+
* @return float
57+
*/
58+
public function inGrams(): float
59+
{
60+
return $this->weight * 1000;
61+
}
62+
63+
/**
64+
* @return float
65+
*/
66+
public function inPounds(): float
67+
{
68+
return $this->weight * 2.2046;
69+
}
70+
71+
/**
72+
* Compare a value object with another one.
73+
*
74+
* @param static|ValueObject $valueObjectToCompare
75+
* @return bool
76+
*/
77+
public function equals(ValueObject $valueObjectToCompare): bool
78+
{
79+
return ($this->inKilograms() === $valueObjectToCompare->inKilograms());
80+
}
81+
82+
/**
83+
* @param float $kg
84+
* @throws WeightNotValidException
85+
*/
86+
private function changeWeight(float $kg)
87+
{
88+
if ($kg < self::MIN_KG || $kg > self::MAX_KG) {
89+
throw new WeightNotValidException('Weight ' . $kg . ' is not valid');
90+
}
91+
92+
$this->weight = $kg;
93+
}
94+
}

tests/Person/WeightTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* This software was built by:
4+
* Daniel Tomé Fernández <danieltomefer@gmail.com>
5+
* GitHub: danitome24
6+
*/
7+
8+
namespace Test\Person;
9+
10+
use PHPUnit\Framework\TestCase;
11+
use PhpValueObject\Person\Exception\WeightNotValidException;
12+
use PhpValueObject\Person\Weight;
13+
14+
class WeightTest extends TestCase
15+
{
16+
public function testWeightNotValidExceptionIsThrown()
17+
{
18+
$this->expectException(WeightNotValidException::class);
19+
20+
Weight::fromKg(2.34);
21+
}
22+
23+
public function testWeightIsValidInstance()
24+
{
25+
$weightInKg = 90.5;
26+
$weightInPounds = $weightInKg * 2.2046;
27+
$weight = Weight::fromKg($weightInKg);
28+
$this->assertEquals($weightInKg, $weight->inKilograms());
29+
$this->assertEquals($weightInKg * 1000, $weight->inGrams());
30+
$this->assertEquals($weightInPounds, $weight->inPounds());
31+
}
32+
33+
/**
34+
* @dataProvider weights
35+
* @param Weight $weight
36+
* @param Weight $otherWeight
37+
* @param bool $isEquals
38+
*/
39+
public function testEqualsMethod(Weight $weight, Weight $otherWeight, bool $isEquals)
40+
{
41+
$this->assertEquals($weight->equals($otherWeight), $isEquals);
42+
}
43+
44+
/**
45+
* @return array
46+
*/
47+
public function weights(): array
48+
{
49+
return [
50+
[Weight::fromKg(25.98), Weight::fromKg(23.2), false],
51+
[Weight::fromKg(25.98), Weight::fromKg(25.98), true]
52+
];
53+
}
54+
}

0 commit comments

Comments
 (0)