Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

arrilot/collectors

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages