Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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  }