byjg/anydataset-array

Anydataset Array抽象层。Anydataset是PHP中的一种数据源抽象层。

4.9.1 2024-01-04 01:38 UTC

This package is auto-updated.

Last update: 2024-09-11 23:31:40 UTC


README

Build Status Opensource ByJG GitHub source GitHub license GitHub release

数组抽象数据集。Anydataset是PHP中的一种数据源抽象层。

关于Anydataset的更多信息,请参阅这里

示例

简单操作

<?php
$array = ["A", "B", "C"];

$dataset = new \ByJG\AnyDataset\Lists\ArrayDataset($array);

$iterator = $dataset->getIterator();
foreach ($iterator as $row) {
    echo $row->get('__id');     // Print 0, 1, 2
    echo $row->get('__key');    // Print 0, 1, 2
    echo $row->get('value');    // Print "A", "B", "C"
}

关联数组

<?php
$array = ["A" => "ProdA", "B" => "ProdB", "C" => "ProdC"];

$dataset = new \ByJG\AnyDataset\Lists\ArrayDataset($array);

$iterator = $dataset->getIterator();
foreach ($iterator as $row) {
    echo $row->get('__id');     // Print 0, 1, 2
    echo $row->get('__key');    // Print "A", "B", "C"
    echo $row->get('value');    // Print "ProdA", "ProdB", "ProdC"
}

对象数组

<?php
class Name {
    public $name;
    public $surname;

    public function __construct($name, $surname) {
        $this->name = $name;
        $this->surname = $surname;
    }
}
$array = [
    "A" => new Name("Joao", "Gilberto"),
    "B" => new Name("John", "Doe"),
    "C" => new Name("Mary", "Jane")
];

$dataset = new \ByJG\AnyDataset\Lists\ArrayDataset($array);

$iterator = $dataset->getIterator();
foreach ($iterator as $row) {
    echo $row->get('__id');     // Print 0, 1, 2
    echo $row->get('__key');    // Print A, B, C
    echo $row->get('__class');  // Print \Name
    echo $row->get('name');     // Print "Joao", "John", "Mary"
    echo $row->get('surname');  // Print "Gilberto", "Doe", "Jane"
}

过滤结果

<?php
class Name {
    public $name;
    public $surname;

    public function __construct($name, $surname) {
        $this->name = $name;
        $this->surname = $surname;
    }
}
$array = [
    "A" => new Name("Joao", "Gilberto"),
    "B" => new Name("John", "Doe"),
    "C" => new Name("Mary", "Jane")
];

$dataset = new \ByJG\AnyDataset\Lists\ArrayDataset($array);

$filter = new \ByJG\AnyDataset\Core\IteratorFilter();
$filter->addRelation("surname", \ByJG\AnyDataset\Core\Enum\Relation::EQUAL, "Doe");
$iterator = $dataset->getIterator($filter);
foreach ($iterator as $row) {
    echo $row->get('__id');     // Print 1
    echo $row->get('__key');    // Print B
    echo $row->get('__class');  // Print \Name
    echo $row->get('name');     // Print "John"
    echo $row->get('surname');  // Print "Doe"
}

安装

只需输入

composer require "byjg/anydataset-array"

运行单元测试

vendor/bin/phpunit

依赖项

开源ByJG