Qaribou\Collection\ImmArray::fromItems PHP Method

fromItems() public static method

Factory for building ImmArrays from any traversable
public static fromItems ( Traversable $arr ) : ImmArray
$arr Traversable
return ImmArray
    public static function fromItems(Traversable $arr)
    {
        // We can only do it this way if we can count it
        if ($arr instanceof Countable) {
            $sfa = new SplFixedArray(count($arr));
            foreach ($arr as $i => $el) {
                $sfa[$i] = $el;
            }
            return new static($sfa);
        }
        // If we can't count it, it's simplest to iterate into an array first
        return static::fromArray(iterator_to_array($arr));
    }

Usage Example

Exemplo n.º 1
0
 public function testLoadBigSet()
 {
     $startMem = ini_get('memory_limit');
     ini_set('memory_limit', '50M');
     // Big
     $bigSet = ImmArray::fromItems(new MD5Iterator(200000));
     $this->assertCount(200000, $bigSet);
     ini_set('memory_limit', $startMem);
 }