Collections\Collection::slice PHP Method

slice() public method

public slice ( $start, $end )
    public function slice($start, $end)
    {
        if (!is_integer($start) || $start < 0) {
            throw new InvalidArgumentException("Start must be a non-negative integer");
        }
        if (!is_integer($end) || $end < 0) {
            throw new InvalidArgumentException("End must be a positive integer");
        }
        if ($start > $end) {
            throw new InvalidArgumentException("End must be greater than start");
        }
        if ($end > $this->count() + 1) {
            throw new InvalidArgumentException("End must be less than the count of the items in the Collection");
        }
        $length = $end - $start + 1;
        $subsetItems = array_slice($this->items, $start, $length);
        $col = new static($this->type);
        $col->setItemsFromTrustedSource($subsetItems);
        return $col;
    }

Usage Example

 public function test_subset_with_only_end()
 {
     $subset = $this->c->slice(9, 9);
     $this->assertEquals(1, $subset->count());
     $this->assertEquals($this->items[9], $subset->at(0));
 }