PHP code example of fd6130 / fractal-bundle

1. Go to this page and download the library: Download fd6130/fractal-bundle 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/ */

    

fd6130 / fractal-bundle example snippets


class UserTransformer extends TransformerAbstract
{    
    public function transform(User $user): array
    {
        $data = [
            'id' => $user->id(),
            'name' => $user->name(),
        ];
        
        return $data;
    }
}

$resource = new Collection($users, UserTransformer::class);

$response = $manager->createData($resource)->toArray();

class UserTransformer extends TransformerAbstract
{
    private $entityManager;
    
    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }
    
    public function transform(User $user): array
    {
        $data = [
            'id' => $user->id(),
            'name' => $user->name(),
        ];

        // $this->entityManager->getRepository(...)
        
        return $data;
    }
}