From 0700c9690148e9109e71918b24b0d3812b62dd2c Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Sat, 10 Feb 2018 22:30:42 -0600 Subject: [PATCH] fix serialization of converter classes #189 --- app/Http/Resources/Flight.php | 6 ++---- app/Http/Resources/Pirep.php | 12 ++++-------- app/Models/Pirep.php | 4 ++-- tests/AcarsTest.php | 2 ++ 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/app/Http/Resources/Flight.php b/app/Http/Resources/Flight.php index 99a783a8..45c59d28 100644 --- a/app/Http/Resources/Flight.php +++ b/app/Http/Resources/Flight.php @@ -12,10 +12,8 @@ class Flight extends Resource $flight = parent::toArray($request); // Return multiple measures so the client can pick what they want - if(\in_array('distance', $flight, true) - && $flight['distance'] instanceof Distance) - { - $flight['distance'] = $flight['distance']->toObject(); + if(filled($this->distance) && $this->distance instanceof Distance) { + $flight['distance'] = $this->distance->toObject(); } $flight['airline'] = new Airline($this->airline); diff --git a/app/Http/Resources/Pirep.php b/app/Http/Resources/Pirep.php index 0ffcd038..aaa498f0 100644 --- a/app/Http/Resources/Pirep.php +++ b/app/Http/Resources/Pirep.php @@ -17,16 +17,12 @@ class Pirep extends Resource { $pirep = parent::toArray($request); - if (\in_array('distance', $pirep, true) - && $pirep['distance'] instanceof Distance) - { - $pirep['distance'] = $pirep['distance']->toObject(); + if (filled($this->distance) && $this->distance instanceof Distance) { + $pirep['distance'] = $this->distance->toObject(); } - if (\in_array('planned_distance', $pirep, true) - && $pirep['planned_distance'] instanceof Distance) - { - $pirep['planned_distance'] = $pirep['planned_distance']->toObject(); + if (filled($this->planned_distance) && $this->planned_distance instanceof Distance) { + $pirep['planned_distance'] = $this->planned_distance->toObject(); } $pirep['airline'] = new Airline($this->airline); diff --git a/app/Models/Pirep.php b/app/Models/Pirep.php index 22036bdc..31b41fc7 100644 --- a/app/Models/Pirep.php +++ b/app/Models/Pirep.php @@ -155,9 +155,9 @@ class Pirep extends BaseModel public function setPlannedDistanceAttribute($value) { if ($value instanceof Distance) { - $this->attributes['distance'] = $value->toUnit(Distance::STORAGE_UNIT); + $this->attributes['planned_distance'] = $value->toUnit(Distance::STORAGE_UNIT); } else { - $this->attributes['distance'] = $value; + $this->attributes['planned_distance'] = $value; } } diff --git a/tests/AcarsTest.php b/tests/AcarsTest.php index 7864023c..715bf979 100644 --- a/tests/AcarsTest.php +++ b/tests/AcarsTest.php @@ -134,6 +134,8 @@ class AcarsTest extends TestCase $this->assertEquals('custom_field', $pirep['fields'][0]['name']); $this->assertEquals('custom_value', $pirep['fields'][0]['value']); + $this->assertHasKeys($pirep['distance'], ['mi', 'nmi', 'km']); + /** * Update the custom field */