GuzzleHttp\Psr7\LimitStream::seek PHP Method

seek() public method

Allow for a bounded seek on the read limited stream {@inheritdoc}
public seek ( $offset, $whence = SEEK_SET )
    public function seek($offset, $whence = SEEK_SET)
    {
        if ($whence !== SEEK_SET || $offset < 0) {
            throw new \RuntimeException(sprintf('Cannot seek to offset % with whence %s', $offset, $whence));
        }
        $offset += $this->offset;
        if ($this->limit !== -1) {
            if ($offset > $this->offset + $this->limit) {
                $offset = $this->offset + $this->limit;
            }
        }
        $this->stream->seek($offset);
    }

Usage Example

Ejemplo n.º 1
0
 public function testAllowsBoundedSeek()
 {
     $this->body->seek(100);
     $this->assertEquals(10, $this->body->tell());
     $this->assertEquals(13, $this->decorated->tell());
     $this->body->seek(0);
     $this->assertEquals(0, $this->body->tell());
     $this->assertEquals(3, $this->decorated->tell());
     try {
         $this->body->seek(-10);
         $this->fail();
     } catch (\RuntimeException $e) {
     }
     $this->assertEquals(0, $this->body->tell());
     $this->assertEquals(3, $this->decorated->tell());
     $this->body->seek(5);
     $this->assertEquals(5, $this->body->tell());
     $this->assertEquals(8, $this->decorated->tell());
     // Fail
     try {
         $this->body->seek(1000, SEEK_END);
         $this->fail();
     } catch (\RuntimeException $e) {
     }
 }