Ejemplo n.º 1
0
 /**
  * @dataProvider getTestCollectionData
  */
 public function testCollection($item)
 {
     $collection = new Collection($item);
     // count
     $this->assertSame(2, $collection->count());
     // ArrayAccess test
     // isset
     $this->assertTrue(isset($collection['name']));
     $this->assertFalse(isset($collection['precious']));
     $this->assertFalse(isset($collection['foo']));
     // get
     $this->assertSame('dummy', $collection['name']);
     $this->assertSame(42, $collection['answer']);
     // set
     $collection['foo'] = 'bar';
     $this->assertTrue(isset($collection['foo']));
     $this->assertSame('bar', $collection['foo']);
     $this->assertSame(3, $collection->count());
     // unset
     unset($collection['foo']);
     $this->assertFalse(isset($collection['foo']));
     $this->assertSame(2, $collection->count());
     // Iterator test
     $this->assertInstanceOf('\\Traversable', $collection->getIterator());
     $out = [];
     foreach ($collection as $key => $value) {
         $out[$key] = $value;
     }
     $this->assertSame(['name' => 'dummy', 'answer' => 42], $out);
     // Reversed iterator test
     $this->assertInstanceOf('\\Traversable', $collection->getIteratorReversed());
     $out = [];
     foreach ($collection->getIteratorReversed() as $key => $value) {
         $out[$key] = $value;
     }
     $this->assertSame(['answer' => 42, 'name' => 'dummy'], $out);
     // toArray & value
     $this->assertSame(['name' => 'dummy', 'answer' => 42], $collection->toArray());
     $this->assertSame($collection->toArray(), $collection->value());
     $collection['foo'] = 'baz';
     // here we have one extra key-value pair
     $this->assertSame(['name' => 'dummy', 'answer' => 42, 'foo' => 'baz'], $collection->toArray());
     // collection wrapping
     $wrapped = new Collection($collection);
     $this->assertSame($collection->toArray(), $wrapped->toArray());
 }
Ejemplo n.º 2
0
 /**
  * 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);
 }
Ejemplo n.º 3
0
 /**
  * Returns the smallest value in the collection.
  *
  * @param Collection $collection
  * @return mixed
  */
 public function __invoke(Collection $collection)
 {
     return min($collection->toArray());
 }
Ejemplo n.º 4
0
 /**
  * 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);
 }
Ejemplo n.º 5
0
 /**
  * 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);
 }
Ejemplo n.º 6
0
 /**
  * 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);
 }
Ejemplo n.º 7
0
 /**
  * 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);
 }
Ejemplo n.º 8
0
 /**
  * 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;
 }