ldc-legaulois/app/Http/Resources/BreederMapCollection.php
2021-04-06 16:08:47 +02:00

70 lines
2.3 KiB
PHP

<?php
namespace App\Http\Resources;
use App\Enums\DepartaimantCurrent;
use App\Enums\OCBType;
use App\Models\BreedersSites;
use BadMethodCallException;
use Exception;
use Illuminate\Http\Resources\Json\ResourceCollection;
class BreederMapCollection extends ResourceCollection
{
/**
* The resource that this resource collects.
*
* @var string
*/
public $collects = BreedersSites::class;
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return $this->collection->map(function ($breederSite) {
$departaimant = false;
// try to get the departement
try {
$departaimant = DepartaimantCurrent::make(substr($breederSite->breeder->postal_code, 0, 2))
->label;
} catch (BadMethodCallException $e) {
$departaimant = false;
}
$ocbType = '';
switch ($breederSite->ocbType->value) {
case OCBType::poultry()->value:
$ocbType = trans('Éleveur de volaille');
break;
case OCBType::egg()->value:
$ocbType = trans('Producteur d\'oeufs');
break;
case OCBType::rabbit()->value:
$ocbType = trans('Éleveur de lapins');
break;
default:
throw new Exception(sprintf(
'Unhandled OCB type %s',
$breederSite->ocbType->value
));
}
return [
'id' => $breederSite->breeder->id,
'latitude' => $breederSite->latitude,
'longitude' => $breederSite->longitude,
'firstname' => $breederSite->breeder->first_name,
'postal' => $breederSite->breeder->postal_code,
'type' => $ocbType,
'departaimant' => $departaimant,
'testimony' => $breederSite->breeder->testimony,
'place' => $breederSite->place,
'picture' => $breederSite->picture_breeder
];
})->all();
}
}