base conversion classes for units #189
This commit is contained in:
parent
176855b680
commit
a8e06c6cc6
@ -24,6 +24,7 @@ $factory->define(App\Models\Flight::class, function (Faker $faker) use ($airline
|
||||
'alt_airport_id' => function () {
|
||||
return factory(App\Models\Airport::class)->create()->id;
|
||||
},
|
||||
'distance' => $faker->numberBetween(0, 3000),
|
||||
'route' => $faker->randomElement(['', $faker->text(5)]),
|
||||
'dpt_time' => $faker->time(),
|
||||
'arr_time' => $faker->time(),
|
||||
|
@ -62,16 +62,16 @@ class CreateSettingsTable extends Migration
|
||||
$this->addSetting('general.distance_unit', [
|
||||
'name' => 'Distance Units',
|
||||
'group' => 'general',
|
||||
'value' => 'nm',
|
||||
'value' => 'NM',
|
||||
'type' => 'select',
|
||||
'options' => 'km,mi,nm',
|
||||
'options' => 'km,mi,NM',
|
||||
'description' => 'The distance unit to show',
|
||||
]);
|
||||
|
||||
$this->addSetting('general.weight_unit', [
|
||||
'name' => 'Weight Units',
|
||||
'group' => 'general',
|
||||
'value' => 'kg',
|
||||
'value' => 'lbs',
|
||||
'type' => 'select',
|
||||
'options' => 'lbs, kg',
|
||||
'description' => 'The weight unit',
|
||||
@ -80,9 +80,9 @@ class CreateSettingsTable extends Migration
|
||||
$this->addSetting('general.speed_unit', [
|
||||
'name' => 'Speed Units',
|
||||
'group' => 'general',
|
||||
'value' => 'Km/H',
|
||||
'value' => 'knot',
|
||||
'type' => 'select',
|
||||
'options' => 'Km/H,kts',
|
||||
'options' => 'km/h,knot',
|
||||
'description' => 'The speed unit',
|
||||
]);
|
||||
|
||||
@ -98,9 +98,9 @@ class CreateSettingsTable extends Migration
|
||||
$this->addSetting('general.liquid_unit', [
|
||||
'name' => 'Liquid Units',
|
||||
'group' => 'general',
|
||||
'value' => 'lbs',
|
||||
'value' => 'gal',
|
||||
'type' => 'select',
|
||||
'options' => 'liters,gal,kg,lbs',
|
||||
'options' => 'liters,gal',
|
||||
'description' => 'The liquid units',
|
||||
]);
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Support\Units\Distance;
|
||||
use Illuminate\Http\Resources\Json\Resource;
|
||||
|
||||
class Flight extends Resource
|
||||
@ -10,7 +11,11 @@ class Flight extends Resource
|
||||
{
|
||||
$flight = parent::toArray($request);
|
||||
|
||||
$flight['field'] = true;
|
||||
// Return multiple measures so the client can pick what they want
|
||||
if($flight['distance'] instanceof Distance) {
|
||||
$flight['distance'] = $flight['distance']->toJson();
|
||||
}
|
||||
|
||||
$flight['airline'] = new Airline($this->airline);
|
||||
$flight['subfleets'] = Subfleet::collection($this->subfleets);
|
||||
|
||||
|
@ -2,6 +2,10 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Support\Units\Distance;
|
||||
use PhpUnitsOfMeasure\Exception\NonNumericValue;
|
||||
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
|
||||
|
||||
use App\Models\Traits\HashId;
|
||||
|
||||
class Flight extends BaseModel
|
||||
@ -58,11 +62,46 @@ class Flight extends BaseModel
|
||||
$flight_id = $this->airline->code;
|
||||
$flight_id .= $this->flight_number;
|
||||
|
||||
# TODO: Add in code/leg if set
|
||||
if (filled($this->route_code)) {
|
||||
$flight_id .= '/C' . $this->route_code;
|
||||
}
|
||||
|
||||
if (filled($this->route_leg)) {
|
||||
$flight_id .= '/L' . $this->route_leg;
|
||||
}
|
||||
|
||||
return $flight_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new Length unit so conversions can be made
|
||||
* @return int|Distance
|
||||
*/
|
||||
public function getDistanceAttribute()
|
||||
{
|
||||
try {
|
||||
$distance = (float) $this->attributes['distance'];
|
||||
return new Distance($distance, Distance::STORAGE_UNIT);
|
||||
} catch (NonNumericValue $e) {
|
||||
return 0;
|
||||
} catch (NonStringUnitName $e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the distance unit, convert to our internal default unit
|
||||
* @param $value
|
||||
*/
|
||||
public function setDistanceAttribute($value)
|
||||
{
|
||||
if($value instanceof Distance) {
|
||||
$this->attributes['distance'] = $value->toUnit(Distance::STORAGE_UNIT);
|
||||
} else {
|
||||
$this->attributes['distance'] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship
|
||||
*/
|
||||
|
@ -5,6 +5,10 @@ namespace App\Models;
|
||||
use App\Models\Enums\AcarsType;
|
||||
use App\Models\Enums\PirepState;
|
||||
use App\Models\Traits\HashId;
|
||||
use App\Support\Units\Distance;
|
||||
|
||||
use PhpUnitsOfMeasure\Exception\NonNumericValue;
|
||||
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
|
||||
|
||||
/**
|
||||
* Class Pirep
|
||||
@ -99,6 +103,38 @@ class Pirep extends BaseModel
|
||||
return $flight_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new Length unit so conversions can be made
|
||||
* @return int|Distance
|
||||
*/
|
||||
public function getDistanceAttribute()
|
||||
{
|
||||
try {
|
||||
$distance = (float)$this->attributes['distance'];
|
||||
return new Distance($distance, 'mi');
|
||||
} catch (NonNumericValue $e) {
|
||||
return 0;
|
||||
} catch (NonStringUnitName $e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the planned_distance in a converter class
|
||||
* @return int|Distance
|
||||
*/
|
||||
public function getPlannedDistanceAttribute()
|
||||
{
|
||||
try {
|
||||
$distance = (float) $this->attributes['planned_distance'];
|
||||
return new Distance($distance, 'mi');
|
||||
} catch (NonNumericValue $e) {
|
||||
return 0;
|
||||
} catch (NonStringUnitName $e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Do some cleanup on the route
|
||||
* @param $route
|
||||
|
36
app/Support/Units/Altitude.php
Normal file
36
app/Support/Units/Altitude.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Units;
|
||||
|
||||
/**
|
||||
* Wrap the converter class
|
||||
* @package App\Support\Units
|
||||
*/
|
||||
class Altitude extends \PhpUnitsOfMeasure\PhysicalQuantity\Length
|
||||
{
|
||||
/**
|
||||
* The unit that this is stored as
|
||||
*/
|
||||
public const STORAGE_UNIT = 'feet';
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$unit = setting('general.altitude_unit');
|
||||
$value = $this->toUnit($unit);
|
||||
return (string) round($value, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* For the HTTP Resource call
|
||||
*/
|
||||
public function toJson()
|
||||
{
|
||||
return [
|
||||
'ft' => round($this->toUnit('feet'), 2),
|
||||
'm' => round($this->toUnit('meters') / 1000, 2),
|
||||
];
|
||||
}
|
||||
}
|
37
app/Support/Units/Distance.php
Normal file
37
app/Support/Units/Distance.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Units;
|
||||
|
||||
/**
|
||||
* Wrap the converter class
|
||||
* @package App\Support\Units
|
||||
*/
|
||||
class Distance extends \PhpUnitsOfMeasure\PhysicalQuantity\Length
|
||||
{
|
||||
/**
|
||||
* The unit that this is stored as
|
||||
*/
|
||||
public const STORAGE_UNIT = 'mi';
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$unit = setting('general.distance_unit');
|
||||
$value = $this->toUnit($unit);
|
||||
return (string) round($value, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* For the HTTP Resource call
|
||||
*/
|
||||
public function toJson()
|
||||
{
|
||||
return [
|
||||
'mi' => round($this->toUnit('miles'), 2),
|
||||
'nmi' => round($this->toUnit('nmi'), 2),
|
||||
'km' => round($this->toUnit('meters') / 1000, 2),
|
||||
];
|
||||
}
|
||||
}
|
31
app/Support/Units/Mass.php
Normal file
31
app/Support/Units/Mass.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Units;
|
||||
|
||||
/**
|
||||
* Class Mass
|
||||
* @package App\Support\Units
|
||||
*/
|
||||
class Mass extends \PhpUnitsOfMeasure\PhysicalQuantity\Mass
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$unit = setting('general.weight_unit');
|
||||
$value = $this->toUnit($unit);
|
||||
return (string)round($value, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* For the HTTP Resource call
|
||||
*/
|
||||
public function toJson()
|
||||
{
|
||||
return [
|
||||
'kg' => round($this->toUnit('kg'), 2),
|
||||
'lgs' => round($this->toUnit('lbs'), 2),
|
||||
];
|
||||
}
|
||||
}
|
36
app/Support/Units/Velocity.php
Normal file
36
app/Support/Units/Velocity.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Units;
|
||||
|
||||
/**
|
||||
* Class Velocity
|
||||
* @package App\Support\Units
|
||||
*/
|
||||
class Velocity extends \PhpUnitsOfMeasure\PhysicalQuantity\Velocity
|
||||
{
|
||||
/**
|
||||
* The unit that this is stored as
|
||||
*/
|
||||
public const STORAGE_UNIT = 'knot';
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$unit = setting('general.speed_unit');
|
||||
$value = $this->toUnit($unit);
|
||||
return (string) round($value, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* For the HTTP Resource call
|
||||
*/
|
||||
public function toJson()
|
||||
{
|
||||
return [
|
||||
'knot' => round($this->toUnit('knot'), 2),
|
||||
'km/h' => round($this->toUnit('km/h'), 2),
|
||||
];
|
||||
}
|
||||
}
|
36
app/Support/Units/Volume.php
Normal file
36
app/Support/Units/Volume.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Units;
|
||||
|
||||
/**
|
||||
* Wrap the converter class
|
||||
* @package App\Support\Units
|
||||
*/
|
||||
class Volume extends \PhpUnitsOfMeasure\PhysicalQuantity\Volume
|
||||
{
|
||||
/**
|
||||
* The unit that this is stored as
|
||||
*/
|
||||
public const STORAGE_UNIT = 'gal';
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$unit = setting('general.liquid_unit');
|
||||
$value = $this->toUnit($unit);
|
||||
return (string) round($value, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* For the HTTP Resource call
|
||||
*/
|
||||
public function toJson()
|
||||
{
|
||||
return [
|
||||
'gal' => round($this->toUnit('gal'), 2),
|
||||
'liters' => round($this->toUnit('liters'), 2),
|
||||
];
|
||||
}
|
||||
}
|
@ -34,7 +34,8 @@
|
||||
"theiconic/php-ga-measurement-protocol": "2.7.x",
|
||||
"joshbrw/laravel-module-installer": "0.1.x",
|
||||
"irazasyed/laravel-gamp": "1.3.x",
|
||||
"vierbergenlars/php-semver": "3.0.x"
|
||||
"vierbergenlars/php-semver": "3.0.x",
|
||||
"php-units-of-measure/php-units-of-measure": "2.1.x"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "6.4.0",
|
||||
|
103
composer.lock
generated
103
composer.lock
generated
@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "d8de3ca80d581f2d485e43235f9eb68e",
|
||||
"content-hash": "c678f57dcf9193fd06b7334ee2cbfaac",
|
||||
"packages": [
|
||||
{
|
||||
"name": "arrilot/laravel-widgets",
|
||||
@ -2103,6 +2103,57 @@
|
||||
],
|
||||
"time": "2016-01-26T13:27:02+00:00"
|
||||
},
|
||||
{
|
||||
"name": "php-units-of-measure/php-units-of-measure",
|
||||
"version": "v2.1.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/PhpUnitsOfMeasure/php-units-of-measure.git",
|
||||
"reference": "25f476e9c50e11af8cf044d22ffc271c96aa5248"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/PhpUnitsOfMeasure/php-units-of-measure/zipball/25f476e9c50e11af8cf044d22ffc271c96aa5248",
|
||||
"reference": "25f476e9c50e11af8cf044d22ffc271c96aa5248",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.4.0"
|
||||
},
|
||||
"replace": {
|
||||
"triplepoint/php-units-of-measure": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "4.8.*",
|
||||
"squizlabs/php_codesniffer": "2.2.*"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"PhpUnitsOfMeasure\\": "source/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Jonathan Hanson",
|
||||
"email": "jonathan@jonathan-hanson.org",
|
||||
"homepage": "http://www.jonathan-hanson.org/",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"description": "A PHP library for converting between standard units of measure.",
|
||||
"homepage": "https://github.com/PhpUnitsOfMeasure/php-units-of-measure",
|
||||
"keywords": [
|
||||
"conversion",
|
||||
"data",
|
||||
"measurements"
|
||||
],
|
||||
"time": "2016-07-24T22:36:00+00:00"
|
||||
},
|
||||
{
|
||||
"name": "phpseclib/phpseclib",
|
||||
"version": "2.0.9",
|
||||
@ -2197,27 +2248,27 @@
|
||||
},
|
||||
{
|
||||
"name": "pragmarx/version",
|
||||
"version": "v0.2.4",
|
||||
"version": "v0.2.5",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/antonioribeiro/version.git",
|
||||
"reference": "380efdee0e0ef6c2f864d23b5df09fdd34690390"
|
||||
"reference": "ed34e37904067bdb15857d3f2bcc11683f37eb84"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/antonioribeiro/version/zipball/380efdee0e0ef6c2f864d23b5df09fdd34690390",
|
||||
"reference": "380efdee0e0ef6c2f864d23b5df09fdd34690390",
|
||||
"url": "https://api.github.com/repos/antonioribeiro/version/zipball/ed34e37904067bdb15857d3f2bcc11683f37eb84",
|
||||
"reference": "ed34e37904067bdb15857d3f2bcc11683f37eb84",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"laravel/framework": "5.5.*",
|
||||
"laravel/framework": ">=5.5.33",
|
||||
"php": "^7.0",
|
||||
"pragmarx/yaml": "^0.1",
|
||||
"symfony/process": "^3.3"
|
||||
"symfony/process": "^3.3|^4.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"orchestra/testbench": "^3.5",
|
||||
"phpunit/phpunit": "^6.4"
|
||||
"orchestra/testbench": "^3.5|^3.6",
|
||||
"phpunit/phpunit": "^6.4|^7.0"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
@ -2254,30 +2305,30 @@
|
||||
"version",
|
||||
"versioning"
|
||||
],
|
||||
"time": "2018-01-09T14:51:48+00:00"
|
||||
"time": "2018-02-10T17:40:28+00:00"
|
||||
},
|
||||
{
|
||||
"name": "pragmarx/yaml",
|
||||
"version": "v0.1.5",
|
||||
"version": "v0.1.6",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/antonioribeiro/yaml.git",
|
||||
"reference": "cc27a84e8d9760d82014596ef155c34c816c4ea5"
|
||||
"reference": "c07aef85a0b5ee1fb9f545f974210fd6cfdd63f0"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/antonioribeiro/yaml/zipball/cc27a84e8d9760d82014596ef155c34c816c4ea5",
|
||||
"reference": "cc27a84e8d9760d82014596ef155c34c816c4ea5",
|
||||
"url": "https://api.github.com/repos/antonioribeiro/yaml/zipball/c07aef85a0b5ee1fb9f545f974210fd6cfdd63f0",
|
||||
"reference": "c07aef85a0b5ee1fb9f545f974210fd6cfdd63f0",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"laravel/framework": "^5.5",
|
||||
"laravel/framework": ">=5.5.33",
|
||||
"php": "^7.0",
|
||||
"symfony/yaml": "^3.3"
|
||||
"symfony/yaml": "^3.3|^4.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"orchestra/testbench": "^3.5",
|
||||
"phpunit/phpunit": "^6.4"
|
||||
"orchestra/testbench": "3.5|^3.6",
|
||||
"phpunit/phpunit": "^6.4|^4.0"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
@ -2314,7 +2365,7 @@
|
||||
"laravel",
|
||||
"yaml"
|
||||
],
|
||||
"time": "2017-12-19T02:25:46+00:00"
|
||||
"time": "2018-02-10T17:38:21+00:00"
|
||||
},
|
||||
{
|
||||
"name": "prettus/l5-repository",
|
||||
@ -4145,20 +4196,20 @@
|
||||
},
|
||||
{
|
||||
"name": "symfony/yaml",
|
||||
"version": "v3.4.4",
|
||||
"version": "v4.0.4",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/symfony/yaml.git",
|
||||
"reference": "eab73b6c21d27ae4cd037c417618dfd4befb0bfe"
|
||||
"reference": "ffc60bda1d4a00ec0b32eeabf39dc017bf480028"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/symfony/yaml/zipball/eab73b6c21d27ae4cd037c417618dfd4befb0bfe",
|
||||
"reference": "eab73b6c21d27ae4cd037c417618dfd4befb0bfe",
|
||||
"url": "https://api.github.com/repos/symfony/yaml/zipball/ffc60bda1d4a00ec0b32eeabf39dc017bf480028",
|
||||
"reference": "ffc60bda1d4a00ec0b32eeabf39dc017bf480028",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": "^5.5.9|>=7.0.8"
|
||||
"php": "^7.1.3"
|
||||
},
|
||||
"conflict": {
|
||||
"symfony/console": "<3.4"
|
||||
@ -4172,7 +4223,7 @@
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "3.4-dev"
|
||||
"dev-master": "4.0-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
@ -4199,7 +4250,7 @@
|
||||
],
|
||||
"description": "Symfony Yaml Component",
|
||||
"homepage": "https://symfony.com",
|
||||
"time": "2018-01-21T19:05:02+00:00"
|
||||
"time": "2018-01-21T19:06:11+00:00"
|
||||
},
|
||||
{
|
||||
"name": "theiconic/php-ga-measurement-protocol",
|
||||
|
@ -9,7 +9,7 @@ build:
|
||||
mode: number
|
||||
length: 6
|
||||
increment_by: 1
|
||||
number: 180209-bcc6be
|
||||
number: 180211-3aa01a
|
||||
git_absorb: git-local
|
||||
git:
|
||||
git-local: 'git rev-parse --verify HEAD'
|
||||
|
@ -47,6 +47,9 @@ class FlightTest extends TestCase
|
||||
$this->assertEquals($flight->dpt_airport_id, $body['dpt_airport_id']);
|
||||
$this->assertEquals($flight->arr_airport_id, $body['arr_airport_id']);
|
||||
|
||||
# Distance conversion
|
||||
$this->assertHasKeys($body['distance'], ['mi', 'nmi', 'km']);
|
||||
|
||||
$this->get('/api/flights/INVALID', self::$auth_headers)
|
||||
->assertStatus(404);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user