diff --git a/app/Database/factories/FlightFactory.php b/app/Database/factories/FlightFactory.php index ea0b696b..6a47bd9b 100644 --- a/app/Database/factories/FlightFactory.php +++ b/app/Database/factories/FlightFactory.php @@ -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(), diff --git a/app/Database/migrations/2017_06_07_014930_create_settings_table.php b/app/Database/migrations/2017_06_07_014930_create_settings_table.php index d3cfced5..eec845b6 100644 --- a/app/Database/migrations/2017_06_07_014930_create_settings_table.php +++ b/app/Database/migrations/2017_06_07_014930_create_settings_table.php @@ -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', ]); diff --git a/app/Http/Resources/Flight.php b/app/Http/Resources/Flight.php index 4f950c2b..d40f89aa 100644 --- a/app/Http/Resources/Flight.php +++ b/app/Http/Resources/Flight.php @@ -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); diff --git a/app/Models/Flight.php b/app/Models/Flight.php index 56dadc5e..874a5c12 100644 --- a/app/Models/Flight.php +++ b/app/Models/Flight.php @@ -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 */ diff --git a/app/Models/Pirep.php b/app/Models/Pirep.php index 4ae6a410..25d2471a 100644 --- a/app/Models/Pirep.php +++ b/app/Models/Pirep.php @@ -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 diff --git a/app/Support/Units/Altitude.php b/app/Support/Units/Altitude.php new file mode 100644 index 00000000..923d947c --- /dev/null +++ b/app/Support/Units/Altitude.php @@ -0,0 +1,36 @@ +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), + ]; + } +} diff --git a/app/Support/Units/Distance.php b/app/Support/Units/Distance.php new file mode 100644 index 00000000..56dda72c --- /dev/null +++ b/app/Support/Units/Distance.php @@ -0,0 +1,37 @@ +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), + ]; + } +} diff --git a/app/Support/Units/Mass.php b/app/Support/Units/Mass.php new file mode 100644 index 00000000..cc821f66 --- /dev/null +++ b/app/Support/Units/Mass.php @@ -0,0 +1,31 @@ +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), + ]; + } +} diff --git a/app/Support/Units/Velocity.php b/app/Support/Units/Velocity.php new file mode 100644 index 00000000..f68b1042 --- /dev/null +++ b/app/Support/Units/Velocity.php @@ -0,0 +1,36 @@ +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), + ]; + } +} diff --git a/app/Support/Units/Volume.php b/app/Support/Units/Volume.php new file mode 100644 index 00000000..2baa1802 --- /dev/null +++ b/app/Support/Units/Volume.php @@ -0,0 +1,36 @@ +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), + ]; + } +} diff --git a/composer.json b/composer.json index 67d7f24c..4bbb5188 100755 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index 5ae09af8..a612a571 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/config/version.yml b/config/version.yml index 2a12e2d0..2682c1a4 100644 --- a/config/version.yml +++ b/config/version.yml @@ -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' diff --git a/tests/FlightTest.php b/tests/FlightTest.php index 4f813429..d860d8fa 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -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); }