Skip to content

#21 - Added latitude and longitude VO #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* This software was built by:
* Daniel Tomé Fernández <danieltomefer@gmail.com>
* GitHub: danitome24
*/

namespace PhpValueObject\Geography\Exception;

use Throwable;

class InvalidLatitudeException extends \Exception
{
public function __construct($message = "", $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* This software was built by:
* Daniel Tomé Fernández <danieltomefer@gmail.com>
* GitHub: danitome24
*/

namespace PhpValueObject\Geography\Exception;

use Throwable;

class InvalidLongitudeException extends \Exception
{
public function __construct($message = "", $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
78 changes: 78 additions & 0 deletions src/PhpValueObject/Geography/Latitude.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* This software was built by:
* Daniel Tomé Fernández <danieltomefer@gmail.com>
* GitHub: danitome24
*/

namespace PhpValueObject\Geography;

use PhpValueObject\Geography\Exception\InvalidLatitudeException;
use PhpValueObject\ValueObject;

class Latitude implements ValueObject
{

const MAX_LATITUDE = 90;
const MIN_LATITUDE = -90;

/**
* @var float
*/
private $latitude;

/**
* Latitude constructor.
* @param float $latitude
* @throws \PhpValueObject\Geography\Exception\InvalidLatitudeException
*/
private function __construct(float $latitude)
{
$this->checkValidLatitude($latitude);
$this->latitude = $latitude;
}

/**
* Named constructor
*
* @param float $latitude
* @return Latitude
* @throws \PhpValueObject\Geography\Exception\InvalidLatitudeException
*/
public static function fromFloat(float $latitude): self
{
return new static($latitude);
}

/**
* @return float
*/
public function latitude(): float
{
return $this->latitude;
}

/**
* Check if is a valid latitude
*
* @param float $latitude
* @throws InvalidLatitudeException
*/
private function checkValidLatitude(float $latitude): void
{
if ($latitude > static::MAX_LATITUDE || $latitude < static::MIN_LATITUDE) {
throw new InvalidLatitudeException("Latitude value $latitude is invalid");
}
}

/**
* Compare a value object with another one.
*
* @param ValueObject $valueObjectToCompare
* @return bool
*/
public function equals(ValueObject $valueObjectToCompare): bool
{
return ($this->latitude() === $valueObjectToCompare->latitude());
}
}
78 changes: 78 additions & 0 deletions src/PhpValueObject/Geography/Longitude.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* This software was built by:
* Daniel Tomé Fernández <danieltomefer@gmail.com>
* GitHub: danitome24
*/

namespace PhpValueObject\Geography;

use PhpValueObject\Geography\Exception\InvalidLongitudeException;
use PhpValueObject\ValueObject;

class Longitude implements ValueObject
{

const MAX_LONGITUDE = 180;
const MIN_LONGITUDE = -180;

/**
* @var float
*/
private $longitude;

/**
* Latitude constructor.
* @param float $longitude
* @throws \PhpValueObject\Geography\Exception\InvalidLongitudeException
*/
private function __construct(float $longitude)
{
$this->checkValidLongitude($longitude);
$this->longitude = $longitude;
}

/**
* Named constructor
*
* @param float $longitude
* @return static
* @throws \PhpValueObject\Geography\Exception\InvalidLongitudeException
*/
public static function fromFloat(float $longitude): self
{
return new static($longitude);
}

/**
* @return float
*/
public function longitude(): float
{
return $this->longitude;
}

/**
* Check if is a valid latitude
*
* @param float $longitude
* @throws InvalidLongitudeException
*/
private function checkValidLongitude(float $longitude): void
{
if ($longitude > static::MAX_LONGITUDE || $longitude < static::MIN_LONGITUDE) {
throw new InvalidLongitudeException("Longitude value $longitude is invalid");
}
}

/**
* Compare a value object with another one.
*
* @param self|ValueObject $valueObjectToCompare
* @return bool
*/
public function equals(ValueObject $valueObjectToCompare): bool
{
return ($this->longitude() === $valueObjectToCompare->longitude());
}
}
49 changes: 49 additions & 0 deletions tests/Geography/LatitudeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* This software was built by:
* Daniel Tomé Fernández <danieltomefer@gmail.com>
* GitHub: danitome24
*/

namespace Geography;

use PhpValueObject\Geography\Exception\InvalidLatitudeException;
use PhpValueObject\Geography\Latitude;
use PHPUnit\Framework\TestCase;

class LatitudeTest extends TestCase
{
public function testInvalidLatitude()
{
$this->expectException(InvalidLatitudeException::class);

Latitude::fromFloat(-100.983942);
}

public function testValidLatitude()
{
$floatLatitude = 23.9;
$latitude = Latitude::fromFloat($floatLatitude);

$this->assertEquals($floatLatitude, $latitude->latitude());
}

/**
* @dataProvider latitudes
* @param Latitude $latitude
* @param Latitude $latitudeToCompare
* @param bool $isEquals
*/
public function testAssertEquals(Latitude $latitude, Latitude $latitudeToCompare, bool $isEquals)
{
$this->assertEquals($isEquals, $latitude->equals($latitudeToCompare));
}

public function latitudes()
{
return [
[Latitude::fromFloat(12), Latitude::fromFloat(-12), false],
[Latitude::fromFloat(12), Latitude::fromFloat(12), true],
];
}
}
49 changes: 49 additions & 0 deletions tests/Geography/LongitudeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* This software was built by:
* Daniel Tomé Fernández <danieltomefer@gmail.com>
* GitHub: danitome24
*/

namespace Geography;

use PhpValueObject\Geography\Exception\InvalidLongitudeException;
use PhpValueObject\Geography\Longitude;
use PHPUnit\Framework\TestCase;

class LongitudeTest extends TestCase
{
public function testInvalidLatitude()
{
$this->expectException(InvalidLongitudeException::class);

Longitude::fromFloat(-200.234);
}

public function testValidLongitude()
{
$floatLongitude = 23.9;
$longitude = Longitude::fromFloat($floatLongitude);

$this->assertEquals($floatLongitude, $longitude->longitude());
}

/**
* @dataProvider longitudes
* @param Longitude $longitude
* @param Longitude $longitudeToCompare
* @param bool $isEquals
*/
public function testAssertEquals(Longitude $longitude, Longitude $longitudeToCompare, bool $isEquals)
{
$this->assertEquals($isEquals, $longitude->equals($longitudeToCompare));
}

public function longitudes()
{
return [
[Longitude::fromFloat(12), Longitude::fromFloat(-12), false],
[Longitude::fromFloat(12), Longitude::fromFloat(12), true],
];
}
}