Experimental transformers

This commit is contained in:
snipe 2017-01-12 23:40:35 -08:00
parent ca2fa3dab9
commit 6d7c226acd
2 changed files with 70 additions and 0 deletions

View file

@ -0,0 +1,34 @@
<?php
namespace App\Http\Transformers;
use App\Models\Location;
use Illuminate\Database\Eloquent\Collection;
class LocationsTransformer
{
public function transformLocations( Collection $locations)
{
$locations_array = array();
foreach ($locations as $location) {
$locations_array[] = self::transformLocation($locations);
}
return $users_array;
}
public function transformLocation(Location $location = null)
{
if ($location) {
$locations_array[] = [
'id' => e($location->id),
'name' => e($location->name)
];
return $locations_array;
}
}
}

View file

@ -0,0 +1,36 @@
<?php
namespace App\Http\Transformers;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
class UsersTransformer
{
public function transformUsers( Collection $users)
{
$users_array = array();
foreach ($users as $user) {
$users_array[] = self::transformUser($user);
}
return $users_array;
}
public function transformUser(User $user)
{
$user_array[] = [
'id' => e($user->id),
'first_name' => e($user->first_name),
'last_name' => e($user->last_name),
'location' => (new LocationsTransformer)->transformLocation($user->userloc),
'permissions' => $user->decodePermissions(),
'created_at' => $user->created_at,
'updated_at' => $user->updated_at,
];
return $user_array;
}
}