本文整理汇总了PHP中Cake\Collection\Collection类的典型用法代码示例。如果您正苦于以下问题:PHP Collection类的具体用法?PHP Collection怎么用?PHP Collection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Collection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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();
}
示例2: index
/**
* Index method
*
* @return \Cake\Network\Response|null
*/
public function index()
{
$releases = $this->Releases->find('all')->all();
$collection = new Collection($releases);
$attribute_names = array_unique($collection->extract('attribute_name')->toArray());
$idps = array_unique($collection->extract('idp')->toArray());
$releasesByIdp = $collection->groupBy('idp')->toArray();
foreach ($idps as $idp) {
foreach ($attribute_names as $attribute) {
$releasesByIdPbyAttribute = $collection->match(['idp' => $idp, 'attribute_name' => $attribute]);
$temp_result = $releasesByIdPbyAttribute->countBy(function ($result) {
return strtolower($result->validated) == 'fail' ? 'fail' : 'pass';
});
$results[$idp][$attribute] = $temp_result->toArray();
}
}
# My attributes
$persistentid_array = preg_split('/!/', $this->request->env('persistent-id'));
$persistentid = end($persistentid_array);
$myAttributesTemp = $this->Releases->find()->andWhere(['idp' => $this->request->env('Shib-Identity-Provider'), 'persistentid' => $persistentid])->all();
$myAttributesCollection = new Collection($myAttributesTemp);
$myAttributes = $myAttributesCollection->groupBy('attribute_name')->toArray();
$this->set(compact('myAttributes'));
$this->set('_serialize', ['myAttributes']);
$this->set(compact('results'));
$this->set('_serialize', ['results']);
$this->set(compact('idps'));
$this->set('_serialize', ['idps']);
$this->set(compact('attribute_names'));
$this->set('_serialize', ['attribute_names']);
}
示例3: index
/**
* Index method
*
* @return void
*/
public function index()
{
//$this->set('categorias', $this->paginate($this->Categorias));
$find = $this->Categorias->find('all', ['fields' => ['id', 'categoria_id', 'nome']])->toArray();
$collection = new Collection($find);
$categorias = $collection->nest('id', 'categoria_id')->toArray();
$this->set('categorias', $categorias);
$this->set('_serialize', ['categorias']);
}
示例4: getNextRowPosition
/**
* Returns the next highest position for adding a new row
*
* @return int
*/
public function getNextRowPosition()
{
$rows = new Collection($this->cms_rows);
$highestRow = $rows->max('position');
$maxPosition = 0;
if ($highestRow) {
$maxPosition = $highestRow->position;
}
return $maxPosition + 1;
}
示例5: getPhotosSorted
public function getPhotosSorted()
{
if (isset($this->photos) and !empty($this->photos)) {
$photosCollection = new Collection($this->photos);
return $photosCollection->sortBy(function ($photo) {
return $photo->sort_order;
}, SORT_ASC)->toArray();
}
return [];
}
示例6: _getLastFiveComments
public function _getLastFiveComments()
{
$alias = Inflector::classify($this->source());
$comment = TableRegistry::get('Social.Comments');
$comments = $comment->find()->where(['Comments.object_id' => $this->id, 'Comments.object' => $alias])->limit(__MAX_COMMENTS_LISTED)->order('Comments.created DESC')->contain(['Authors' => ['fields' => ['id', 'first_name', 'last_name', 'avatar']]]);
// Reorder the Comments by creation order
// (even though we got them by descending order)
$collection = new Collection($comments);
$comments = $collection->sortBy('Comment.created');
return $comments->toArray();
}
示例7: _getSelectOptions
/**
* Method that retrieves select input options by list name.
*
* @param string $listName list name
* @param string $spacer The string to use for prefixing the values according to
* their depth in the tree
* @param bool $flatten flat list flag
* @return array list options
*/
protected function _getSelectOptions($listName, $spacer = ' - ', $flatten = true)
{
$result = $this->__getListFieldOptions($listName);
$result = $this->__filterOptions($result);
if (!$flatten) {
return $result;
}
// flatten list options
$collection = new Collection($result);
$result = $collection->listNested()->printer('name', 'id', $spacer)->toArray();
return $result;
}
示例8: _getTagString
protected function _getTagString()
{
if (isset($this->_properties['tag_string'])) {
return $this->_properties['tag_string'];
}
if (empty($this->tags)) {
return '';
}
$tags = new Collection($this->tags);
$str = $tags->reduce(function ($string, $tag) {
return $string . $tag->label . ', ';
}, '');
return trim($str, ', ');
}
示例9: 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']);
}
}
示例10: get_for_parent
public function get_for_parent($parent, $page)
{
$comments = $this->Comments->find()->where(['Comments.object_id' => $parent])->limit(__MAX_COMMENTS_LISTED)->page($page)->order('Comments.created DESC')->contain(['Authors' => ['fields' => ['id', 'first_name', 'last_name', 'avatar']]]);
// Reorder the Comments by creation order
// (even though we got them by descending order)
$collection = new Collection($comments);
$comments = $collection->sortBy('Comment.created');
$view = new View($this->request, $this->response, null);
$view->layout = 'ajax';
// layout to use or false to disable
$view->set('comments', $comments->toArray());
$data['html'] = $view->render('Social.Comments/get_for_parent');
$this->layout = 'ajax';
$this->set('data', $data);
$this->render('/Shared/json/data');
}
示例11: addPattern
/**
* addPattern
*
*/
public function addPattern($field, $pattern)
{
$pattern = new Collection((array) $pattern);
$validationSet = $this->field($field);
$validationPatterns = self::$validationPatterns;
$pattern->each(function ($key) use($field, $validationSet, $validationPatterns) {
if (empty($validationPatterns[$key])) {
if (method_exists($this, $key)) {
$this->{$key}($field);
return;
}
throw new NoValidationPatternException('Not found pattern `' . $key . '`');
}
$rules = new Collection($validationPatterns[$key]);
$rules->each(function ($rule, $name) use($validationSet) {
$validationSet->add($name, $rule);
});
});
return $this;
}
示例12: getLibrary
public function getLibrary()
{
$this->viewBuilder()->layout("ajax");
$this->autoRender = false;
$this->loadModel('Playlists');
$this->loadModel('Tracks');
$userId = $this->request->session()->read('user.id');
$library = $this->Playlists->find('all')->where(['user_id' => $userId])->contain('Tracks');
if ($library->count() == 0) {
echo json_encode(['success' => false]);
return;
}
$library = $library->toArray();
// remove tracks from playlists
$libraryCollection = new Collection($library);
$playlists = $libraryCollection->extract(function ($list) {
unset($list->tracks);
return $list;
});
// if tokens is expired get new downloadUrls
// GAUTH: is token expired
$gotUpdatedDownloadUrls = false;
$client = new \Google_Client();
$client->setAuthConfigFile('../client_secret.json');
$client->addScope(\Google_Service_Drive::DRIVE);
if ($this->request->session()->check('user.drive_token')) {
$client->setAccessToken($this->request->session()->read('user.drive_token'));
if ($client->isAccessTokenExpired()) {
$gotUpdatedDownloadUrls = true;
}
} else {
$gotUpdatedDownloadUrls = true;
}
$driveFiles = $this->getMusicFromDrive();
$driveFileUrlById = (new Collection($driveFiles))->combine('id', function ($entity) {
return str_replace("?e=download&gd=true", "", $entity['downloadUrl']);
})->toArray();
// create tracks by playlist id, also update new download_url if gotten it
$tracksByPlaylistId = [];
$tracksById = [];
foreach ($library as $list) {
$tracksByPlaylistId[$list->id] = [];
foreach ($list->tracks as $track) {
if ($gotUpdatedDownloadUrls) {
$this->Tracks->patchEntity($track, ['download_url' => $driveFileUrlById[$track->drive_id]]);
$this->Tracks->save($track);
}
$tracksByPlaylistId[$track->playlist_id][] = $track;
$tracksById[$track->id] = $track;
}
}
echo json_encode(['playlists' => $playlists, 'tracksByPlaylistId' => $tracksByPlaylistId, 'tracksById' => $tracksById]);
}
示例13: 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;
}
}
示例14: 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");
}
}
示例15: __construct
/**
* Wraps this iterator around the passed items so when iterated they are returned
* in order.
*
* The callback will receive as first argument each of the elements in $items,
* the value returned in the callback will be used as the value for sorting such
* element. Please not that the callback function could be called more than once
* per element.
*
* @param array|\Traversable $items The values to sort
* @param callable|string $callback A function used to return the actual value to
* be compared. It can also be a string representing the path to use to fetch a
* column or property in each element
* @param int $dir either SORT_DESC or SORT_ASC
* @param int $type the type of comparison to perform, either SORT_STRING
* SORT_NUMERIC or SORT_NATURAL
*/
public function __construct($items, $callback, $dir = SORT_DESC, $type = SORT_NUMERIC)
{
if (is_array($items)) {
$items = new Collection($items);
}
$items = iterator_to_array($items, false);
$callback = $this->_propertyExtractor($callback);
$results = [];
foreach ($items as $key => $value) {
$results[$key] = $callback($value);
}
$dir === SORT_DESC ? arsort($results, $type) : asort($results, $type);
foreach (array_keys($results) as $key) {
$results[$key] = $items[$key];
}
parent::__construct($results);
}