本文整理汇总了PHP中Cake\Collection\Collection::map方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::map方法的具体用法?PHP Collection::map怎么用?PHP Collection::map使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\Collection\Collection
的用法示例。
在下文中一共展示了Collection::map方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testCompile
/**
* Tests that by calling compile internal iteration operations are not done
* more than once
*
* @return void
*/
public function testCompile()
{
$items = ['a' => 1, 'b' => 2, 'c' => 3];
$collection = new Collection($items);
$callable = $this->getMock('stdClass', ['__invoke']);
$callable->expects($this->at(0))->method('__invoke')->with(1, 'a')->will($this->returnValue(4));
$callable->expects($this->at(1))->method('__invoke')->with(2, 'b')->will($this->returnValue(5));
$callable->expects($this->at(2))->method('__invoke')->with(3, 'c')->will($this->returnValue(6));
$compiled = $collection->map($callable)->compile();
$this->assertEquals(['a' => 4, 'b' => 5, 'c' => 6], $compiled->toArray());
$this->assertEquals(['a' => 4, 'b' => 5, 'c' => 6], $compiled->toArray());
}
示例2: override
/**
* Set an override for a column, columns or restore all to default
*
* Pass CRUDDATA_DEFAULT_OVERRIDES (FALSE) to restore to default types for all columns
* Pass string_name, string_value to override one column
* Pass [
* string_name => string_value,
* string_name => string_value
* ] for multiple overrides at once
*
* @param array|string|boolean $override
* @param string $type
* @return array
*/
public function override($override = NULL, $type = 'string')
{
if ($override === CRUDDATA_DEFAULT_OVERRIDES) {
$this->_clearOverrides();
} elseif (is_string($override)) {
$this->_columns[$override]['type'] = $type;
} elseif (is_array($override)) {
foreach ($override as $column => $type) {
$this->_columns[$column]['type'] = $type;
}
}
$columns = new Collection($this->columns());
$overrides = [];
return $columns->map(function ($value, $key) use(&$overrides) {
return $overrides[$key] = ['type' => $value['type']];
})->toArray();
}