arrilot / collectors
Installs: 1 901
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=5.6.0
- illuminate/support: >=5.4
Requires (Dev)
- phpunit/phpunit: ~5
README
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');