Flow\Helper::first PHP Méthode

first() public static méthode

public static first ( $obj = null, $default = null )
    public static function first($obj = null, $default = null)
    {
        if (is_string($obj)) {
            return strlen($obj) ? substr($obj, 0, 1) : $default;
        }
        $obj = $obj instanceof \Traversable ? iterator_to_array($obj) : (array) $obj;
        $keys = array_keys($obj);
        if (count($keys)) {
            return $obj[$keys[0]];
        }
        return $default;
    }

Usage Example

Exemple #1
0
 public function test_first()
 {
     $string = 'Hello, World!';
     $this->assertEquals('H', Helper::first($string));
     $array = array(1, 2, 3);
     $this->assertEquals(1, Helper::first($array));
     $arrayIterator = new \ArrayIterator($array);
     $this->assertEquals(1, Helper::first($arrayIterator));
     $object = new \StdClass();
     $object->foo = 'foo';
     $object->bar = 'bar';
     $this->assertEquals('foo', Helper::first($object));
     $this->assertEquals(42, Helper::first(array(), 42));
 }