Convert Figma logo to code with AI

thephpleague logofractal

Output complex, flexible, AJAX/RESTful data structures.

3,526
351
3,526
58

Top Related Projects

An easy to use Fractal wrapper built for Laravel and Lumen applications

9,325

A RESTful API package for the Laravel and Lumen frameworks.

Quick Overview

Fractal is a PHP package that provides a presentation and transformation layer for complex data output, particularly useful for RESTful APIs. It allows developers to easily transform database records and other raw data into consistent, well-structured API responses.

Pros

  • Flexible and customizable data transformation
  • Supports nested relationships and includes
  • Integrates well with popular PHP frameworks like Laravel and Symfony
  • Helps maintain clean and consistent API responses

Cons

  • Learning curve for complex transformations
  • Can add overhead for simple data structures
  • Limited built-in caching mechanisms
  • Requires manual setup for pagination

Code Examples

  1. Basic transformation:
$transformer = new BookTransformer();
$resource = new Item($book, $transformer);
$fractal = new Manager();
$data = $fractal->createData($resource)->toArray();

This example transforms a single book object into an array using a custom transformer.

  1. Including relationships:
$resource = new Item($book, new BookTransformer());
$resource->setIncludes(['author', 'publisher']);
$fractal = new Manager();
$data = $fractal->createData($resource)->toArray();

This code includes related author and publisher data in the book transformation.

  1. Collection transformation with pagination:
$resource = new Collection($books, new BookTransformer());
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
$fractal = new Manager();
$data = $fractal->createData($resource)->toArray();

This example transforms a collection of books with pagination information.

Getting Started

  1. Install Fractal via Composer:

    composer require league/fractal
    
  2. Create a transformer class:

    use League\Fractal\TransformerAbstract;
    
    class BookTransformer extends TransformerAbstract
    {
        public function transform(Book $book)
        {
            return [
                'id'    => (int) $book->id,
                'title' => $book->title,
                'author' => $book->author_name,
            ];
        }
    }
    
  3. Use the transformer in your code:

    $resource = new Item($book, new BookTransformer());
    $fractal = new Manager();
    $data = $fractal->createData($resource)->toArray();
    

This setup allows you to start transforming your data objects into consistent API responses using Fractal.

Competitor Comparisons

An easy to use Fractal wrapper built for Laravel and Lumen applications

Pros of laravel-fractal

  • Seamless integration with Laravel, including Facades and Service Providers
  • Built-in support for Laravel's pagination
  • Simplified API with Laravel-specific helper methods

Cons of laravel-fractal

  • Limited to Laravel framework, less flexible for other PHP projects
  • Fewer customization options compared to the more generic fractal package
  • Smaller community and potentially slower updates

Code Comparison

fractal:

$resource = new Item($book, new BookTransformer);
$manager = new Manager();
$manager->createData($resource)->toArray();

laravel-fractal:

fractal()
    ->item($book)
    ->transformWith(new BookTransformer)
    ->toArray();

Key Differences

  • laravel-fractal provides a more concise syntax for Laravel applications
  • fractal offers greater flexibility and can be used in any PHP project
  • laravel-fractal leverages Laravel's dependency injection and configuration system
  • fractal has a larger community and more frequent updates
  • laravel-fractal includes Laravel-specific features like pagination support out of the box

Both packages are based on the Fractal specification, but laravel-fractal is tailored specifically for Laravel applications, while fractal is a more general-purpose implementation suitable for any PHP project.

9,325

A RESTful API package for the Laravel and Lumen frameworks.

Pros of dingo/api

  • More comprehensive API solution, offering routing, rate limiting, and authentication out of the box
  • Built-in support for API versioning, allowing easier management of multiple API versions
  • Includes OAuth2 server implementation for secure API authentication

Cons of dingo/api

  • Steeper learning curve due to its more complex feature set
  • Less flexibility in data transformation compared to Fractal's layered approach
  • Potentially overkill for simpler API projects that don't require all features

Code Comparison

dingo/api:

$api = app('Dingo\Api\Routing\Router');

$api->version('v1', function ($api) {
    $api->get('users', 'App\Http\Controllers\UserController@index');
});

fractal:

$fractal = new Manager();
$resource = new Item($user, new UserTransformer);
$array = $fractal->createData($resource)->toArray();

Summary

dingo/api offers a more comprehensive API solution with built-in features like routing, versioning, and authentication. It's ideal for complex API projects but may be overkill for simpler ones. fractal, on the other hand, focuses primarily on data transformation and serialization, providing more flexibility in this area. The choice between the two depends on the specific needs of your project and the level of control you require over API functionality.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Fractal

Latest Version Software License The PHP League Tests Total Downloads

Fractal provides a presentation and transformation layer for complex data output, the like found in RESTful APIs, and works really well with JSON. Think of this as a view layer for your JSON/YAML/etc.

When building an API it is common for people to just grab stuff from the database and pass it to json_encode(). This might be passable for "trivial" APIs but if they are in use by the public, or used by mobile applications then this will quickly lead to inconsistent output.

Goals

  • Create a protective shield between source data and output, so schema changes do not affect users
  • Systematic type-casting of data, to avoid foreach()ing through and (bool)ing everything
  • Include (a.k.a embedding, nesting or side-loading) relationships for complex data structures
  • Work with standards like HAL and JSON-API but also allow custom serialization
  • Support the pagination of data results, for small and large data sets alike
  • Generally ease the subtle complexities of outputting data in a non-trivial API

This package is compliant with PSR-1, PSR-2 and PSR-4. If you notice compliance oversights, please send a patch via pull request.

Install

Via Composer

$ composer require league/fractal

Requirements

The following versions of PHP are supported by this version:

>= PHP 7.4

Documentation

Fractal has full documentation, powered by Jekyll.

Contribute to this documentation in the gh-pages branch.

Todo

  • add HAL serializers

Testing

$ phpunit

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Maintainers

Credits

License

The MIT License (MIT). Please see License File for more information.