fractal

A presentation and transformation layer for complex PHP API output

Library
Composer
v0.21
3,544stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
50/100Fair
Development Activity0
Maintenance20
Community80
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
83/100Excellent
Architecture86
Code Quality84
Innovation80
Learning Curve82

Fractal provides a presentation and transformation layer for complex data output, the kind found in RESTful APIs, and works especially well with JSON. Think of it as a view layer for your API responses: instead of grabbing rows from the database and passing them straight to json_encode(), you route them through transformers that give you consistent, well-typed, versionable output.

Part of The PHP League, Fractal creates a protective shield between your source data and what clients see, handles embedding of related resources, supports pagination, and works with output standards like JSON:API and HAL as well as fully custom serializers.

What You Get

  • Transformer classes that map source records to clean, consistent output arrays
  • Resource types for single items, collections, primitives, and null resources
  • Pluggable serializers including a JSON:API serializer and custom serializer support
  • Includes/embedding for nesting related resources and paginator adapters for common frameworks

Common Use Cases

  • Building consistent, versionable JSON responses for public REST APIs
  • Decoupling API output from database schema so internal changes don’t break clients
  • Embedding related resources on demand and paginating large collections

Under The Hood

Architecture - The Manager is the entry point: you create a resource (Item, Collection, Primitive, or NullResource from src/Resource) bound to a Transformer, then call createData() to build a Scope. Scopes handle nesting and the requested includes tree, while a Serializer (src/Serializer) turns the transformed data into the final array shape. A ScopeFactory decouples scope creation, and Pagination adapters normalize paginators from several frameworks behind a PaginatorInterface.

Tech Stack - Pure PHP 7.4+ with no required runtime dependencies; framework paginators (Illuminate, Doctrine, Pagerfanta, Laminas) are optional dev/suggested packages. Distributed via Composer under the League\Fractal namespace, with php-cs-fixer, PHPStan, and Psalm in the dev toolchain.

Code Quality - A mature, well-structured codebase built on interfaces (ResourceInterface, SerializerInterface, PaginatorInterface) that keep the design extensible. Roughly 35 test files with PHPUnit and Mockery cover the core, though recent commit activity is low as the library is stable and feature-complete.

API Design - The developer experience is clean and well documented on fractal.thephpleague.com: define a transformer, wrap data in a resource, choose a serializer, and read the output. Include definitions and default includes make embedding intuitive, and swapping serializers to target JSON:API or a custom format is a one-line change.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search