eden / collection
Eden 通用集合组件
4.0.1
2015-10-13 05:27 UTC
Requires
- php: >=5.4.1
- eden/array: 4.*
- eden/core: 4.*
- eden/model: 4.*
- eden/string: 4.*
README
====
安装
composer install eden/collection
====
简介
在大多数情况下,对表格、矩阵或多维数据的操作可以表示为一个集合。在 Eden 中,集合被定义得较为宽松,作为一个工具类,以帮助以受控和链式的方式管理数据。集合的基本设置在图 1 中描述。
图 1. 设置
$users = array(
array(
'user_name' => 'Chris',
'user_email' => 'cblanquera@openovate.com',
'user_location' => 'Manila, Philippines'
),
array(
'user_name' => 'Dan',
'user_email' => 'dmolina@openovate.com',
'user_location' => 'Manila, Philippines'
),
);
eden('collection', $users);
集合与模型的作用完全相同,只是它操作的是多个模型。集合可以迭代并且可以像数组一样访问。集合只持有模型对象,所以如果你想使用自己的扩展模型,你需要调用 ->setModel('Your_Model')
。从这里我们可以将集合中的属性作为方法、属性或数组访问。图 2 显示了访问数据的方式。
**图 2. 访问集合、模型和行 **
//set user name for all rows
$collection->setUserName('Chris');
// set or get any abstract key for all rows
$collection->setAnyThing();
//collections are iterable
foreach($collection as $model) {
echo $model->getUserName().' ';
echo $model['user_email'];
}
//access as array
echo $collection[0]['user_name'];
//set as array
$collection[0]['user_email'] = 'my@email.com';
上述示例中没有涵盖的一些其他实用方法包括日期格式化和从一个列复制到另一个列。图 3 显示了如何进行这些操作。
图 3. 实用方法
//for each row, copy the value of post_user to the user_id column
$collection->copy('post_user', 'user_id');
//remove the row with the index of 1, reindexes all the rows
$collection->cut(1);
//returns the number of rows
$collection->count();
//adds a new row
$collection->add(array('post_title' => 'Hi'));
//returns a table array (no objects)
$collection->get();
====
对 Eden 的贡献遵循 Github 的工作流程。请在贡献之前阅读。
##设置包含 Eden 仓库和你的分支的机器
- 分支仓库
- 在你的本地终端启动,从你的分支的
v4
分支创建一个新的分支,分支名称应描述你的更改。可能的分支名称类型- bugfix
- feature
- improvement
- 进行你的更改。始终确保在所有提交中签名 (-s)(git commit -s -m "提交信息")
##制作拉取请求
- 请在提交拉取请求之前运行
phpunit
。 - 将你的代码推送到你的远程分支版本。
- 回到你的 GitHub 上的分支版本,提交拉取请求。
- Eden 开发者将审查你的代码,并在它被认为适合时合并它。