Open Journal Systems  3.3.0
ReadLimitEntityBody.php
1 <?php
2 
3 namespace Guzzle\Http;
4 
6 
11 {
13  protected $limit;
14 
16  protected $offset;
17 
23  public function __construct(EntityBodyInterface $body, $limit, $offset = 0)
24  {
25  parent::__construct($body);
26  $this->setLimit($limit)->setOffset($offset);
27  }
28 
33  public function __toString()
34  {
35  if (!$this->body->isReadable() ||
36  (!$this->body->isSeekable() && $this->body->isConsumed())
37  ) {
38  return '';
39  }
40 
41  $originalPos = $this->body->ftell();
42  $this->body->seek($this->offset);
43  $data = '';
44  while (!$this->feof()) {
45  $data .= $this->read(1048576);
46  }
47  $this->body->seek($originalPos);
48 
49  return (string) $data ?: '';
50  }
51 
52  public function isConsumed()
53  {
54  return $this->body->isConsumed() ||
55  ($this->body->ftell() >= $this->offset + $this->limit);
56  }
57 
62  public function getContentLength()
63  {
64  $length = $this->body->getContentLength();
65 
66  return $length === false
67  ? $this->limit
68  : min($this->limit, min($length, $this->offset + $this->limit) - $this->offset);
69  }
70 
75  public function seek($offset, $whence = SEEK_SET)
76  {
77  return $whence === SEEK_SET
78  ? $this->body->seek(max($this->offset, min($this->offset + $this->limit, $offset)))
79  : false;
80  }
81 
89  public function setOffset($offset)
90  {
91  $this->body->seek($offset);
92  $this->offset = $offset;
93 
94  return $this;
95  }
96 
104  public function setLimit($limit)
105  {
106  $this->limit = $limit;
107 
108  return $this;
109  }
110 
111  public function read($length)
112  {
113  // Check if the current position is less than the total allowed bytes + original offset
114  $remaining = ($this->offset + $this->limit) - $this->body->ftell();
115  if ($remaining > 0) {
116  // Only return the amount of requested data, ensuring that the byte limit is not exceeded
117  return $this->body->read(min($remaining, $length));
118  } else {
119  return false;
120  }
121  }
122 }
Guzzle\Http\ReadLimitEntityBody\isConsumed
isConsumed()
Definition: ReadLimitEntityBody.php:58
Guzzle\Http\ReadLimitEntityBody\read
read($length)
Definition: ReadLimitEntityBody.php:117
Guzzle\Http\AbstractEntityBodyDecorator\$body
$body
Definition: AbstractEntityBodyDecorator.php:16
Guzzle\Http\EntityBodyInterface
Definition: EntityBodyInterface.php:10
Guzzle\Http\ReadLimitEntityBody\$limit
$limit
Definition: ReadLimitEntityBody.php:16
Guzzle\Http\ReadLimitEntityBody\__toString
__toString()
Definition: ReadLimitEntityBody.php:39
Guzzle\Http\AbstractEntityBodyDecorator
Definition: AbstractEntityBodyDecorator.php:10
Guzzle\Http\ReadLimitEntityBody\getContentLength
getContentLength()
Definition: ReadLimitEntityBody.php:68
Guzzle\Http\ReadLimitEntityBody\setOffset
setOffset($offset)
Definition: ReadLimitEntityBody.php:95
Guzzle\Http\ReadLimitEntityBody\__construct
__construct(EntityBodyInterface $body, $limit, $offset=0)
Definition: ReadLimitEntityBody.php:29
Guzzle\Http\ReadLimitEntityBody
Definition: ReadLimitEntityBody.php:10
Guzzle\Http\ReadLimitEntityBody\setLimit
setLimit($limit)
Definition: ReadLimitEntityBody.php:110
Guzzle\Http\AbstractEntityBodyDecorator\feof
feof()
Definition: AbstractEntityBodyDecorator.php:166
Guzzle\Http
Definition: AbstractEntityBodyDecorator.php:3
Guzzle\Stream\StreamInterface
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Stream/StreamInterface.php:8
Guzzle\Http\ReadLimitEntityBody\$offset
$offset
Definition: ReadLimitEntityBody.php:22
Guzzle\Http\ReadLimitEntityBody\seek
seek($offset, $whence=SEEK_SET)
Definition: ReadLimitEntityBody.php:81