Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   1  <?php
   2  
   3  namespace Kevinrob\GuzzleCache;
   4  
   5  class KeyValueHttpHeader implements \Iterator
   6  {
   7      /**
   8       * Take from https://github.com/hapijs/wreck.
   9       */
  10      const REGEX_SPLIT = '/(?:^|(?:\s*\,\s*))([^\x00-\x20\(\)<>@\,;\:\\\\"\/\[\]\?\=\{\}\x7F]+)(?:\=(?:([^\x00-\x20\(\)<>@\,;\:\\\\"\/\[\]\?\=\{\}\x7F]+)|(?:\"((?:[^"\\\\]|\\\\.)*)\")))?/';
  11  
  12      /**
  13       * @var string[]
  14       */
  15      protected $values = [];
  16  
  17      /**
  18       * @param array $values
  19       */
  20      public function __construct(array $values)
  21      {
  22          foreach ($values as $value) {
  23              $matches = [];
  24              if (preg_match_all(self::REGEX_SPLIT, $value, $matches, PREG_SET_ORDER)) {
  25                  foreach ($matches as $match) {
  26                      $val = '';
  27                      if (count($match) == 3) {
  28                          $val = $match[2];
  29                      } elseif (count($match) > 3) {
  30                          $val = $match[3];
  31                      }
  32  
  33                      $this->values[$match[1]] = $val;
  34                  }
  35              }
  36          }
  37      }
  38  
  39      /**
  40       * @param string $key
  41       *
  42       * @return bool
  43       */
  44      public function has($key)
  45      {
  46          // For performance, we can use isset,
  47          // but it will not match if value == 0
  48          return isset($this->values[$key]) || array_key_exists($key, $this->values);
  49      }
  50  
  51      /**
  52       * @param string $key
  53       * @param string $default the value to return if don't exist
  54       * @return string
  55       */
  56      public function get($key, $default = '')
  57      {
  58          if ($this->has($key)) {
  59              return $this->values[$key];
  60          }
  61  
  62          return $default;
  63      }
  64  
  65      /**
  66       * @return bool
  67       */
  68      public function isEmpty()
  69      {
  70          return count($this->values) === 0;
  71      }
  72  
  73      /**
  74       * Return the current element
  75       * @link http://php.net/manual/en/iterator.current.php
  76       * @return mixed Can return any type.
  77       * @since 5.0.0
  78       */
  79      #[\ReturnTypeWillChange]
  80      public function current()
  81      {
  82          return current($this->values);
  83      }
  84  
  85      /**
  86       * Move forward to next element
  87       * @link http://php.net/manual/en/iterator.next.php
  88       * @return void Any returned value is ignored.
  89       * @since 5.0.0
  90       */
  91      public function next(): void
  92      {
  93          next($this->values);
  94      }
  95  
  96      /**
  97       * Return the key of the current element
  98       * @link http://php.net/manual/en/iterator.key.php
  99       * @return mixed scalar on success, or null on failure.
 100       * @since 5.0.0
 101       *
 102       */
 103      #[\ReturnTypeWillChange]
 104      public function key()
 105      {
 106          return key($this->values);
 107      }
 108  
 109      /**
 110       * Checks if current position is valid
 111       * @link http://php.net/manual/en/iterator.valid.php
 112       * @return boolean The return value will be casted to boolean and then evaluated.
 113       * Returns true on success or false on failure.
 114       * @since 5.0.0
 115       */
 116      public function valid(): bool
 117      {
 118          return key($this->values) !== null;
 119      }
 120  
 121      /**
 122       * Rewind the Iterator to the first element
 123       * @link http://php.net/manual/en/iterator.rewind.php
 124       * @return void Any returned value is ignored.
 125       * @since 5.0.0
 126       */
 127      public function rewind(): void
 128      {
 129          reset($this->values);
 130      }
 131  }