first() public method

Returns the first non-empty value in the collection after a filter is applied, or rewinds the collection and returns the first value.
See also: lithium\util\Collection::rewind()
public first ( callback $filter = null ) : mixed
$filter callback A closure through which collection values will be passed. If the return value of this function is non-empty, it will be returned as the result of the method call. If `null`, the collection is rewound (see `rewind()`) and the first item is returned.
return mixed Returns the first non-empty collection value returned from `$filter`.
Beispiel #1
0
 public function export(Source $dataSource, array $options = array())
 {
     $defaults = array('atomic' => true);
     $options += $defaults;
     list($data, $nested) = $this->_exportRecursive($dataSource, $options);
     if ($options['atomic'] && $this->_exists) {
         $data = array_intersect_key($data, $this->_modified + $nested);
     }
     if ($model = $this->_model) {
         $name = null;
         $options = array('atomic' => false) + $options;
         $relations = new Collection(array('data' => $model::relations()));
         $find = function ($relation) use(&$name) {
             return $relation->fieldName === $name;
         };
         foreach ($this->_relationships as $name => $subObject) {
             if (($rel = $relations->first($find)) && $rel->link == $rel::LINK_EMBEDDED) {
                 $data[$name] = $subObject->export($dataSource, $options);
             }
         }
     }
     return $data;
 }
Beispiel #2
0
 /**
  * Tests that the `first()` method properly returns the first non-empty value.
  *
  * @return void
  */
 public function testCollectionFirstFilter()
 {
     $collection = new Collection(array('data' => array(0, 1, 2)));
     $result = $collection->first(function ($value) {
         return $value;
     });
     $this->assertEqual(1, $result);
     $collection = new Collection(array('data' => array('Hello', '', 'Goodbye')));
     $result = $collection->first(function ($value) {
         return $value;
     });
     $this->assertEqual('Hello', $result);
     $collection = new Collection(array('data' => array('', 'Hello', 'Goodbye')));
     $result = $collection->first(function ($value) {
         return $value;
     });
     $this->assertEqual('Hello', $result);
     $collection = new Collection(array('data' => array('', 'Hello', 'Goodbye')));
     $result = $collection->first();
     $this->assertEqual('', $result);
 }
Beispiel #3
0
 /**
  * Overrides parent `first()` implementation to enable key/value-based filtering.
  *
  * @param mixed $filter In addition to a callback (see parent), can also be an array where the
  *              keys and values must match the property values of the objects being inspected.
  * @return object Returns the first object found matching the filter criteria.
  */
 public function first($filter = null)
 {
     return parent::first(is_array($filter) ? $this->_filterFromArray($filter) : $filter);
 }
Beispiel #4
0
 /**
  * Tests the `ArrayAccess` interface implementation for traversing values.
  */
 public function testArrayAccessTraversalMethods()
 {
     $collection = new Collection(array('data' => array('foo', 'bar', 'baz' => 'dib')));
     $this->assertEqual('foo', $collection->current());
     $this->assertEqual('bar', $collection->next());
     $this->assertEqual('foo', $collection->prev());
     $this->assertEqual('bar', $collection->next());
     $this->assertEqual('dib', $collection->next());
     $this->assertEqual('baz', $collection->key());
     $this->assertTrue($collection->valid());
     $this->assertFalse($collection->next());
     $this->assertFalse($collection->valid());
     $this->assertEqual('foo', $collection->rewind());
     $this->assertTrue($collection->valid());
     $this->assertEqual('dib', $collection->prev());
     $this->assertTrue($collection->valid());
     $this->assertEqual('bar', $collection->prev());
     $this->assertTrue($collection->valid());
     $this->assertEqual('dib', $collection->end());
     $this->assertTrue($collection->valid());
     $collection = new Collection(array('data' => array(0, 1, 2, 3, 4)));
     $this->assertIdentical(0, $collection->first());
     $this->assertIdentical(0, $collection->rewind());
     $this->assertIdentical(1, $collection->next());
     $this->assertIdentical(2, $collection->next());
     $this->assertIdentical(3, $collection->next());
     $this->assertIdentical(2, $collection->prev());
     $this->assertIdentical(2, $collection->current());
     $this->assertIdentical(3, $collection->next());
     $this->assertIdentical(4, $collection->next());
     $this->assertIdentical(3, $collection->prev());
     $this->assertIdentical(4, $collection->next());
     $this->assertTrue($collection->valid());
     $this->assertFalse($collection->next());
     $this->assertFalse($collection->valid());
     $this->assertFalse($collection->current());
     $this->assertIdentical(4, $collection->prev());
     $this->assertTrue($collection->valid());
 }
Beispiel #5
0
 public function export(Source $dataSource, array $options = array())
 {
     $defaults = array('atomic' => true);
     $options += $defaults;
     $data = array();
     $nested = array();
     foreach ($this->_data as $key => $val) {
         if (!is_object($val)) {
             $data[$key] = $val;
             continue;
         }
         $nestedOptions = $options;
         switch (true) {
             case is_a($val, $this->_classes['set']):
                 $nestedOptions = array('atomic' => false) + $options;
             case is_a($val, $this->_classes['entity']):
                 if ($data[$key] = $val->export($dataSource, $nestedOptions)) {
                     $nested[$key] = true;
                 }
                 break;
         }
     }
     if ($options['atomic'] && $this->_exists) {
         $data = array_intersect_key($data, $this->_modified + $nested);
     }
     if ($model = $this->_model) {
         $name = null;
         $options = array('atomic' => false) + $options;
         $relations = new Collection(array('data' => $model::relations()));
         $find = function ($relation) use(&$name) {
             return $relation->fieldName === $name;
         };
         foreach ($this->_relationships as $name => $subObject) {
             if (($rel = $relations->first($find)) && $rel->link == $rel::LINK_EMBEDDED) {
                 $data[$name] = $subObject->export($dataSource, $options);
             }
         }
     }
     return $data;
 }