Skip to content

Commit 98d598a

Browse files
committed
Refactor on weight
1 parent df7453a commit 98d598a

File tree

5 files changed

+251
-60
lines changed

5 files changed

+251
-60
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
final class Grams extends Weight
11+
{
12+
/**
13+
* Named constructor
14+
*
15+
* @param float $weight
16+
* @return static
17+
* @throws \PhpValueObject\Person\Exception\WeightNotValidException
18+
*/
19+
public static function fromGrams(float $weight)
20+
{
21+
return new static($weight);
22+
}
23+
24+
/**
25+
* To Kg
26+
*
27+
* @return mixed
28+
* @throws \PhpValueObject\Person\Exception\WeightNotValidException
29+
*/
30+
public function toKilograms(): Kilograms
31+
{
32+
return Kilograms::fromKg($this->weight() / 1000);
33+
}
34+
35+
/**
36+
* To pounds
37+
*
38+
* @return mixed
39+
* @throws \PhpValueObject\Person\Exception\WeightNotValidException
40+
*/
41+
public function toPounds(): Pounds
42+
{
43+
return Pounds::fromPounds($this->weight() * 0.0022);
44+
}
45+
46+
/**
47+
* To grams
48+
*
49+
* @return mixed
50+
* @throws \PhpValueObject\Person\Exception\WeightNotValidException
51+
*/
52+
public function toGrams(): self
53+
{
54+
return new static($this->weight());
55+
}
56+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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\WeightNotValidException;
11+
12+
final class Kilograms extends Weight
13+
{
14+
const MIN_KG = 0;
15+
const MAX_KG = 200;
16+
17+
/**
18+
* Kilograms constructor.
19+
* @param float $weight
20+
* @throws \PhpValueObject\Person\Exception\WeightNotValidException
21+
*/
22+
protected function __construct($weight)
23+
{
24+
$this->checkIsValidWeight($weight);
25+
parent::__construct($weight);
26+
}
27+
28+
/**
29+
* Named constructor
30+
*
31+
* @param float $weight
32+
* @return static
33+
* @throws \PhpValueObject\Person\Exception\WeightNotValidException
34+
*/
35+
public static function fromKg(float $weight): self
36+
{
37+
return new static($weight);
38+
}
39+
40+
/**
41+
* To Kg
42+
*
43+
* @return mixed
44+
* @throws \PhpValueObject\Person\Exception\WeightNotValidException
45+
*/
46+
public function toKilograms(): self
47+
{
48+
return static::fromKg($this->weight());
49+
}
50+
51+
/**
52+
* To pounds
53+
*
54+
* @return mixed
55+
* @throws \PhpValueObject\Person\Exception\WeightNotValidException
56+
*/
57+
public function toPounds(): Pounds
58+
{
59+
return Pounds::fromPounds($this->weight() * 2.2046);
60+
}
61+
62+
/**
63+
* To grams
64+
*
65+
* @return mixed
66+
* @throws \PhpValueObject\Person\Exception\WeightNotValidException
67+
*/
68+
public function toGrams(): Grams
69+
{
70+
return Grams::fromGrams($this->weight() * 1000);
71+
}
72+
73+
/**
74+
* @param float $kg
75+
* @throws WeightNotValidException
76+
*/
77+
private function checkIsValidWeight(float $kg): void
78+
{
79+
if ($kg < self::MIN_KG || $kg > self::MAX_KG) {
80+
throw new WeightNotValidException('Weight ' . $kg . ' is not valid');
81+
}
82+
}
83+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
final class Pounds extends Weight
11+
{
12+
/**
13+
* Named constructor
14+
*
15+
* @param float $weight
16+
* @return static
17+
* @throws \PhpValueObject\Person\Exception\WeightNotValidException
18+
*/
19+
public static function fromPounds(float $weight): self
20+
{
21+
return new static($weight);
22+
}
23+
24+
/**
25+
* To Kg
26+
*
27+
* @return mixed
28+
* @throws \PhpValueObject\Person\Exception\WeightNotValidException
29+
*/
30+
public function toKilograms(): Kilograms
31+
{
32+
return Kilograms::fromKg($this->weight() * 0.453592);
33+
}
34+
35+
/**
36+
* To pounds
37+
*
38+
* @return mixed
39+
* @throws \PhpValueObject\Person\Exception\WeightNotValidException
40+
*/
41+
public function toPounds(): self
42+
{
43+
return new static($this->weight());
44+
}
45+
46+
/**
47+
* To grams
48+
*
49+
* @return mixed
50+
* @throws \PhpValueObject\Person\Exception\WeightNotValidException
51+
*/
52+
public function toGrams(): Grams
53+
{
54+
return Grams::fromGrams($this->weight() * 453.592);
55+
}
56+
}

src/PhpValueObject/Person/Weight.php

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,63 +10,46 @@
1010
use PhpValueObject\Person\Exception\WeightNotValidException;
1111
use PhpValueObject\ValueObject;
1212

13-
final class Weight implements ValueObject
13+
abstract class Weight implements ValueObject
1414
{
15-
const MIN_KG = 10;
16-
const MAX_KG = 200;
1715

1816
/**
1917
* Weight in kg
2018
*
2119
* @var float
2220
*/
23-
private $weight;
21+
protected $weight;
2422

2523
/**
2624
* Weight constructor.
2725
* @param float $weight
2826
* @throws \PhpValueObject\Person\Exception\WeightNotValidException
2927
*/
30-
private function __construct(float $weight)
28+
protected function __construct(float $weight)
3129
{
32-
$this->changeWeight($weight);
30+
$this->weight = $weight;
3331
}
3432

3533
/**
36-
* Named constructor
34+
* To Kg
3735
*
38-
* @param float $weight
39-
* @return Weight
40-
* @throws \PhpValueObject\Person\Exception\WeightNotValidException
36+
* @return mixed
4137
*/
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-
}
38+
abstract public function toKilograms(): Kilograms;
5439

5540
/**
56-
* @return float
41+
* To pounds
42+
*
43+
* @return mixed
5744
*/
58-
public function inGrams(): float
59-
{
60-
return $this->weight * 1000;
61-
}
45+
abstract public function toPounds(): Pounds;
6246

6347
/**
64-
* @return float
48+
* To grams
49+
*
50+
* @return mixed
6551
*/
66-
public function inPounds(): float
67-
{
68-
return $this->weight * 2.2046;
69-
}
52+
abstract public function toGrams(): Grams;
7053

7154
/**
7255
* Compare a value object with another one.
@@ -76,19 +59,14 @@ public function inPounds(): float
7659
*/
7760
public function equals(ValueObject $valueObjectToCompare): bool
7861
{
79-
return ($this->inKilograms() === $valueObjectToCompare->inKilograms());
62+
return ($this->weight() === $valueObjectToCompare->weight());
8063
}
8164

8265
/**
83-
* @param float $kg
84-
* @throws WeightNotValidException
66+
* @return float
8567
*/
86-
private function changeWeight(float $kg)
68+
public function weight(): float
8769
{
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;
70+
return $this->weight;
9371
}
9472
}

tests/Person/WeightTest.php

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,64 @@
99

1010
use PHPUnit\Framework\TestCase;
1111
use PhpValueObject\Person\Exception\WeightNotValidException;
12+
use PhpValueObject\Person\Grams;
13+
use PhpValueObject\Person\Kilograms;
14+
use PhpValueObject\Person\Pounds;
1215
use PhpValueObject\Person\Weight;
1316

1417
class WeightTest extends TestCase
1518
{
16-
public function testWeightNotValidExceptionIsThrown()
19+
public function testPoundsIsBuild()
1720
{
18-
$this->expectException(WeightNotValidException::class);
21+
$pounds = 4.5;
22+
$poundsInstance = Pounds::fromPounds($pounds);
23+
$this->assertEquals($pounds, $poundsInstance->weight());
24+
$this->assertEquals(Grams::fromGrams(2041.164), $poundsInstance->toGrams());
25+
$this->assertEquals(Kilograms::fromKg(2.041164), $poundsInstance->toKilograms());
26+
$this->assertEquals(Pounds::fromPounds($pounds), $poundsInstance->toPounds());
27+
}
1928

20-
Weight::fromKg(2.34);
29+
public function testKilogramsIsBuild()
30+
{
31+
$kg = 87.7;
32+
$kilograms = Kilograms::fromKg($kg);
33+
$this->assertEquals($kg, $kilograms->weight());
34+
$this->assertEquals(Grams::fromGrams($kg * 1000), $kilograms->toGrams());
35+
$this->assertEquals(Pounds::fromPounds(193.34342), $kilograms->toPounds());
36+
$this->assertEquals(Kilograms::fromKg($kg), $kilograms->toKilograms());
2137
}
2238

23-
public function testWeightIsValidInstance()
39+
public function testNotValidKilogramsException()
2440
{
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());
41+
$this->expectException(WeightNotValidException::class);
42+
Kilograms::fromKg(-4.1);
43+
}
44+
45+
public function testGramsIsBuild()
46+
{
47+
$grams = 87700;
48+
$grms = Grams::fromGrams($grams);
49+
$this->assertEquals(Kilograms::fromKg(87.7), $grms->toKilograms());
50+
$this->assertEquals(Pounds::fromPounds(192.94), $grms->toPounds());
51+
$this->assertEquals(Grams::fromGrams($grams), $grms->toGrams());
3152
}
3253

3354
/**
3455
* @dataProvider weights
3556
* @param Weight $weight
36-
* @param Weight $otherWeight
57+
* @param Weight $weightToCompare
3758
* @param bool $isEquals
3859
*/
39-
public function testEqualsMethod(Weight $weight, Weight $otherWeight, bool $isEquals)
60+
public function testEqualsMethod(Weight $weight, Weight $weightToCompare, bool $isEquals)
4061
{
41-
$this->assertEquals($weight->equals($otherWeight), $isEquals);
62+
$this->assertEquals($isEquals, $weight->equals($weightToCompare));
4263
}
4364

44-
/**
45-
* @return array
46-
*/
47-
public function weights(): array
65+
public function weights()
4866
{
4967
return [
50-
[Weight::fromKg(25.98), Weight::fromKg(23.2), false],
51-
[Weight::fromKg(25.98), Weight::fromKg(25.98), true]
68+
[Grams::fromGrams(123), Grams::fromGrams(123), true],
69+
[Grams::fromGrams(111), Grams::fromGrams(123), false]
5270
];
5371
}
5472
}

0 commit comments

Comments
 (0)