本文整理汇总了PHP中collection函数的典型用法代码示例。如果您正苦于以下问题:PHP collection函数的具体用法?PHP collection怎么用?PHP collection使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了collection函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _getTagString
protected function _getTagString()
{
if (isset($this->_properties['tag_string'])) {
return $this->_properties['tag_string'];
}
return implode(', ', collection($this->tags ?: [])->extract('title')->toArray());
}
示例2: format
/**
* Get format permission data.
*
* @param $acos
* @param array $aros
* @param array $options
* @return array
* @SuppressWarnings("unused")
*/
public function format($acos, array $aros, array $options = [])
{
$options = Hash::merge(['perms' => true, 'model' => 'Roles'], $options);
$permissions = [];
/** @var \Acl\Model\Entity\Aco $aco */
foreach ($acos as $aco) {
$acoId = $aco->get('id');
$acoAlias = $aco->get('alias');
$path = $this->Acos->find('path', ['for' => $acoId]);
$path = join('/', collection($path)->extract('alias')->toArray());
$data = ['path' => $path, 'alias' => $acoAlias, 'depth' => substr_count($path, '/')];
foreach ($aros as $key => $aroId) {
$role = ['foreign_key' => $key, 'model' => $options['model']];
if ($options['perms']) {
$isCheck = $this->check($role, $path);
if ($key == Role::ADMIN_ID || $isCheck) {
$data['roles'][$key] = 1;
} else {
$data['roles'][$key] = (int) $isCheck;
}
}
$permissions[$acoId] = new Data($data);
}
}
return $permissions;
}
示例3: _loadPermissions
/**
* Loads ll permissions rules for this content type.
*
* @return void
*/
protected function _loadPermissions()
{
if (empty($this->_permissions)) {
$permissions = TableRegistry::get('Content.ContentTypePermissions')->find()->where(['content_type_id' => $this->get('id')])->toArray();
$this->_permissions = collection($permissions);
}
}
示例4: getRole
/**
* Get aros role ids.
*
* @param array $roles
* @return array
* @SuppressWarnings("unused")
*/
public function getRole(array $roles = [])
{
$aros = $this->find()->where(['model' => 'Roles'])->where(function ($exp, $q) use($roles) {
/** @var \Cake\Database\Expression\QueryExpression $exp */
return $exp->in('foreign_key', array_keys($roles));
});
return collection($aros)->combine('foreign_key', 'id')->toArray();
}
示例5: setUp
/**
* setUp.
*
* @return void
*/
public function setUp()
{
parent::setUp();
$View = new View();
$View->theme(option('front_theme'));
$options = ['fixMissing' => false];
$this->regions = [new Region($View, 'left-sidebar', $options), new Region($View, 'right-sidebar', $options), new Region($View, 'footer', $options)];
$this->regions[0]->blocks(collection([new Entity(['region' => new Entity(['region' => 'left-sidebar'])]), new Entity(['region' => new Entity(['region' => 'left-sidebar'])]), new Entity(['region' => new Entity(['region' => 'left-sidebar'])])]));
$this->regions[1]->blocks(collection([new Entity(['region' => new Entity(['region' => 'right-sidebar'])]), new Entity(['region' => new Entity(['region' => 'right-sidebar'])])]));
$this->regions[2]->blocks(collection([new Entity(['region' => new Entity(['region' => 'footer'])]), new Entity(['region' => new Entity(['region' => 'footer'])]), new Entity(['region' => new Entity(['region' => 'footer'])]), new Entity(['region' => new Entity(['region' => 'footer'])]), new Entity(['region' => new Entity(['region' => 'footer'])])]));
}
示例6: getAgencies
public static function getAgencies()
{
$posts = [];
$items = collection("Agencies")->find()->toArray();
foreach ($items as $key => $value) {
//var_dump($value);
if (stripos($value["Name"], $_POST["query"]) !== false) {
array_push($posts, $value);
}
}
echo json_encode($posts);
}
示例7: fieldIsKey
protected function fieldIsKey($field, $type)
{
$associations = collection($this->helper->associations())->filter(function ($association, $key) use($field) {
return $association['foreign_key'] === $field;
});
foreach ($associations as $association) {
if (isset($this->helper->foreignKeys()[$field]) && !$association['owner'] && $association['association_type'] === $type) {
return $association;
}
}
return false;
}
示例8: handle
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param Closure $next
* @param $options
* @return mixed
*/
public function handle($request, Closure $next, $options)
{
$parsedOptions = $this->parseOptions($options);
$permissions = array_keys($parsedOptions);
if ($this->getGuard()->guest()) {
return $this->handleUnauthorized();
}
$roles = $this->getGuard()->user()->roles->pluck('slug')->toArray();
if (!collection($permissions)->anyIn($roles)) {
return $this->handleUnauthorized();
}
return $next($request);
}
示例9: testPrepareCollectionData
/**
* test method which converts a collection to a flat array
*/
public function testPrepareCollectionData()
{
$articles = $this->Articles->find('all');
$data = collection($articles->toArray());
$result = $this->Excel->prepareCollectionData($data);
$this->assertEquals(count($result), 4);
$headers = $result[0];
$first = $result[1];
$last = $result[3];
$this->assertEquals($headers, ['id', 'author_id', 'title', 'body', 'published']);
$this->assertEquals($first, [1, 1, 'First Article', 'First Article Body', 'Y']);
$this->assertEquals($last, [3, 1, 'Third Article', 'Third Article Body', 'Y']);
}
示例10: testDeepBelongsToManySubqueryStrategy2
/**
* Tests that using the subquery strategy in a deep association returns the right results
*
* @see https://github.com/cakephp/cakephp/issues/5769
* @return void
*/
public function testDeepBelongsToManySubqueryStrategy2()
{
$table = TableRegistry::get('Authors');
$table->hasMany('Articles');
$table->Articles->belongsToMany('Tags', ['strategy' => 'subquery']);
$table->belongsToMany('Tags', ['strategy' => 'subquery']);
$table->Articles->belongsTo('Authors');
$result = $table->Articles->find()->where(['Authors.id >' => 1])->contain(['Authors' => ['Tags' => function ($q) {
return $q->order(['name']);
}]])->toArray();
$this->assertEquals(['tag1', 'tag2'], collection($result[0]->author->tags)->extract('name')->toArray());
$this->assertEquals(3, $result[0]->author->id);
}
示例11: getBlogPosts
function getBlogPosts($filter, $page)
{
global $blogPostsPerPage;
$collection = collection('Blog Posts');
$count = $collection->count($filter);
$pages = ceil($count / $blogPostsPerPage);
if ($page > $pages && $page != 1) {
return null;
}
$posts = $collection->find($filter);
$posts->limit($blogPostsPerPage)->skip(($page - 1) * $blogPostsPerPage);
$posts->sort(["posted_date" => -1]);
return ['posts' => $posts->toArray(), 'page' => $page, 'pages' => $pages];
}
示例12: _map
/**
* Maps raw data using mapFields
*
* @return mixed
*/
protected function _map()
{
$result = [];
collection($this->_mapFields)->each(function ($mappedField, $field) use(&$result) {
$value = Hash::get($this->_rawData, $mappedField);
$function = '_' . $field;
if (method_exists($this, $function)) {
$value = $this->{$function}();
}
$result[$field] = $value;
});
$token = Hash::get($this->_rawData, 'token');
$result['credentials'] = ['token' => is_array($token) ? Hash::get($token, 'accessToken') : $token->getToken(), 'secret' => is_array($token) ? Hash::get($token, 'tokenSecret') : null, 'expires' => is_array($token) ? Hash::get($token, 'expires') : $token->getExpires()];
$result['raw'] = $this->_rawData;
return $result;
}
示例13: main
/**
* Sends queued emails.
*/
public function main()
{
if ($this->params['stagger']) {
sleep(rand(0, $this->params['stagger']));
}
Configure::write('App.baseUrl', '/');
$emailQueue = TableRegistry::get('EmailQueue', ['className' => EmailQueueTable::class]);
$emails = $emailQueue->getBatch($this->params['limit']);
$count = count($emails);
foreach ($emails as $e) {
$configName = $e->config === 'default' ? $this->params['config'] : $e->config;
$template = $e->template === 'default' ? $this->params['template'] : $e->template;
$layout = $e->layout === 'default' ? $this->params['layout'] : $e->layout;
$headers = empty($e->headers) ? array() : (array) $e->headers;
$theme = empty($e->theme) ? '' : (string) $e->theme;
try {
$email = $this->_newEmail($configName);
if (!empty($e->from_email) && !empty($e->from_name)) {
$email->from($e->from_email, $e->from_name);
}
$transport = $email->transport();
if ($transport && $transport->config('additionalParameters')) {
$from = key($email->from());
$transport->config(['additionalParameters' => "-f {$from}"]);
}
if (!empty($e->attachments)) {
$email->attachments($e->attachments);
}
$sent = $email->to($e->email)->subject($e->subject)->template($template, $layout)->emailFormat($e->format)->addHeaders($headers)->theme($theme)->viewVars($e->template_vars)->messageId(false)->returnPath($email->from())->send();
} catch (SocketException $exception) {
$this->err($exception->getMessage());
$sent = false;
}
if ($sent) {
$emailQueue->success($e->id);
$this->out('<success>Email ' . $e->id . ' was sent</success>');
} else {
$emailQueue->fail($e->id);
$this->out('<error>Email ' . $e->id . ' was not sent</error>');
}
}
if ($count > 0) {
$emailQueue->releaseLocks(collection($emails)->extract('id')->toList());
}
}
示例14: getFeaturedImage
/**
* Loads the featured image (if any)
*
* @return void
*/
public function getFeaturedImage(Event $event, Entity $entity)
{
$featuredImage = Hash::extract($entity->meta, '{n}[key=featured_image]');
if (empty($featuredImage)) {
return;
}
$featuredImage = end($featuredImage);
$entity->featured_image_meta_id = $featuredImage->id;
$entity->featured_image_id = $featuredImage->value;
try {
$entity->featured_image = TableRegistry::get('Croogo/FileManager.Attachments')->get($entity->featured_image_id);
} catch (RecordNotFoundException $e) {
//Image does not exist
unset($entity->featured_image_id);
}
$entity->meta = collection($entity->meta)->reject(function ($item) {
return $item->key === 'featured_image';
})->toList();
}
示例15: enqueue
/**
* Stores a new email message in the queue.
*
* @param mixed $to email or array of emails as recipients
* @param array $data associative array of variables to be passed to the email template
* @param array $options list of options for email sending. Possible keys:
*
* - subject : Email's subject
* - send_at : date time sting representing the time this email should be sent at (in UTC)
* - template : the name of the element to use as template for the email message
* - layout : the name of the layout to be used to wrap email message
* - format: Type of template to use (html, text or both)
* - config : the name of the email config to be used for sending
*
* @return bool
*/
public function enqueue($to, array $data, array $options = [])
{
$defaults = ['subject' => '', 'send_at' => new FrozenTime('now'), 'template' => 'default', 'layout' => 'default', 'theme' => '', 'format' => 'both', 'headers' => [], 'template_vars' => $data, 'config' => 'default', 'attachments' => []];
$email = $options + $defaults;
if (!is_array($to)) {
$to = [$to];
}
$emails = [];
foreach ($to as $t) {
$emails[] = ['email' => $t] + $email;
}
$emails = $this->newEntities($emails);
return $this->connection()->transactional(function () use($emails) {
$failure = collection($emails)->map(function ($email) {
return $this->save($email);
})->contains(false);
return !$failure;
});
}