Download the PHP package spatie/fractalistic without Composer

On this page you can find all versions of the php package spatie/fractalistic. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package fractalistic

A developer friendly wrapper around Fractal

Latest Version on Packagist GitHub Workflow Status Quality Score Total Downloads

Fractal is an amazing package to transform data before using it in an API. Unfortunately working with Fractal can be a bit verbose.

Using Fractal data can be transformed like this:

This package makes that process a tad easier:

There's also a very short syntax available to quickly transform data:

If you want to use this package inside Laravel, it's recommend to use laravel-fractal instead. That package contains a few more whistles and bells specifically targetted at Laravel users.

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Install

You can pull in the package via composer:

Usage

In the following examples were going to use the following array as example input:

But know that any structure that can be looped (for instance a collection) can be used.

Let's start with a simple transformation.

This will return:

In all following examples it's assumed that you imported the Spatie\Fractalistic\Fractal at the top of your php file.

Instead of using a closure you can also pass a Transformer:

You can also pass the classname of the Transformer:

To make your code a bit shorter you could also pass the transform closure, class, or classname as a second parameter of the collection-method:

Want to get some sweet json output instead of an array? No problem!

A single item can also be transformed:

Using a serializer

Let's take a look again at the output of the first example:

Notice that data-key? That's part of Fractal's default behaviour. Take a look at Fractals's documentation on serializers to find out why that happens.

If you want to use another serializer you can specify one with the serializeWith-method. The Spatie\Fractalistic\ArraySerializer comes out of the box. It removes the data namespace for both collections and items.

You can also pass the serializer classname instead of an instantiation:

Changing the default serializer

You can change the default serializer by providing the classname or an instantiation of your favorite serializer in the config file.

Using includes

Fractal provides support for optionally including data on the relationships for the data you're exporting. You can use Fractal's parseIncludes which accepts a string or an array:

To improve readablity you can also use a function named include followed by the name of the include you want to... include:

Using excludes

Similar to includes Fractal also provides support for optionally excluding data on the relationships for the data you're exporting. You can use Fractal's parseExcludes which accepts a string or an array:

To improve readability you can also use a function named exclude followed by the name of the include you want to... exclude:

Including meta data

Fractal has support for including meta data. You can use addMeta which accepts one or more arrays:

This will return the following array:

Using pagination

Fractal provides a Laravel-specific paginator, IlluminatePaginatorAdapter, which accepts an instance of Laravel's LengthAwarePaginator and works with paginated Eloquent results. When using some serializers, such as the JsonApiSerializer, pagination data can be automatically generated and included in the result set:

Using a cursor

Fractal provides a simple cursor class, League\Fractal\Pagination\Cursor. You can use any other cursor class as long as it implements the League\Fractal\Pagination\CursorInterface interface. When using it, the cursor information will be automatically included in the result metadata:

Setting a custom resource name

Certain serializers wrap the array output with a data element. The name of this element can be customized:

Limit recursion

To increase or decrease the level of embedded includes you can use limitRecursion.

If you do not call limitRecursion a default value of 10 is used.

Quickly transform data with the short function syntax

You can also pass arguments to the fractal-function itself. The first arguments should be the data you which to transform. The second one should be a transformer or a closure that will be used to transform the data. The third one should be a serializer.

Here are some examples

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

License

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


All versions of fractalistic with dependencies

PHP Build Version
Package Version
Requires php Version ^7.4|^8.0
league/fractal Version ^0.20.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package spatie/fractalistic contains the following files

Loading the files please wait ....