PHP code example of samj / fractal-bundle

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

    

samj / fractal-bundle example snippets


public function registerBundles()
{
    return [
        // ...
        new SamJ\FractalBundle\SamJFractalBundle(),
    ];
}

class UserTransformer extends TransformerAbstract
{
    private $authorizationCheker;
    
    public function __construct(AuthorizationChecker $authorizationCheker)
    {
        $this->authorizationCheker = $authorizationCheker;
    }
    
    public function transform(User $user)
    {
        $data = [
            'id' => $user->id(),
            'name' => $user->name(),
        ];
        
        if ($this->authorizationChecker->isGranted(UserVoter::SEE_EMAIL, $user)) {
            $data['email'] = $user->email();
        }
        
        return $data;
    }
}

$resource = new Collection($users, 'app.transformer.user');

public function n $this->collection($user->friends(), 'app.transformer.user');
}