arrilot/collectors

此包已被废弃且不再维护。未建议替代包。

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 (开发中)

介绍

Collectors扫描项/集合中给定的字段以查找ids,并从数据库或其他存储中获取详细数据

安装

composer require arrilot/collectors

使用方法

首先,你需要创建自己的收集器类。

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

如果你已经知道额外的ids,可以手动添加。

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

你可以像这样将select传递给getlist

$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');

桥接包