nilportugues / sql-repository

[PHP 7] SQL Repository implementation
http://nilportugues.com
MIT License
36 stars 3 forks source link

Add RepositoryHydratorTrait #6

Closed nilportugues closed 8 years ago

nilportugues commented 8 years ago

Use this trait to create repositories with hydratation.

Usage:

<?php 
class UserRepository extends SqlRepository
{
    use RepositoryHydrator;
}

Now querying will return User objects instead of arrays with user data.

Code:

<?php
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Fields;
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Filter;
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Identity;
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Pageable;
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Sort;
use NilPortugues\Foundation\Domain\Model\Repository\Page;

trait RepositoryHydrator
{
    /**
     * {@inheritdoc}
     */
    public function find(Identity $id, Fields $fields = null)
    {
        $result = parent::find($id, $fields);

        return ($result) ? $this->mapping->fromArray($result) : [];
    }

    /**
     * {@inheritdoc}
     */
    public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null)
    {
        $results = parent::findBy($filter, $sort, $fields);

        return ($results) ? $this->hydrateArray($results) : [];
    }

    /**
     * @param array $results
     *
     * @return array
     */
    protected function hydrateArray(array $results)
    {
        return array_map(function ($result) {
            return $this->mapping->fromArray($result);
        }, $results);
    }

    /**
     * {@inheritdoc}
     */
    public function findAll(Pageable $pageable = null)
    {
        $page = parent::findAll($pageable);

        return new Page(
            ($page->content()) ? $this->hydrateArray($page->content()) : [],
            $page->totalElements(),
            $page->pageNumber(),
            $page->totalPages(),
            $page->sortings(),
            $page->filters(),
            $page->fields()
        );
    }

    /**
     * {@inheritdoc}
     */
    public function findByDistinct(Fields $distinctFields, Filter $filter = null, Sort $sort = null)
    {
        $results = parent::findByDistinct($distinctFields, $filter, $sort);

        return ($results) ? $this->hydrateArray($results) : [];
    }
}
nilportugues commented 8 years ago

https://github.com/PHPRepository/php-sql-repository/commit/bff06bf298d4490bc31adee9885716154fca4c6c