2017 © Pedro Peláez
 

yii2-extension yii2-data-provider

Data provider for yii2

image

zabachok/yii2-data-provider

Data provider for yii2

  • Tuesday, May 15, 2018
  • by zabachok
  • Repository
  • 1 Watchers
  • 0 Stars
  • 21 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 11 % Grown

The README.md

Yii2 Data Provider

Library for getting data from repository by filters., (*1)

Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

php composer.phar require zabachok/yii2-data-provider "*"

or add, (*4)

"zabachok/yii2-data-provider": "*"

to the require section of your composer.json file., (*5)

Examples

Repository

namespace app\repositories\statistic;

use app\models\statistic\StatisticExportRequest;
use app\repositories\statistic\dto\RequestDto;
use zabachok\dataProvider\IFilterRepository;

class StatisticRepository implements IStatisticRepository, IFilterRepository
{
    /**
     * @inheritdoc
     * @return RequestDto[]
     */
    public function getRecordsByFilters(array $filters, int $pageSize): array
    {
        $query = StatisticExportRequest::find()
            ->limit($pageSize);

        foreach ($filters as $filter) {
            $filter->setFilter($query);
        }

        $models = $query->all();

        return $this->getRequestDtos($models);
    }

    /**
     * @inheritdoc
     */
    public function getTotalByFilters(array $filters): int
    {
        $query = StatisticExportRequest::find();

        foreach ($filters as $filter) {
            $filter->setFilter($query);
        }

        return $query->count();
    }

    ...

Enum

namespace app\enums\statistic;

use app\filters\statistic\CreatedAtOrderFilter;
use zabachok\dataProvider\filters\PaginationFilter;
use zabachok\dataProvider\IFilterEnum;

class FiltersEnum implements IFilterEnum
{
    public const
        PAGINATION_FILTER = PaginationFilter::class,
        CREATED_AT_ORDER_FILTER = CreatedAtOrderFilter::class;

    /**
     * @inheritdoc
     */
    public function getFiltersForRecords(): array
    {
        return [
            self::PAGINATION_FILTER,
            self::CREATED_AT_ORDER_FILTER,
        ];
    }

    /**
     * @inheritdoc
     */
    public function getFiltersForTotal(): array
    {
        return [
            self::CREATED_AT_ORDER_FILTER,
        ];
    }
}

Service

namespace app\services\statistic;

use app\builders\IBuilder;
use app\builders\statistic\IndexBuilder;
use app\components\enums\statistic\FiltersEnum;
use app\forms\statistic\IndexForm;
use app\repositories\statistic\IStatisticRepository;
use app\repositories\statistic\StatisticRepository;
use app\services\IService;
use Yii;
use yii\base\Model;
use zabachok\dataProvider\DataProvider;

class IndexService implements IService
{
    /**
     * @var DataProvider
     */
    private $dataProvider;

    /**
     * @var IndexBuilder
     */
    private $builder;

    /**
     * IndexService constructor.
     * @param StatisticRepository $repository
     * @param FiltersEnum $filtersEnum
     * @param IndexBuilder $builder
     */
    public function __construct(
        StatisticRepository $repository,
        FiltersEnum $filtersEnum,
        IndexBuilder $builder
    ) {
        $this->dataProvider = Yii::$container->get(DataProvider::class, [$filtersEnum, $repository]);
        $this->builder = $builder;
    }

    /**
     * @param Model|IndexForm $form
     * @return IBuilder
     */
    public function behave(Model $form): IBuilder
    {
        $this->dataProvider->setForm($form->toArray());

        $this->builder
            ->addItems(
                $this->dataProvider->getRecords()
            )
            ->setTotal(
                $this->dataProvider->getTotal()
            );

        return $this->builder;
    }
}

The Versions

15/05 2018

dev-master

9999999-dev

Data provider for yii2

  Sources   Download

MIT

The Requires

 

database filter data provider

14/05 2018

1.0.0

1.0.0.0

Data provider for yii2

  Sources   Download

MIT

The Requires

 

database filter data provider