Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
namespace Kevinrob\GuzzleCache;
4
 
5
/**
6
 *
7
 * This object is only meant to provide a callable to `GuzzleHttp\Psr7\PumpStream`.
8
 *
9
 * @internal don't use it in your project.
10
 */
11
class BodyStore
12
{
13
    private $body;
14
 
15
    private $read = 0;
16
 
17
    private $toRead;
18
 
19
    public function __construct(string $body)
20
    {
21
        $this->body = $body;
22
        $this->toRead = mb_strlen($this->body);
23
    }
24
 
25
    /**
26
     * @param int $length
27
     * @return false|string
28
     */
29
    public function __invoke(int $length)
30
    {
31
        if ($this->toRead <= 0) {
32
            return false;
33
        }
34
 
35
        $length = min($length, $this->toRead);
36
 
37
        $body = mb_substr(
38
            $this->body,
39
            $this->read,
40
            $length
41
        );
42
        $this->toRead -= $length;
43
        $this->read += $length;
44
        return $body;
45
    }
46
}