Skip to content

Commit

Permalink
Support complex calculation.
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Oct 18, 2019
1 parent 9477e03 commit cfd2337
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 52 deletions.
56 changes: 39 additions & 17 deletions src/Distance.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,52 @@

use InvalidArgumentException;

class Distance
class Distance extends Metric
{
/**
* Speed value in METER.
* List of supported formats.
*
* @var int
* @var array
*/
protected $value;
protected $supportedFormats = [
'm' => [
'from' => 1,
'to' => 1,
],
'km' => [
'from' => 0.001,
'to' => 1000.0,
],
'mi' => [
'from' => 0.000621371,
'to' => 1609.34,
],
];

/**
* Construct a new Speed from METER.
*
* @param int $value
* @param float $value
*/
public function __construct(int $value)
public function __construct(float $value, string $format = 'm')
{
$this->value = $value;
$value = \is_numeric($value) ? $value : 0;

$this->format = $format;
$this->value = $this->convertTo($value, $format, 'to');
}

/**
* Convert to Speed with new format.
*
* @param string $format
* @return static
*/
public function to(string $format = 'km')
{
return new static(
$this->convertTo($this->value, $format, 'from'), $format
);
}

/**
Expand All @@ -32,15 +61,8 @@ public function __construct(int $value)
*/
public function humanize(string $format = 'km'): float
{
$value = \is_numeric($this->value) ? $this->value : 0;

switch ($format) {
case 'km':
return \round($value * 0.001, 2);
case 'mi':
return \round($value * 0.000621371, 2);
default:
throw new InvalidArgumentException("Unvalid given {$format} format.");
}
return \round(
$this->convertTo($this->value, $format, 'from'), 2
);
}
}
74 changes: 74 additions & 0 deletions src/Metric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Katsana\Metric;

abstract class Metric
{
/**
* Value.
*
* @var float|int
*/
protected $value;

/**
* Value format.
*
* @var string
*/
protected $format;

/**
* List of supported formats.
*
* @var array
*/
protected $supportedFormats = [];

/**
* Format as string.
*
* @return string
*/
public function __toString()
{
return \number_format($this->humanize($this->format), 0, '.', ',');
}

/**
* Convert to new format.
*
* @param string $format
* @return static
*/
abstract public function to(string $format);

/**
* Convert to humanize.
*
* @param string $format
* @return float|int
*
* @throws \InvalidArgumentException
*/
abstract public function humanize(string $format);

/**
* Convert value to.
*
* @param float|int $value
* @param string $format
* @param string $type
* @return float|int
*/
protected function convertTo($value, string $format, string $type = 'from')
{
if (! \array_key_exists($format, $this->supportedFormats)) {
throw new InvalidArgumentException("Unvalid use unsupported {$format} format.");
}

$conversion = $this->supportedFormats[$format];

return ($value * $conversion[$type]);
}
}
57 changes: 40 additions & 17 deletions src/Speed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,53 @@

use InvalidArgumentException;

class Speed
class Speed extends Metric
{
/**
* Speed value in KNOT.
* List of supported formats.
*
* @var float
* @var array
*/
protected $value;
protected $supportedFormats = [
'kn' => [
'from' => 1,
'to' => 1,
],
'kmh' => [
'from' => 1.85200,
'to' => 0.539957,
],
'mph' => [
'from' => 1.15078,
'to' => 0.868976,
],
];

/**
* Construct a new Speed from KNOT.
* Construct a new Speed.
*
* @param float $value
* @param string $format
*/
public function __construct(float $value)
public function __construct(float $value, string $format = 'kn')
{
$this->value = $value;
$value = \is_numeric($value) ? $value : 0;

$this->format = $format;
$this->value = $this->convertTo($value, $format, 'to');
}

/**
* Convert to Speed with new format.
*
* @param string $format
* @return static
*/
public function to(string $format = 'kmh')
{
return new static(
$this->convertTo($this->value, $format, 'from'), $format
);
}

/**
Expand All @@ -32,15 +62,8 @@ public function __construct(float $value)
*/
public function humanize(string $format = 'kmh'): float
{
$value = \is_numeric($this->value) ? $this->value : 0;

switch ($format) {
case 'kmh':
return \round($value * 1.85200, 2);
case 'mph':
return \round($value * 1.15078, 2);
default:
throw new InvalidArgumentException("Unvalid given {$format} format.");
}
return \round(
$this->convertTo($this->value, $format, 'from'), 2
);
}
}
26 changes: 17 additions & 9 deletions tests/DistanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@

class DistanceTest extends TestCase
{
/** @test */
public function it_can_display_formatted_distance()
{
$this->assertSame('60', (string) (new Distance(60000))->to('km'));
$this->assertSame('7', (string) (new Distance(6812))->to('km'));
$this->assertSame('0', (string) (new Distance(12))->to('km'));
$this->assertSame('0', (string) (new Distance(0))->to('km'));
}

/** @test */
public function it_can_convert_distance_from_meter_to_km()
{
$this->assertSame(60.0, (new Distance(60000))->humanize());
$this->assertSame(6.81, (new Distance(6812))->humanize());
$this->assertSame(0.01, (new Distance(12))->humanize());
$this->assertSame(0.0, (new Distance(0))->humanize());

$this->assertSame(60.0, (new Distance('60000'))->humanize());
$this->assertSame(6.81, (new Distance('6812'))->humanize());
$this->assertSame(0.01, (new Distance('12'))->humanize());
$this->assertSame(0.0, (new Distance('0'))->humanize());
}

/** @test */
Expand All @@ -28,10 +32,14 @@ public function it_can_convert_distance_from_meter_to_miles()
$this->assertSame(4.23, (new Distance(6812))->humanize('mi'));
$this->assertSame(0.01, (new Distance(12))->humanize('mi'));
$this->assertSame(0.0, (new Distance(0))->humanize('mi'));
}

$this->assertSame(37.28, (new Distance('60000'))->humanize('mi'));
$this->assertSame(4.23, (new Distance('6812'))->humanize('mi'));
$this->assertSame(0.01, (new Distance('12'))->humanize('mi'));
$this->assertSame(0.0, (new Distance('0'))->humanize('mi'));
/** @test */
public function it_can_convert_distance_from_meter_to_miles_via_km()
{
$this->assertSame(37.28, (new Distance(60000))->to('km')->humanize('mi'));
$this->assertSame(4.23, (new Distance(6812))->to('km')->humanize('mi'));
$this->assertSame(0.01, (new Distance(12))->to('km')->humanize('mi'));
$this->assertSame(0.0, (new Distance(0))->to('km')->humanize('mi'));
}
}
26 changes: 17 additions & 9 deletions tests/SpeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@

class SpeedTest extends TestCase
{
/** @test */
public function it_can_display_formatted_speed()
{
$this->assertSame('111', (string) (new Speed(60))->to('kmh'));
$this->assertSame('126', (string) (new Speed(68.12))->to('kmh'));
$this->assertSame('2', (string) (new Speed(1.2))->to('kmh'));
$this->assertSame('0', (string) (new Speed(0))->to('kmh'));
}

/** @test */
public function it_can_convert_speed_from_knot_to_kmh()
{
$this->assertSame(111.12, (new Speed(60))->humanize());
$this->assertSame(126.16, (new Speed(68.12))->humanize());
$this->assertSame(2.22, (new Speed(1.2))->humanize());
$this->assertSame(0.0, (new Speed(0))->humanize());

$this->assertSame(111.12, (new Speed('60'))->humanize());
$this->assertSame(126.16, (new Speed('68.12'))->humanize());
$this->assertSame(2.22, (new Speed('1.2'))->humanize());
$this->assertSame(0.0, (new Speed('0'))->humanize());
}

/** @test */
Expand All @@ -28,10 +32,14 @@ public function it_can_convert_speed_from_knot_to_mph()
$this->assertSame(78.39, (new Speed(68.12))->humanize('mph'));
$this->assertSame(1.38, (new Speed(1.2))->humanize('mph'));
$this->assertSame(0.0, (new Speed(0))->humanize('mph'));
}

$this->assertSame(69.05, (new Speed('60'))->humanize('mph'));
$this->assertSame(78.39, (new Speed('68.12'))->humanize('mph'));
$this->assertSame(1.38, (new Speed('1.2'))->humanize('mph'));
$this->assertSame(0.0, (new Speed('0'))->humanize('mph'));
/** @test */
public function it_can_convert_speed_from_knot_to_mph_via_kmh()
{
$this->assertSame(69.05, (new Speed(60))->to('kmh')->humanize('mph'));
$this->assertSame(78.39, (new Speed(68.12))->to('kmh')->humanize('mph'));
$this->assertSame(1.38, (new Speed(1.2))->to('kmh')->humanize('mph'));
$this->assertSame(0.0, (new Speed(0))->to('kmh')->humanize('mph'));
}
}

0 comments on commit cfd2337

Please sign in to comment.