transform() public method

Transform each item in the collection using a callback.
public transform ( callable $callback )
$callback callable
Example #1
0
 /**
  * @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;
 }
 /**
  * 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();
 }
Example #3
0
 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());
 }
Example #4
0
 /**
  * @param Collection $data
  * @return mixed
  */
 private function formatData(Collection $data)
 {
     return $data->transform(function ($item) {
         return $this->data_provider->transform($item);
     });
 }
 /**
  * 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;
 }
Example #6
0
 protected function transformAnnotationAddon(Collection $collection)
 {
     return $collection->transform(function ($item, $class) {
         return "{$item['name']}({$class}::class)";
     })->implode("\n");
 }
Example #7
0
 /**
  * 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));
 }
Example #8
0
 /**
  * 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;
 }
 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;
     });
 }