PHP code example of joshbrw / entity-transformers

1. Go to this page and download the library: Download joshbrw/entity-transformers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

joshbrw / entity-transformers example snippets




use Joshbrw\EntityTransfomers\EntityTransformer;

class UserTransformer extends EntityTransformer
{

    /**
     * Transform a single Entity into an array
     * @param mixed $entity Entity instance
     * @return array
     */
    public function transform($entity): array
    {
        return [
            'id' => $entity->id,
            'title' => $entity->title,
            'first_name' => $entity->first_name,
            'last_name' => $entity->last_name,
            'email' => $entity->email,
            'permissions' => $entity->permissions,
            'age' => $this->getTransformationFlag('showAge') === true ? $entity->age : null
        ];
    }
}

$transformer = new UserTransformer;
$response = $transformer->transform($user);

$users = new \Illuminate\Support\Collection($data);
$transformer = new UserTransformer;
$response = $transformer->transform($users);

$transformer = new UserTransformer;
$transformer->setTransformationFlag('showAge', true);
$response = $transformer->transform($user);