Skip to content

Commit cc8809c

Browse files
authored
Merge pull request #22 from danitome24/21-latitude-longitude-vo
#21 - Added latitude and longitude VO
2 parents 4dd80d9 + 8818e42 commit cc8809c

File tree

6 files changed

+290
-0
lines changed

6 files changed

+290
-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\Geography\Exception;
9+
10+
use Throwable;
11+
12+
class InvalidLatitudeException extends \Exception
13+
{
14+
public function __construct($message = "", $code = 0, Throwable $previous = null)
15+
{
16+
parent::__construct($message, $code, $previous);
17+
}
18+
}
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\Geography\Exception;
9+
10+
use Throwable;
11+
12+
class InvalidLongitudeException extends \Exception
13+
{
14+
public function __construct($message = "", $code = 0, Throwable $previous = null)
15+
{
16+
parent::__construct($message, $code, $previous);
17+
}
18+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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\Geography\Exception\InvalidLatitudeException;
11+
use PhpValueObject\ValueObject;
12+
13+
class Latitude implements ValueObject
14+
{
15+
16+
const MAX_LATITUDE = 90;
17+
const MIN_LATITUDE = -90;
18+
19+
/**
20+
* @var float
21+
*/
22+
private $latitude;
23+
24+
/**
25+
* Latitude constructor.
26+
* @param float $latitude
27+
* @throws \PhpValueObject\Geography\Exception\InvalidLatitudeException
28+
*/
29+
private function __construct(float $latitude)
30+
{
31+
$this->checkValidLatitude($latitude);
32+
$this->latitude = $latitude;
33+
}
34+
35+
/**
36+
* Named constructor
37+
*
38+
* @param float $latitude
39+
* @return Latitude
40+
* @throws \PhpValueObject\Geography\Exception\InvalidLatitudeException
41+
*/
42+
public static function fromFloat(float $latitude): self
43+
{
44+
return new static($latitude);
45+
}
46+
47+
/**
48+
* @return float
49+
*/
50+
public function latitude(): float
51+
{
52+
return $this->latitude;
53+
}
54+
55+
/**
56+
* Check if is a valid latitude
57+
*
58+
* @param float $latitude
59+
* @throws InvalidLatitudeException
60+
*/
61+
private function checkValidLatitude(float $latitude): void
62+
{
63+
if ($latitude > static::MAX_LATITUDE || $latitude < static::MIN_LATITUDE) {
64+
throw new InvalidLatitudeException("Latitude value $latitude is invalid");
65+
}
66+
}
67+
68+
/**
69+
* Compare a value object with another one.
70+
*
71+
* @param ValueObject $valueObjectToCompare
72+
* @return bool
73+
*/
74+
public function equals(ValueObject $valueObjectToCompare): bool
75+
{
76+
return ($this->latitude() === $valueObjectToCompare->latitude());
77+
}
78+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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\Geography\Exception\InvalidLongitudeException;
11+
use PhpValueObject\ValueObject;
12+
13+
class Longitude implements ValueObject
14+
{
15+
16+
const MAX_LONGITUDE = 180;
17+
const MIN_LONGITUDE = -180;
18+
19+
/**
20+
* @var float
21+
*/
22+
private $longitude;
23+
24+
/**
25+
* Latitude constructor.
26+
* @param float $longitude
27+
* @throws \PhpValueObject\Geography\Exception\InvalidLongitudeException
28+
*/
29+
private function __construct(float $longitude)
30+
{
31+
$this->checkValidLongitude($longitude);
32+
$this->longitude = $longitude;
33+
}
34+
35+
/**
36+
* Named constructor
37+
*
38+
* @param float $longitude
39+
* @return static
40+
* @throws \PhpValueObject\Geography\Exception\InvalidLongitudeException
41+
*/
42+
public static function fromFloat(float $longitude): self
43+
{
44+
return new static($longitude);
45+
}
46+
47+
/**
48+
* @return float
49+
*/
50+
public function longitude(): float
51+
{
52+
return $this->longitude;
53+
}
54+
55+
/**
56+
* Check if is a valid latitude
57+
*
58+
* @param float $longitude
59+
* @throws InvalidLongitudeException
60+
*/
61+
private function checkValidLongitude(float $longitude): void
62+
{
63+
if ($longitude > static::MAX_LONGITUDE || $longitude < static::MIN_LONGITUDE) {
64+
throw new InvalidLongitudeException("Longitude value $longitude is invalid");
65+
}
66+
}
67+
68+
/**
69+
* Compare a value object with another one.
70+
*
71+
* @param self|ValueObject $valueObjectToCompare
72+
* @return bool
73+
*/
74+
public function equals(ValueObject $valueObjectToCompare): bool
75+
{
76+
return ($this->longitude() === $valueObjectToCompare->longitude());
77+
}
78+
}

tests/Geography/LatitudeTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\Exception\InvalidLatitudeException;
11+
use PhpValueObject\Geography\Latitude;
12+
use PHPUnit\Framework\TestCase;
13+
14+
class LatitudeTest extends TestCase
15+
{
16+
public function testInvalidLatitude()
17+
{
18+
$this->expectException(InvalidLatitudeException::class);
19+
20+
Latitude::fromFloat(-100.983942);
21+
}
22+
23+
public function testValidLatitude()
24+
{
25+
$floatLatitude = 23.9;
26+
$latitude = Latitude::fromFloat($floatLatitude);
27+
28+
$this->assertEquals($floatLatitude, $latitude->latitude());
29+
}
30+
31+
/**
32+
* @dataProvider latitudes
33+
* @param Latitude $latitude
34+
* @param Latitude $latitudeToCompare
35+
* @param bool $isEquals
36+
*/
37+
public function testAssertEquals(Latitude $latitude, Latitude $latitudeToCompare, bool $isEquals)
38+
{
39+
$this->assertEquals($isEquals, $latitude->equals($latitudeToCompare));
40+
}
41+
42+
public function latitudes()
43+
{
44+
return [
45+
[Latitude::fromFloat(12), Latitude::fromFloat(-12), false],
46+
[Latitude::fromFloat(12), Latitude::fromFloat(12), true],
47+
];
48+
}
49+
}

tests/Geography/LongitudeTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\Exception\InvalidLongitudeException;
11+
use PhpValueObject\Geography\Longitude;
12+
use PHPUnit\Framework\TestCase;
13+
14+
class LongitudeTest extends TestCase
15+
{
16+
public function testInvalidLatitude()
17+
{
18+
$this->expectException(InvalidLongitudeException::class);
19+
20+
Longitude::fromFloat(-200.234);
21+
}
22+
23+
public function testValidLongitude()
24+
{
25+
$floatLongitude = 23.9;
26+
$longitude = Longitude::fromFloat($floatLongitude);
27+
28+
$this->assertEquals($floatLongitude, $longitude->longitude());
29+
}
30+
31+
/**
32+
* @dataProvider longitudes
33+
* @param Longitude $longitude
34+
* @param Longitude $longitudeToCompare
35+
* @param bool $isEquals
36+
*/
37+
public function testAssertEquals(Longitude $longitude, Longitude $longitudeToCompare, bool $isEquals)
38+
{
39+
$this->assertEquals($isEquals, $longitude->equals($longitudeToCompare));
40+
}
41+
42+
public function longitudes()
43+
{
44+
return [
45+
[Longitude::fromFloat(12), Longitude::fromFloat(-12), false],
46+
[Longitude::fromFloat(12), Longitude::fromFloat(12), true],
47+
];
48+
}
49+
}

0 commit comments

Comments
 (0)