dotink / flourish-collection
一个基本集合类
这个包的官方仓库似乎已不存在,因此该包已被冻结。
1.1.6
2015-05-07 06:18 UTC
Requires
- php: >=5.4.0
- dotink/flourish-core: ^1.0.0
- imarc/opus: ^1.0.0
Requires (Dev)
- dotink/lab: ^1.1.0
README
这是一个非常简单的集合类,设计用于扩展。它在需要轻松进行选择性集合查询或使用默认值时非常有用。
基本用法
$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'