mibexx/collection

可以将数组转换为集合对象,也可以将集合对象转换回数组

v0.1.2 2017-05-01 13:10 UTC

This package is auto-updated.

Last update: 2024-09-29 04:03:28 UTC


README

用于将列表作为对象的集合。
可以将数组转换为集合,也可以将集合转换为数组。
集合是可迭代的。

安装

composer require mibexx/collection

配置

无需配置

使用方法

将数组转换为集合

use mibexx\collection\Collection\Collection;
use mibexx\collection\Parser\CollectionParser;

$list = [
    'foo' => 'bar',
    'fuzz' => [
        'foo' => 'buzz'
    ]
];

$parser = new CollectionParser();
$collection = new Collection();
$parser->convertToCollection($list, $collection);
 
echo $collection->get('foo')->get(); // prints bar
echo $collection->get('fuzz')->get('foo')->get(); // prints buzz

创建集合并将转换为数组

use mibexx\collection\Collection\Collection;
use mibexx\collection\Parser\ArrayParser;

$parser = new ArrayParser();
$collection = new Collection();

$fuzz = new Collection();
$fuzz->set('foo', new SimpleElement('buzz'));

$collection->set('foo', new SimpleElement('bar'));
$collection->set('fuzz', new CollectionElement($fuzz));

$newList = $parser->convertToArray($collection);

print_r($newList);
/* equal to:
array(
    'foo' => 'bar',
    'fuzz' => [
        'foo' => 'buzz'
    ]
)
*/