dotink/flourish-collection

一个基本集合类

这个包的官方仓库似乎已不存在,因此该包已被冻结。

安装: 905

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 0

开放问题: 0

类型:opus-package

1.1.6 2015-05-07 06:18 UTC

This package is auto-updated.

Last update: 2024-02-29 02:29:02 UTC


README

Build Status

这是一个非常简单的集合类,设计用于扩展。它在需要轻松进行选择性集合查询或使用默认值时非常有用。

基本用法

$collection = new Dotink\Flourish\Collection([
	'a' => 'apple',
	'b' => 'banana',
	'c' => 'carrot'
]);

获取值

$collection->get('a'); // returns 'apple'

获取值 + 默认值

$collection->get('d', 'date'); // returns 'date'

获取多个值

$collection->get(['a', 'c']); // returns ['a' => 'apple', 'c' => 'carrot']

设置值

$collection->set('d', 'date');

设置多个值

$collection->set([
	'e' => 'eggplant',
	'f' => 'fig'
]);

遍历集合

foreach ($collection as $letter => $fruit_or_vegetable) {
	echo sprintf(
		'The letter "%s" is for "%s"',
		$letter,
		$fruit_or_vegetable
	);
}

复合键

可以使用复合键来访问嵌套数组中的数据(获取和设置)。

设置嵌套数据

$collection->set('foo.bar', 'foobar'); // $collection->get('foo') returns ['bar' => 'foobar']

获取嵌套数据

$collection->get('foo.bar'); // return 'foobar'