From f14a9e3ba1b90a7efe816b998cdcb79ed91f5284 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Sat, 10 Feb 2018 22:04:30 -0600 Subject: [PATCH] implements ArrayAccess for casting to type #189 --- app/Http/Resources/Flight.php | 6 ++++-- app/Http/Resources/Pirep.php | 13 +++++++++++++ app/Models/Pirep.php | 28 +++++++++++++++++++++++++++- app/Support/Units/Altitude.php | 13 +++++++++++-- app/Support/Units/Distance.php | 13 +++++++++++-- app/Support/Units/Mass.php | 23 +++++++++++++++++++---- app/Support/Units/Time.php | 21 +++++++++++++++++++-- app/Support/Units/Velocity.php | 13 +++++++++++-- app/Support/Units/Volume.php | 13 +++++++++++-- 9 files changed, 126 insertions(+), 17 deletions(-) diff --git a/app/Http/Resources/Flight.php b/app/Http/Resources/Flight.php index d40f89aa..99a783a8 100644 --- a/app/Http/Resources/Flight.php +++ b/app/Http/Resources/Flight.php @@ -12,8 +12,10 @@ class Flight extends Resource $flight = parent::toArray($request); // Return multiple measures so the client can pick what they want - if($flight['distance'] instanceof Distance) { - $flight['distance'] = $flight['distance']->toJson(); + if(\in_array('distance', $flight, true) + && $flight['distance'] instanceof Distance) + { + $flight['distance'] = $flight['distance']->toObject(); } $flight['airline'] = new Airline($this->airline); diff --git a/app/Http/Resources/Pirep.php b/app/Http/Resources/Pirep.php index 6393c331..0ffcd038 100644 --- a/app/Http/Resources/Pirep.php +++ b/app/Http/Resources/Pirep.php @@ -2,6 +2,7 @@ namespace App\Http\Resources; +use App\Support\Units\Distance; use Illuminate\Http\Resources\Json\Resource; class Pirep extends Resource @@ -16,6 +17,18 @@ class Pirep extends Resource { $pirep = parent::toArray($request); + if (\in_array('distance', $pirep, true) + && $pirep['distance'] instanceof Distance) + { + $pirep['distance'] = $pirep['distance']->toObject(); + } + + if (\in_array('planned_distance', $pirep, true) + && $pirep['planned_distance'] instanceof Distance) + { + $pirep['planned_distance'] = $pirep['planned_distance']->toObject(); + } + $pirep['airline'] = new Airline($this->airline); $pirep['dpt_airport'] = new Airport($this->dpt_airport); $pirep['arr_airport'] = new Airport($this->arr_airport); diff --git a/app/Models/Pirep.php b/app/Models/Pirep.php index 25d2471a..5ca2e5ec 100644 --- a/app/Models/Pirep.php +++ b/app/Models/Pirep.php @@ -110,7 +110,7 @@ class Pirep extends BaseModel public function getDistanceAttribute() { try { - $distance = (float)$this->attributes['distance']; + $distance = (float) $this->attributes['distance']; return new Distance($distance, 'mi'); } catch (NonNumericValue $e) { return 0; @@ -119,6 +119,19 @@ class Pirep extends BaseModel } } + /** + * Set the distance unit, convert to our internal default unit + * @param $value + */ + public function setDistanceAttribute($value): void + { + if ($value instanceof Distance) { + $this->attributes['distance'] = $value->toUnit(Distance::STORAGE_UNIT); + } else { + $this->attributes['distance'] = $value; + } + } + /** * Return the planned_distance in a converter class * @return int|Distance @@ -135,6 +148,19 @@ class Pirep extends BaseModel } } + /** + * Set the distance unit, convert to our internal default unit + * @param $value + */ + public function setPlannedDistanceAttribute($value) + { + if ($value instanceof Distance) { + $this->attributes['distance'] = $value->toUnit(Distance::STORAGE_UNIT); + } else { + $this->attributes['distance'] = $value; + } + } + /** * Do some cleanup on the route * @param $route diff --git a/app/Support/Units/Altitude.php b/app/Support/Units/Altitude.php index 923d947c..d023f83d 100644 --- a/app/Support/Units/Altitude.php +++ b/app/Support/Units/Altitude.php @@ -1,12 +1,13 @@ round($this->toUnit('feet'), 2), 'm' => round($this->toUnit('meters') / 1000, 2), ]; } + + /** + * Get the instance as an array. + */ + public function toArray() + { + return round($this->toUnit(self::STORAGE_UNIT), 2); + } } diff --git a/app/Support/Units/Distance.php b/app/Support/Units/Distance.php index 56dda72c..b575f508 100644 --- a/app/Support/Units/Distance.php +++ b/app/Support/Units/Distance.php @@ -1,12 +1,13 @@ round($this->toUnit('miles'), 2), @@ -34,4 +35,12 @@ class Distance extends \PhpUnitsOfMeasure\PhysicalQuantity\Length 'km' => round($this->toUnit('meters') / 1000, 2), ]; } + + /** + * Get the instance as an array. + */ + public function toArray() + { + return round($this->toUnit(self::STORAGE_UNIT), 2); + } } diff --git a/app/Support/Units/Mass.php b/app/Support/Units/Mass.php index cc821f66..8a825ae5 100644 --- a/app/Support/Units/Mass.php +++ b/app/Support/Units/Mass.php @@ -1,13 +1,19 @@ toUnit($unit); - return (string)round($value, 2); + return (string) round($value, 2); } /** * For the HTTP Resource call */ - public function toJson() + public function toObject() { return [ 'kg' => round($this->toUnit('kg'), 2), - 'lgs' => round($this->toUnit('lbs'), 2), + 'lbs' => round($this->toUnit('lbs'), 2), ]; } + + /** + * Get the instance as an array. + * @return array + */ + public function toArray() + { + return round($this->toUnit(self::STORAGE_UNIT), 2); + } } diff --git a/app/Support/Units/Time.php b/app/Support/Units/Time.php index 1857111c..66bf2b50 100644 --- a/app/Support/Units/Time.php +++ b/app/Support/Units/Time.php @@ -1,12 +1,13 @@ getTotalMinutes(); + return $this->getMinutes(); } /** @@ -55,4 +56,20 @@ class Time { return $this->hours . 'h ' . $this->minutes . 'm'; } + + /** + * @return float|int + */ + public function toObject() + { + return $this->getMinutes(); + } + + /** + * Get the instance as an array. + */ + public function toArray() + { + return $this->getMinutes(); + } } diff --git a/app/Support/Units/Velocity.php b/app/Support/Units/Velocity.php index f68b1042..ccbe9668 100644 --- a/app/Support/Units/Velocity.php +++ b/app/Support/Units/Velocity.php @@ -1,12 +1,13 @@ round($this->toUnit('knot'), 2), 'km/h' => round($this->toUnit('km/h'), 2), ]; } + + /** + * Get the instance as an array. + */ + public function toArray() + { + return round($this->toUnit(self::STORAGE_UNIT), 2); + } } diff --git a/app/Support/Units/Volume.php b/app/Support/Units/Volume.php index 2baa1802..b43188b0 100644 --- a/app/Support/Units/Volume.php +++ b/app/Support/Units/Volume.php @@ -1,12 +1,13 @@ round($this->toUnit('gal'), 2), 'liters' => round($this->toUnit('liters'), 2), ]; } + + /** + * Get the instance as an array. + */ + public function toArray() + { + return round($this->toUnit(self::STORAGE_UNIT), 2); + } }