giadc/doctrine-json-api

A package for handling JSON API requests with Doctrine

3.0.0 2020-11-17 16:08 UTC

This package is auto-updated.

Last update: 2021-11-20 20:32:58 UTC


README

A package for automating pagination, filtering, sorting, and includes when working with Doctrine and the JSON API standard.

Installation

composer install giadc/doctrine-json-api

Basic Usage

Using the Read Service

$entityReadService->findById('id123', $includes = []);
$entityReadService->findByArray(['id123', 'id456'], 'id', $includes = []);
$entityReadService->findByField('name', 'Chiquita');
$entityReadService->paginate($includes = []);
<?php
namespace App\Bananas\Services;

use App\Common\Services\AbstractReadService;
use App\Bananas\Repositories\BananaRepositoryInterface as BananaRepo;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class RoleReadService extends AbstractReadService
{
    public function __construct(
        BananaRepo $bananaRepo
    ) {
        $this->initialize($bananaRepo, 'Banana');
    }

Filters Skeleton

<?php

namespace App\Bananas\Filters;

use Giadc\DoctrineJsonApi\Filters\FilterManager;

/**
 * Class BananaFilters
 */
class BananaFilter extends FilterManager
{
    /**
     * @var array
     */
    protected $accepted = [
        'id'    => ['type' => 'id'],      // id must match exactly
        'name' => ['type' => 'keyword'], // keyword will match fuzzily
    ];
}

Repository Skeleton

<?php
namespace App\Bananas\Repositories;

use App\Bananas\Banana;
use App\Bananas\Filters\BananaFilter;
use App\Bananas\Repositories\BananaRepositoryInterface;
use Doctrine\ORM\EntityManagerInterface as EntityManager;
use Giadc\DoctrineJsonApi\Interfaces\AbstractJsonApiRepositoryInterface as JsonApiInterface;
use Giadc\DoctrineJsonApi\Repositories\AbstractJsonApiDoctrineRepository as JsonApiRepository;

class RoleDoctrineRepository extends JsonApiRepository implements
JsonApiInterface,
RoleRepositoryInterface
{
    /** @var EntityManager */
    protected $em;

    /** @var string */
    protected $class;
    
    /** @var BananaFilter **/
    protected $filters;

    /**
     * Create a new BananaDoctrineRepository
     *
     * @param EntityManager $em
     * @param BananaFilters $filters
     * @return void
     */
    public function __construct(EntityManager $em, BananaFilter $filters)
    {
        $this->em      = $em;
        $this->class   = Role::class;
        $this->filters = $filters;
    }
}