当前位置: 首页>>代码示例>>PHP>>正文


PHP Collection::each方法代码示例

本文整理汇总了PHP中Collection::each方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::each方法的具体用法?PHP Collection::each怎么用?PHP Collection::each使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Collection的用法示例。


在下文中一共展示了Collection::each方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: addPermissions

 /**
  * Filter collection by permissions and add attributes canBeEdited and canBeRemoved
  * @param  Collection $collection
  * @return Collection
  */
 public function addPermissions($collection)
 {
     $newCollection = collect([]);
     $customActions = $this->crudeSetup->getCustomActions();
     $collection->each(function ($model) use($newCollection, $customActions) {
         if ($this->permissionView($model)) {
             $model->canBeEdited = $this->permissionUpdate($model);
             $model->canBeRemoved = $this->permissionDelete($model);
             if (!empty($customActions)) {
                 foreach ($customActions as $action => $value) {
                     $permission = $action . 'CustomActionPermission';
                     $param = $action . 'CustomActionAvailable';
                     $model->{$param} = method_exists($this, $permission) ? $this->{$permission}($model) : true;
                 }
             }
             $newCollection->push($model);
         }
     });
     return $newCollection;
 }
开发者ID:jan-dolata,项目名称:crude-crud,代码行数:25,代码来源:CollectionTrait.php

示例2: testEach

 public function testEach()
 {
     $arr = [];
     $this->collection->each(function ($item, $key) use(&$arr) {
         $arr[$key] = $item;
     });
     $this->assertEquals([$this->_0, $this->_1, $this->_2, $this->_3], $arr);
 }
开发者ID:dtkahl,项目名称:php-array-tools,代码行数:8,代码来源:CollectionTest.php

示例3: testEach

 public function testEach()
 {
     $this->collection->each(function ($item) {
         $this->assertArrayHasKey('first_name', $item);
         $this->assertArrayHasKey('last_name', $item);
         $this->assertArrayHasKey('age', $item);
     });
 }
开发者ID:dtkahl,项目名称:php-collection,代码行数:8,代码来源:CollectionTest.php

示例4: test6_Mapping

 public function test6_Mapping()
 {
     # mapping
     static::$collection->append('part2', ['books' => ['No name', 'Perpetual change']]);
     $result = [];
     static::$collection->each(function ($key) use(&$result) {
         $result[] = $key;
     });
 }
开发者ID:anctemarry27,项目名称:cogs,代码行数:9,代码来源:CollectionTest.php

示例5: groupBy

 /**
  * Splits the values into every possible response returned by the callback.
  *
  * For example, if the callback juste return the value:
  * ['a', 'b', 'c']
  * will become:
  * ['a' => ['a'], 'b' => ['b'], 'c' => ['c']]
  *
  * If the callback return a boolean, it will be return an array with two collections.
  * [0 => $nonMatchedCollection, 1 => $matchedCollection]
  * This seems to be quite the same as Doctrine's ARrayCollection. But be careful because keys are inverted.
  *
  * @param callable $callback
  * @return $this[]
  */
 public function groupBy(callable $callback)
 {
     $results = [];
     $collection = new Collection($this);
     foreach ($collection->each($callback) as $key => $result) {
         if (!isset($results[$result])) {
             $results[$result] = new $this();
         }
         $results[$result][$key] = $this[$key];
     }
     return $results;
 }
开发者ID:Raphhh,项目名称:trex-collection,代码行数:27,代码来源:CollectionSorterTrait.php

示例6: testEachWithReturn

 public function testEachWithReturn()
 {
     $collection = new Collection(['a']);
     $result = $collection->each(function ($bar) {
         return strtoupper($bar);
     });
     $this->assertNotSame($collection, $result);
     $this->assertInstanceOf(get_class($collection), $result);
     $this->assertCount(1, $result);
     foreach ($result as $bar) {
         $this->assertSame('A', $bar);
     }
 }
开发者ID:Raphhh,项目名称:trex-collection,代码行数:13,代码来源:CollectionFilterTraitTest.php


注:本文中的Collection::each方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。