PHP code example of arrilot / collectors

1. Go to this page and download the library: Download arrilot/collectors library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

arrilot / collectors example snippets



use Arrilot\Collectors\Collector;

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

    $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",
          ],
        ]
    */

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

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

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

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