本文整理汇总了PHP中Cake\Collection\Collection::filter方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::filter方法的具体用法?PHP Collection::filter怎么用?PHP Collection::filter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\Collection\Collection
的用法示例。
在下文中一共展示了Collection::filter方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: checkPermission
/**
* Checks if the provided $roleId is allowed to perform the given $action.
*
* @param int $roleId Role ID
* @param string $action Action to check: create, edit, delete or publish
* @return bool
*/
public function checkPermission($roleId, $action)
{
if ($roleId == ROLE_ID_ADMINISTRATOR) {
return true;
}
$this->_loadPermissions();
$rule = $this->_permissions->filter(function ($rule) use($roleId, $action) {
return $rule->get('role_id') == $roleId && $rule->get('action') == $action;
})->first();
return !empty($rule);
}
示例2: getBlocksForColumn
/**
* Return all blocks to render in the given row column
*
* @param int $column Numeric column index, starting with 1
* @return array
*/
public function getBlocksForColumn($column)
{
$blocks = new \Cake\Collection\Collection($this->cms_blocks);
return $blocks->filter(function ($block) use($column) {
return $block->column_index == $column;
})->toArray();
}
示例3: isPermitted
public function isPermitted($permissions, $operator = OPERATOR_AND)
{
$session = $this->request->session()->read('Auth.User');
if ($session['idr_admin']) {
return true;
}
$acoes = new Collection($session['acoes']);
if (is_array($permissions)) {
if ($operator == OPERATOR_AND) {
foreach ($permissions as $k => $p) {
$permitido = $acoes->filter(function ($acao, $key) use($p) {
return mb_strtoupper($acao['tag']) == mb_strtoupper($p);
});
if (count($permitido->toArray()) == 0) {
break;
}
}
} else {
foreach ($permissions as $k => $p) {
$permitido = $acoes->filter(function ($acao, $key) use($p) {
return mb_strtoupper($acao['tag']) == mb_strtoupper($p);
});
if (count($permitido->toArray()) > 0) {
break;
}
}
}
} else {
$permitido = $acoes->filter(function ($acao, $key) use($permissions) {
return mb_strtoupper($acao['tag']) == mb_strtoupper($permissions);
});
}
if (count($permitido->toArray()) > 0) {
return true;
} else {
return false;
}
}
示例4: anyPermitted
private function anyPermitted($permissions, $operator = self::OPERATOR_AND)
{
$session = $this->request->session()->read('Auth.User');
if ($session['idr_admin']) {
return true;
}
$acoes = new Collection($session['acoes']);
if (is_array($permissions)) {
if ($operator == self::OPERATOR_AND) {
foreach ($permissions as $k => $p) {
$permitido = $acoes->filter(function ($acao, $key) use($p) {
return mb_strtoupper($acao['tag']) == mb_strtoupper($p);
});
if (count($permitido->toArray()) == 0) {
break;
}
}
} else {
foreach ($permissions as $k => $p) {
$permitido = $acoes->filter(function ($acao, $key) use($p) {
return mb_strtoupper($acao['tag']) == mb_strtoupper($p);
});
if (count($permitido->toArray()) > 0) {
break;
}
}
}
} else {
$permitido = $acoes->filter(function ($acao, $key) use($permissions) {
return mb_strtoupper($acao['tag']) == mb_strtoupper($permissions);
});
}
if (count($permitido->toArray()) == 0) {
throw new UnauthorizedException("Usuário da sessão não possui permissão para acessar a ação escolhida");
}
}
示例5: testFilterChaining
/**
* Tests that it is possible to chain filter() as it returns a collection object
*
* @return void
*/
public function testFilterChaining()
{
$this->assertFalse(defined('HHVM_VERSION'), 'Broken on HHVM');
$items = ['a' => 1, 'b' => 2, 'c' => 3];
$collection = new Collection($items);
$callable = $this->getMock('stdClass', ['__invoke']);
$callable->expects($this->once())->method('__invoke')->with(3, 'c');
$filtered = $collection->filter(function ($value, $key, $iterator) {
return $value > 2;
});
$this->assertInstanceOf('\\Cake\\Collection\\Collection', $filtered);
$filtered->each($callable);
}
示例6: getType
/**
* Retrieves a type that should be used for a specific field
*
* @param string $field Name of field
* @param string $type User-specified type
* @return string
*/
public function getType($field, $type)
{
$reflector = new ReflectionClass('Phinx\\Db\\Adapter\\AdapterInterface');
$collection = new Collection($reflector->getConstants());
$validTypes = $collection->filter(function ($value, $constant) {
$value;
return substr($constant, 0, strlen('PHINX_TYPE_')) === 'PHINX_TYPE_';
})->toArray();
$fieldType = $type;
if ($type === null || !in_array($type, $validTypes)) {
if ($type === 'primary') {
$fieldType = 'integer';
} elseif ($field === 'id') {
$fieldType = 'integer';
} elseif (in_array($field, ['created', 'modified', 'updated'])) {
$fieldType = 'datetime';
} else {
$fieldType = 'string';
}
}
return $fieldType;
}
示例7: testFilterChaining
/**
* Tests that it is possible to chain filter() as it returns a collection object
*
* @return void
*/
public function testFilterChaining()
{
$items = ['a' => 1, 'b' => 2, 'c' => 3];
$collection = new Collection($items);
$callable = $this->getMockBuilder(\StdClass::class)->setMethods(['__invoke'])->getMock();
$callable->expects($this->once())->method('__invoke')->with(3, 'c');
$filtered = $collection->filter(function ($value, $key, $iterator) {
return $value > 2;
});
$this->assertInstanceOf('Cake\\Collection\\Collection', $filtered);
$filtered->each($callable);
}
示例8: filterPrimaryKey
/**
* This method is called in case a primary key was defined using the addPrimaryKey() method.
* It currently does something only if using SQLite.
* If a column is an auto-increment key in SQLite, it has to be a primary key and it has to defined
* when defining the column. Phinx takes care of that so we have to make sure columns defined as autoincrement were
* not added with the addPrimaryKey method, otherwise, SQL queries will be wrong.
*
* @return void
*/
protected function filterPrimaryKey()
{
if ($this->getAdapter()->getAdapterType() !== 'sqlite' || empty($this->options['primary_key'])) {
return;
}
$primaryKey = $this->options['primary_key'];
if (!is_array($primaryKey)) {
$primaryKey = [$primaryKey];
}
$primaryKey = array_flip($primaryKey);
$columnsCollection = new Collection($this->columns);
$primaryKeyColumns = $columnsCollection->filter(function ($columnDef, $key) use($primaryKey) {
return isset($primaryKey[$columnDef->getName()]);
})->toArray();
if (empty($primaryKeyColumns)) {
return;
}
foreach ($primaryKeyColumns as $primaryKeyColumn) {
if ($primaryKeyColumn->isIdentity()) {
unset($primaryKey[$primaryKeyColumn->getName()]);
}
}
$primaryKey = array_flip($primaryKey);
if (!empty($primaryKey)) {
$this->options['primary_key'] = $primaryKey;
} else {
unset($this->options['primary_key']);
}
}
示例9: _getLosers
protected function _getLosers()
{
// $gameMembershipsCollection = new Collection($this->game_memberships);
// $victor_member = $gameMembershipsCollection->indexBy('id')->max('points');
//
// $victor['full_name'] = Hash::get($victor_member, 'member.full_name');
// $victor['id'] = Hash::get($victor_member, 'member.id');
$gm_collection = new Collection($this->game_memberships);
$victor = $gm_collection->indexBy('id')->max('points');
$victor_id = $victor->generic_member->id;
$losers = $gm_collection->filter(function ($gm, $key) use($victor_id) {
return $victor_id != $gm->generic_member->id;
});
// if(!$loser = $losers->first())
// return false;
// if(!($loser->member or $loser->temp_member))
// return false;
$losers_array = [];
foreach ($losers as $loser) {
array_push($losers_array, $loser);
}
return $losers_array;
}