arrilot/collectors

This package is abandoned and no longer maintained. No replacement package was suggested.

0.2.3 2017-12-07 22:00 UTC

This package is auto-updated.

Last update: 2023-01-20 20:29:06 UTC


README

Latest Stable Version Total Downloads Build Status Scrutinizer Quality Score

PHP Collectors (In development)

Introduction

Collectors scan across given fields in items/collections for ids and fetch detailed data from database or another storage

Installation

composer require arrilot/collectors

Usage

First of all you need to create your own collector class.

use Arrilot\Collectors\Collector;

class FooCollector extends Collector
{
    /**
     * Get data for given ids.
     *
     * @param array $ids
     * @return array
     */
    public function getList(array $ids)
    {
        ...
    }
}

Example

    $elements = [
        ['id' => 1, 'files' => 1],
        ['id' => 2, 'files' => [2, 1]],
    ];
    
    $item = [
        'id' => 3,
        'another_files' => 3
    ];
    
    $collector = new FooCollector();
    $collector->scanCollection($elements, 'files');
    $collector->scanItem($item, 'another_files'); 
    // You can also pass several fields as array  - $collector->scanItem($item, ['field_1', 'field_2']);
    
    $files = $collector->performQuery();
    var_dump($files);

    // result
    /*
        array:2 [▼
          1 => array:3 [▼
              "id" => 1
              "name" => "avatar.png",
              "module" => "main",
          ]
          2 => array:3 [▼
              "id" => 2
              "name" => "test.png",
              "module" => "main",
          ],
          3 => array:3 [▼
               "id" => 3
               "name" => "test2.png",
               "module" => "main",
          ],
        ]
    */

You can manually add additional ids if you already know them.

$files = $collector->addIds([626, 277, 23])->performQuery();

You can pass select to getlist like that:

$files = $collector->select(['id', 'name'])->performQuery();
// $this->select is ['id', 'name'] in `->getList()` and you can implement logic handling it.

Same is true for an additional filter.

$collector->where(['active' => 1])->performQuery();
// $this->where is ['active' => 1]

You can use dot notation to locate a field, e.g

$collector->fromItem($item, 'properties.files');

Bridge packages