本文整理汇总了PHP中Illuminate\Support\Collection::transform方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::transform方法的具体用法?PHP Collection::transform怎么用?PHP Collection::transform使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Support\Collection
的用法示例。
在下文中一共展示了Collection::transform方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: fill
/**
* @param array|Arrayable $data
* @return $this
*/
public function fill($data)
{
$this->data = new Collection($data);
$this->data->transform(function ($record) {
return $record instanceof Arrayable ? $record->toArray() : $record;
});
return $this;
}
示例2: getFields
/**
* Get the fields of the type.
*
* @return array
*/
public function getFields()
{
$collection = new Collection($this->fields());
return $collection->transform(function ($field, $name) {
if (is_string($field)) {
$field = app($field);
$field->name = $name;
return $field->toArray();
} else {
$resolver = $this->getFieldResolver($name, $field);
if ($resolver) {
$field['resolve'] = $resolver;
}
return $field;
}
})->toArray();
}
示例3: testTransform
public function testTransform()
{
$data = new Collection(['first' => 'taylor', 'last' => 'otwell']);
$data->transform(function ($item, $key) {
return $key . '-' . strrev($item);
});
$this->assertEquals(['first' => 'first-rolyat', 'last' => 'last-llewto'], $data->all());
}
示例4: formatData
/**
* @param Collection $data
* @return mixed
*/
private function formatData(Collection $data)
{
return $data->transform(function ($item) {
return $this->data_provider->transform($item);
});
}
示例5: generateSuffix
/**
* Generate a unique suffix for the given slug (and list of existing, "similar" slugs.
*
* @param string $slug
* @param string $separator
* @param \Illuminate\Support\Collection $list
* @return string
*/
protected function generateSuffix($slug, $separator, Collection $list)
{
$len = strlen($slug . $separator);
// If the slug already exists, but belongs to
// our model, return the current suffix.
if ($list->search($slug) === $this->model->getKey()) {
$suffix = explode($separator, $slug);
return end($suffix);
}
$list->transform(function ($value, $key) use($len) {
return intval(substr($value, $len));
});
// find the highest value and return one greater.
return $list->max() + 1;
}
示例6: transformAnnotationAddon
protected function transformAnnotationAddon(Collection $collection)
{
return $collection->transform(function ($item, $class) {
return "{$item['name']}({$class}::class)";
})->implode("\n");
}
示例7: generateType
/**
* Generate type from collection of fields.
*
* @param Collection $fields
* @param array $options
* @return \GraphQL\Type\Definition\ObjectType
*/
public function generateType(Collection $fields, $options = [])
{
$typeFields = $fields->transform(function ($field) {
if (is_string($field)) {
return app($field)->toArray();
}
return $field;
})->toArray();
return new ObjectType(array_merge(['fields' => $typeFields], $options));
}
示例8: createTags
/**
* Create new tags from a string "group:tag_name" and fetch tag from a tag id.
*
* @param array $tags
* @param bool $isAdmin
*
* @return Collection
*/
protected function createTags(array $tags, $isAdmin = false)
{
$newTags = new Collection($tags);
// Transform the user input tags into tag objects
$newTags->transform(function ($tagNameOrId) use($isAdmin) {
if (strpos($tagNameOrId, ':') !== false && $isAdmin) {
return (new Tag())->createTagFromString($tagNameOrId);
} else {
return Tag::find($tagNameOrId);
}
});
// Filter out invalid tags entered by the user
$newTags = $newTags->filter(function ($tag) {
return $tag instanceof Tag;
});
return $newTags;
}
示例9: createImageDerivatives
public function createImageDerivatives(Collection $collection)
{
$collection->transform(function ($item, $key) {
$image_styles = collect($this->getImageStyles());
$image_derivatives = collect($this->getImageDerivatives());
$fields = $image_derivatives->keys()->intersect($item->attributeKeys());
foreach ($fields->all() as $field) {
$styles = collect($image_derivatives->get($field))->intersect($image_styles->keys());
foreach ($styles->all() as $style) {
// Create image derivative and add it to $item.
if ($new_image = $this->generateImageDerivative($item->{$field}, $style)) {
// We have to deal with the attribute indirectly
// because it is an overloaded property.
if ($item->hasAttribute('image_derivatives')) {
$item_image_derivatives = (array) $item->image_derivatives;
} else {
$item_image_derivatives = [];
}
$item_image_derivatives[$style] = $new_image;
$item->image_derivatives = $item_image_derivatives;
}
}
}
return $item;
});
}