selene / common
dev-development
2014-08-23 20:41 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- league/phpunit-coverage-listener: dev-master
- selene/cryptography: dev-development
- selene/testsuite: dev-development
This package is not auto-updated.
Last update: 2015-12-05 10:10:37 UTC
README
通用组件几乎被所有其他Selene组件共享。
安装
通过composer进行安装。
将selene/events
添加到你的composer.json
文件中作为需求。
{ "require": { "selene/common":"dev-development" } }
运行composer install
或composer update
$ composer install --dev
测试
使用以下命令运行测试
$ vendor/bin/phpunit
数据结构
列表
列表受Python列表的启发
用法<?php use \Selene\Components\Common\Data\BaseList; $list = new BaseList(1, 2, 3); // do operations $values = $list->toArray();append()
向列表末尾添加值
<?php $list->append('foo'); // [1, 2, 3, 'foo'] // or $list->add('foo'); // [1, 2, 3, 'foo']insert()
在指定索引插入值
<?php $list->insert(4, 3); // [1, 2, 3, 4, 'foo']pop()
返回列表中的最后一个值并从中移除
<?php $list->pop(); // 'foo'pop($index)
返回指定索引的值并将其从列表中移除
<?php $list->pop(2); // 3remove($value)
从列表中移除值
<?php $list->remove(1); // [2, 4]countValue($value)
计算值的出现次数
<?php $list->countValue(2); // 1sort()
对所有值进行排序
<?php $list->sort();reverse()
反转列表顺序
<?php $list->reverse(); // [4, 2]extend($list)
使用另一个列表扩展列表
<?php $newList = new BaseList('foo', 'bar'); $list->extend($newList);
集合
<?php use \Selene\Components\Common\Data\Collection; $collection = new Collection;
特性
获取器
Getter
特性包含一些访问器方法,用于从数组中检索数据。
如果给定键设置了值,则从数据集中返回一个值,否则返回默认值。
<?php use Getter; private $attributes = []; public function get($attribute, $default = null); { return $this->getDefault($this->attributes, $attribute, $default); }getDefaultUsingKey($data, $key, $defaultValue [可选])
如果存在键,则从数据集中返回一个值,否则返回默认值。
<?php use Getter; private $attributes = []; public function get($attribute, $default = null); { return $this->getDefaultUsingKey($this->attributes, $attribute, $default); }getDefaultUsing($data, $key, $closure [可选])
如果给定键设置了值,则从数据集中返回一个值,否则使用回调函数返回默认值。
<?php use Getter; private $attributes = []; public function get($attribute); { return $this->getDefaultUsing($this->attributes, $attribute, function () { // get some default value return $result; }); }getDefaultArray($data, $key, $defaultValue [可选])
如果存在键,则从多维数据集中返回一个值,否则返回默认值。您可以指定一个键分隔符来访问数组键,例如 foo.bar
将在 ['foo' => ['bar' => 12]]
中搜索值。
<?php use Getter; private $attributes = []; public function get($attribute); { return $this->getDefaultArray($this->attributes, $attribute, $default, '.'); }