Download the PHP package dmytrof/fractal-bundle without Composer

On this page you can find all versions of the php package dmytrof/fractal-bundle. 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 fractal-bundle

DmytrofFractalBundle

This bundle helps you to implement Fractal by League into your Symfony 3/4/5 application

Installation

Step 1: Install the bundle

$ composer require dmytrof/fractal-bundle 

Step 2: Enable the bundle

Symfony 3:
<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Dmytrof\FractalBundle\DmytrofFractalBundle(),
        // ...
    );
}
Symfony 4/5:
<?php
    // config/bundles.php

    return [
        // ...
        Dmytrof\FractalBundle\DmytrofFractalBundle::class => ['all' => true],
    ];

Usage

Read the official Fractal documentation before using this bundle

1.Basic usage

Imagine you have some models Article:

<?php

namespace App\Model;

use App\Model\Author;

class Article
{
    /**
     * @var int
     */
    protected $id;

    /**
     * @var Author
     */
    protected $author;

    /**
     * @var string
     */
    protected $title;

    /**
     * @var string
     */
    protected $body;

    /**
     * @var \DateTime
     */
    protected $publishedAt;

    /**
     * Returns id
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * Sets id
     * @param int|null $id
     * @return $this
     */
    public function setId(?int $id): Article
    {
        $this->id = $id;
        return $this;
    }

    /**
     * Returns author
     * @return Author|null
     */
    public function getAuthor(): ?Author
    {
        return $this->author;
    }

    /**
     * Sets author
     * @param Author|null $author
     * @return Article
     */
    public function setAuthor(?Author $author): Article
    {
        $this->author = $author;
        return $this;
    }

    // ...
    // Other Setters and Getters
    // ...

    /**
     * Returns published at date
     * @return \DateTime|null
     */
    public function getPublishedAt(): ?\DateTime
    {
        return $this->publishedAt;
    }

    /**
     * Sets published at date
     * @param \DateTime|null $publishedAt
     * @return Article
     */
    public function setPublishedAt(?\DateTime $publishedAt): Article
    {
        $this->publishedAt = $publishedAt;
        return $this;
    }
}

and Author:

<?php

namespace App\Model;

class Author
{
    /**
     * @var int
     */
    protected $id;

    /**
     * @var string
     */
    protected $firstName;

    /**
     * @var string
     */
    protected $lastName;

    /**
     * @var string
     */
    protected $email;

    /**
     * Returns id
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * Sets id
     * @param int|null $id
     * @return Author
     */
    public function setId(?int $id): Author
    {
        $this->id = $id;
        return $this;
    }

    // ...
    // Other Getters and Setters
    // ...
}
Create transformers for your entities

AuthorTransformer:

<?php

namespace App\FractalTransformer;

use App\Model\Author;
use Dmytrof\FractalBundle\Transformer\AbstractTransformer;

class AuthorTransformer extends AbstractTransformer
{
    // Model which is handled by this transformer
    protected const SUBJECT_CLASS = Author::class;

    /**
     * @var bool
     */
    protected $showShortInfo = false;

    /**
     * @return bool
     */
    public function isShowShortInfo(): bool
    {
        return $this->showShortInfo;
    }

    /**
     * Sets show short info
     * @param bool $showShortInfo
     * @return AuthorTransformer
     */
    public function setShowShortInfo(bool $showShortInfo = true): AuthorTransformer
    {
        $this->showShortInfo = $showShortInfo;
        return $this;
    }

    /**
     * Transforms Author to array
     * @param Author $subject
     * @return array
     */
    public function transformSubject($subject): array
    {
        $data = [
            'id' => $subject->getId(),
            'firstName' => $subject->getFirstName(),
            'lastName' => $this->isShowShortInfo() ? substr($subject->getLastName(), 0, 1).'.' : $subject->getLastName(),
        ];

        if (!$this->isShowShortInfo()) {
            $data['email'] = $subject->getEmail();
        }

        return $data;
    }
}

and ArticleTransformer:

<?php

namespace App\FractalTransformer;

use App\Model\Article;
use Dmytrof\FractalBundle\Transformer\AbstractTransformer;
use League\Fractal\Resource\{Item, ResourceInterface};

class ArticleTransformer extends AbstractTransformer
{
    // Model which is handled by this transformer
    protected const SUBJECT_CLASS = Article::class;

    protected $defaultIncludes = [
        'author'
    ];

    protected $availableIncludes = [
        'body',
    ];

    /**
     * Transforms Article to array
     * @param Article $subject
     * @return array
     */
    public function transformSubject($subject): array
    {
        return [
            'id' => $subject->getId(),
            'title' => $subject->getTitle(),
            'publishedAt' => $this->transformDateTime($subject->getPublishedAt()),
        ];
    }

    /**
     * Includes author
     * @param Article $article
     * @return Item
     */
    public function includeAuthor(Article $article): Item
    {
        return $this->item($article->getAuthor(), AuthorTransformer::class);
    }

    /**
     * Includes body
     * @param Article $article
     * @return ResourceInterface
     */
    public function includeBody(Article $article): ResourceInterface
    {
        return $this->primitive($article->getBody());
    }
}
Update controller
<?php

namespace App\Controller\Api\v1;

use App\Model\Article;
use App\FractalTransformer\ArticleTransformer;
use Dmytrof\FractalBundle\Service\FractalManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\{Request, JsonResponse};

/**
 * @Route("/api/v2/articles")
 */
class ArticleController extends AbstractController
{
    /**
     * @Route("/{id<\d+>}", methods={"GET"})
     */
    public function getOne(int $id, Request $request, FractalManager $fractalManager)
    {
        $model = $this->getArticeById($id); // Some method to receive model

        $fractalManager
            ->parseIncludes($request->get('include', ''))
            ->parseExcludes($request->get('exclude', ''))
            ->parseFieldsets($request->get('fieldsets', []))
        ;

        return new JsonResponse([
            'data' => $fractalManager->getModelData($model, ArticleTransformer::class),
        ], JsonResponse::HTTP_OK);
    }
}

All versions of fractal-bundle with dependencies

PHP Build Version
Package Version
Requires php Version ^7.2 || ^8.0
ext-json Version *
league/fractal Version ^0.18
symfony/framework-bundle Version ^3.4 || ^4.3 || ^5.0
doctrine/collections Version ^1.6
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 dmytrof/fractal-bundle contains the following files

Loading the files please wait ....