implements ArrayAccess for casting to type #189

This commit is contained in:
Nabeel Shahzad 2018-02-10 22:04:30 -06:00
parent cb02a7c15e
commit f14a9e3ba1
9 changed files with 126 additions and 17 deletions

View File

@ -12,8 +12,10 @@ class Flight extends Resource
$flight = parent::toArray($request); $flight = parent::toArray($request);
// Return multiple measures so the client can pick what they want // Return multiple measures so the client can pick what they want
if($flight['distance'] instanceof Distance) { if(\in_array('distance', $flight, true)
$flight['distance'] = $flight['distance']->toJson(); && $flight['distance'] instanceof Distance)
{
$flight['distance'] = $flight['distance']->toObject();
} }
$flight['airline'] = new Airline($this->airline); $flight['airline'] = new Airline($this->airline);

View File

@ -2,6 +2,7 @@
namespace App\Http\Resources; namespace App\Http\Resources;
use App\Support\Units\Distance;
use Illuminate\Http\Resources\Json\Resource; use Illuminate\Http\Resources\Json\Resource;
class Pirep extends Resource class Pirep extends Resource
@ -16,6 +17,18 @@ class Pirep extends Resource
{ {
$pirep = parent::toArray($request); $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['airline'] = new Airline($this->airline);
$pirep['dpt_airport'] = new Airport($this->dpt_airport); $pirep['dpt_airport'] = new Airport($this->dpt_airport);
$pirep['arr_airport'] = new Airport($this->arr_airport); $pirep['arr_airport'] = new Airport($this->arr_airport);

View File

@ -110,7 +110,7 @@ class Pirep extends BaseModel
public function getDistanceAttribute() public function getDistanceAttribute()
{ {
try { try {
$distance = (float)$this->attributes['distance']; $distance = (float) $this->attributes['distance'];
return new Distance($distance, 'mi'); return new Distance($distance, 'mi');
} catch (NonNumericValue $e) { } catch (NonNumericValue $e) {
return 0; 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 the planned_distance in a converter class
* @return int|Distance * @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 * Do some cleanup on the route
* @param $route * @param $route

View File

@ -1,12 +1,13 @@
<?php <?php
namespace App\Support\Units; namespace App\Support\Units;
use Illuminate\Contracts\Support\Arrayable;
/** /**
* Wrap the converter class * Wrap the converter class
* @package App\Support\Units * @package App\Support\Units
*/ */
class Altitude extends \PhpUnitsOfMeasure\PhysicalQuantity\Length class Altitude extends \PhpUnitsOfMeasure\PhysicalQuantity\Length implements Arrayable
{ {
/** /**
* The unit that this is stored as * The unit that this is stored as
@ -26,11 +27,19 @@ class Altitude extends \PhpUnitsOfMeasure\PhysicalQuantity\Length
/** /**
* For the HTTP Resource call * For the HTTP Resource call
*/ */
public function toJson() public function toObject()
{ {
return [ return [
'ft' => round($this->toUnit('feet'), 2), 'ft' => round($this->toUnit('feet'), 2),
'm' => round($this->toUnit('meters') / 1000, 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);
}
} }

View File

@ -1,12 +1,13 @@
<?php <?php
namespace App\Support\Units; namespace App\Support\Units;
use Illuminate\Contracts\Support\Arrayable;
/** /**
* Wrap the converter class * Wrap the converter class
* @package App\Support\Units * @package App\Support\Units
*/ */
class Distance extends \PhpUnitsOfMeasure\PhysicalQuantity\Length class Distance extends \PhpUnitsOfMeasure\PhysicalQuantity\Length implements Arrayable
{ {
/** /**
* The unit that this is stored as * The unit that this is stored as
@ -26,7 +27,7 @@ class Distance extends \PhpUnitsOfMeasure\PhysicalQuantity\Length
/** /**
* For the HTTP Resource call * For the HTTP Resource call
*/ */
public function toJson() public function toObject(): array
{ {
return [ return [
'mi' => round($this->toUnit('miles'), 2), 'mi' => round($this->toUnit('miles'), 2),
@ -34,4 +35,12 @@ class Distance extends \PhpUnitsOfMeasure\PhysicalQuantity\Length
'km' => round($this->toUnit('meters') / 1000, 2), 'km' => round($this->toUnit('meters') / 1000, 2),
]; ];
} }
/**
* Get the instance as an array.
*/
public function toArray()
{
return round($this->toUnit(self::STORAGE_UNIT), 2);
}
} }

View File

@ -1,13 +1,19 @@
<?php <?php
namespace App\Support\Units; namespace App\Support\Units;
use Illuminate\Contracts\Support\Arrayable;
/** /**
* Class Mass * Class Mass
* @package App\Support\Units * @package App\Support\Units
*/ */
class Mass extends \PhpUnitsOfMeasure\PhysicalQuantity\Mass class Mass extends \PhpUnitsOfMeasure\PhysicalQuantity\Mass implements Arrayable
{ {
/**
* The unit this is stored as
*/
public const STORAGE_UNIT = 'lbs';
/** /**
* @return string * @return string
*/ */
@ -15,17 +21,26 @@ class Mass extends \PhpUnitsOfMeasure\PhysicalQuantity\Mass
{ {
$unit = setting('general.weight_unit'); $unit = setting('general.weight_unit');
$value = $this->toUnit($unit); $value = $this->toUnit($unit);
return (string)round($value, 2); return (string) round($value, 2);
} }
/** /**
* For the HTTP Resource call * For the HTTP Resource call
*/ */
public function toJson() public function toObject()
{ {
return [ return [
'kg' => round($this->toUnit('kg'), 2), '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);
}
} }

View File

@ -1,12 +1,13 @@
<?php <?php
namespace App\Support\Units; namespace App\Support\Units;
use Illuminate\Contracts\Support\Arrayable;
/** /**
* Class Time * Class Time
* @package App\Support\Units * @package App\Support\Units
*/ */
class Time class Time implements Arrayable
{ {
public $hours, public $hours,
$minutes; $minutes;
@ -44,7 +45,7 @@ class Time
*/ */
public function asInt() public function asInt()
{ {
return $this->getTotalMinutes(); return $this->getMinutes();
} }
/** /**
@ -55,4 +56,20 @@ class Time
{ {
return $this->hours . 'h ' . $this->minutes . 'm'; 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();
}
} }

View File

@ -1,12 +1,13 @@
<?php <?php
namespace App\Support\Units; namespace App\Support\Units;
use Illuminate\Contracts\Support\Arrayable;
/** /**
* Class Velocity * Class Velocity
* @package App\Support\Units * @package App\Support\Units
*/ */
class Velocity extends \PhpUnitsOfMeasure\PhysicalQuantity\Velocity class Velocity extends \PhpUnitsOfMeasure\PhysicalQuantity\Velocity implements Arrayable
{ {
/** /**
* The unit that this is stored as * The unit that this is stored as
@ -26,11 +27,19 @@ class Velocity extends \PhpUnitsOfMeasure\PhysicalQuantity\Velocity
/** /**
* For the HTTP Resource call * For the HTTP Resource call
*/ */
public function toJson() public function toObject()
{ {
return [ return [
'knot' => round($this->toUnit('knot'), 2), 'knot' => round($this->toUnit('knot'), 2),
'km/h' => round($this->toUnit('km/h'), 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);
}
} }

View File

@ -1,12 +1,13 @@
<?php <?php
namespace App\Support\Units; namespace App\Support\Units;
use Illuminate\Contracts\Support\Arrayable;
/** /**
* Wrap the converter class * Wrap the converter class
* @package App\Support\Units * @package App\Support\Units
*/ */
class Volume extends \PhpUnitsOfMeasure\PhysicalQuantity\Volume class Volume extends \PhpUnitsOfMeasure\PhysicalQuantity\Volume implements Arrayable
{ {
/** /**
* The unit that this is stored as * The unit that this is stored as
@ -26,11 +27,19 @@ class Volume extends \PhpUnitsOfMeasure\PhysicalQuantity\Volume
/** /**
* For the HTTP Resource call * For the HTTP Resource call
*/ */
public function toJson() public function toObject()
{ {
return [ return [
'gal' => round($this->toUnit('gal'), 2), 'gal' => round($this->toUnit('gal'), 2),
'liters' => round($this->toUnit('liters'), 2), 'liters' => round($this->toUnit('liters'), 2),
]; ];
} }
/**
* Get the instance as an array.
*/
public function toArray()
{
return round($this->toUnit(self::STORAGE_UNIT), 2);
}
} }