2018-01-24 11:40:34 +08:00
|
|
|
<?php
|
|
|
|
|
2020-03-23 21:31:35 +08:00
|
|
|
namespace App\Contracts;
|
2018-01-24 11:40:34 +08:00
|
|
|
|
2020-05-16 06:20:16 +08:00
|
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
|
2018-01-24 11:40:34 +08:00
|
|
|
/**
|
2020-03-23 21:31:35 +08:00
|
|
|
* Base class for a resource/response
|
2018-01-24 11:40:34 +08:00
|
|
|
*/
|
2020-05-16 06:20:16 +08:00
|
|
|
class Resource extends JsonResource
|
2018-01-24 11:40:34 +08:00
|
|
|
{
|
2019-07-16 03:14:40 +08:00
|
|
|
/**
|
|
|
|
* Iterate through the list of $fields and check if they're a "Unit"
|
|
|
|
* If they are, then add the response
|
|
|
|
*
|
|
|
|
* @param $response
|
|
|
|
* @param array $fields
|
|
|
|
*/
|
|
|
|
public function checkUnitFields(&$response, array $fields): void
|
|
|
|
{
|
|
|
|
foreach ($fields as $f) {
|
|
|
|
if ($this->{$f} instanceof Unit) {
|
|
|
|
$response[$f] = $this->{$f}->getResponseUnits();
|
|
|
|
} else {
|
|
|
|
$response[$f] = $this->{$f};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 11:40:34 +08:00
|
|
|
}
|