lithium\util\Collection::first PHP Method

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`.
    public function first($filter = null)
    {
        if (!$filter) {
            return $this->rewind();
        }
        foreach ($this as $item) {
            if ($filter($item)) {
                return $item;
            }
        }
    }

Usage Example

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;
 }
All Usage Examples Of lithium\util\Collection::first