/** * @dataProvider getTestCollectionData */ public function testSetValueOnNullKey($item) { $collection = new Collection($item); /** @var $collection Collection */ $this->assertEquals(2, $collection->count()); $collection[] = 'foo'; $this->assertEquals(3, $collection->count()); // test finding next free numeric key $collection[] = 'foo'; $this->assertEquals(4, $collection->count()); }
/** * Gets all but the last element or last n elements of collection. * * Returns mixed[] * * @param Collection $collection * @param int $count * @return Collection */ public function __invoke($collection, $count = 1) { $values = array_slice($collection->toArray(), 0, -$count); return $this->copyCollectionWith($collection, $values); }
/** * Returns the smallest value in the collection. * * @param Collection $collection * @return mixed */ public function __invoke(Collection $collection) { return min($collection->toArray()); }
/** * Shuffles the values of a collection while preserving the keys. * * @param Collection $collection * @return Collection */ public function __invoke($collection) { $values = $this->shuffleAssoc($collection->toArray()); return $this->copyCollectionWith($collection, $values); }
/** * Allows applying some operator to entire collection at once and returning it's result * * @param Collection $collection * @param callable $iterator * @return Collection */ public function __invoke($collection, $iterator) { $values = call_user_func($iterator, $collection->toArray()); return $this->copyCollectionWith($collection, $values); }
/** * Returns the key of the given value, in the last possible position. * * @param Collection $collection * @return mixed */ public function __invoke(Collection $collection, $value) { $values = array_reverse($collection->toArray(), true); return array_search($value, $values, true); }
/** * Gets the size of the collection by returning length for arrays or number of enumerable properties for objects. * * @param Collection $collection * @return int */ public function __invoke(Collection $collection) { return $collection->count(); }
/** * Creates an collection composed of the enumerable property values of object. * * @param Collection $collection * @return Collection */ public function __invoke($collection) { $values = array_values($collection->toArray()); return $this->copyCollectionWith($collection, $values); }
/** * Invokes $callback with the wrapped value of collection as the first argument * and then wraps it back. * * The purpose of this Mutator * is to "tap into" a Mutator * chain in order to * perform operations on intermediate results within the chain. * * @param Collection $collection * @param callable $iterator * @return Collection */ public function __invoke($collection, $iterator) { call_user_func($iterator, $collection->toArray()); return $collection; }
/** * Determine if the given key exists in the collection. * * @param Collection $collection * @param string $key * @return mixed */ public function __invoke(Collection $collection, $key) { return $collection->offsetExists($key); }
/** * Returns wrapped object * * @param Collection $collection * @return mixed */ public function __invoke(Collection $collection) { return $collection->value(); }