[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/http-foundation/ -> AcceptHeaderItem.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\HttpFoundation;
  13  
  14  /**
  15   * Represents an Accept-* header item.
  16   *
  17   * @author Jean-François Simon <contact@jfsimon.fr>
  18   */
  19  class AcceptHeaderItem
  20  {
  21      private $value;
  22      private $quality = 1.0;
  23      private $index = 0;
  24      private $attributes = array();
  25  
  26      /**
  27       * @param string $value
  28       * @param array  $attributes
  29       */
  30      public function __construct($value, array $attributes = array())
  31      {
  32          $this->value = $value;
  33          foreach ($attributes as $name => $value) {
  34              $this->setAttribute($name, $value);
  35          }
  36      }
  37  
  38      /**
  39       * Builds an AcceptHeaderInstance instance from a string.
  40       *
  41       * @param string $itemValue
  42       *
  43       * @return self
  44       */
  45      public static function fromString($itemValue)
  46      {
  47          $bits = preg_split('/\s*(?:;*("[^"]+");*|;*(\'[^\']+\');*|;+)\s*/', $itemValue, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  48          $value = array_shift($bits);
  49          $attributes = array();
  50  
  51          $lastNullAttribute = null;
  52          foreach ($bits as $bit) {
  53              if (($start = substr($bit, 0, 1)) === ($end = substr($bit, -1)) && ('"' === $start || '\'' === $start)) {
  54                  $attributes[$lastNullAttribute] = substr($bit, 1, -1);
  55              } elseif ('=' === $end) {
  56                  $lastNullAttribute = $bit = substr($bit, 0, -1);
  57                  $attributes[$bit] = null;
  58              } else {
  59                  $parts = explode('=', $bit);
  60                  $attributes[$parts[0]] = isset($parts[1]) && \strlen($parts[1]) > 0 ? $parts[1] : '';
  61              }
  62          }
  63  
  64          return new self(($start = substr($value, 0, 1)) === ($end = substr($value, -1)) && ('"' === $start || '\'' === $start) ? substr($value, 1, -1) : $value, $attributes);
  65      }
  66  
  67      /**
  68       * Returns header value's string representation.
  69       *
  70       * @return string
  71       */
  72      public function __toString()
  73      {
  74          $string = $this->value.($this->quality < 1 ? ';q='.$this->quality : '');
  75          if (\count($this->attributes) > 0) {
  76              $string .= ';'.implode(';', array_map(function ($name, $value) {
  77                  return sprintf(preg_match('/[,;=]/', $value) ? '%s="%s"' : '%s=%s', $name, $value);
  78              }, array_keys($this->attributes), $this->attributes));
  79          }
  80  
  81          return $string;
  82      }
  83  
  84      /**
  85       * Set the item value.
  86       *
  87       * @param string $value
  88       *
  89       * @return $this
  90       */
  91      public function setValue($value)
  92      {
  93          $this->value = $value;
  94  
  95          return $this;
  96      }
  97  
  98      /**
  99       * Returns the item value.
 100       *
 101       * @return string
 102       */
 103      public function getValue()
 104      {
 105          return $this->value;
 106      }
 107  
 108      /**
 109       * Set the item quality.
 110       *
 111       * @param float $quality
 112       *
 113       * @return $this
 114       */
 115      public function setQuality($quality)
 116      {
 117          $this->quality = $quality;
 118  
 119          return $this;
 120      }
 121  
 122      /**
 123       * Returns the item quality.
 124       *
 125       * @return float
 126       */
 127      public function getQuality()
 128      {
 129          return $this->quality;
 130      }
 131  
 132      /**
 133       * Set the item index.
 134       *
 135       * @param int $index
 136       *
 137       * @return $this
 138       */
 139      public function setIndex($index)
 140      {
 141          $this->index = $index;
 142  
 143          return $this;
 144      }
 145  
 146      /**
 147       * Returns the item index.
 148       *
 149       * @return int
 150       */
 151      public function getIndex()
 152      {
 153          return $this->index;
 154      }
 155  
 156      /**
 157       * Tests if an attribute exists.
 158       *
 159       * @param string $name
 160       *
 161       * @return bool
 162       */
 163      public function hasAttribute($name)
 164      {
 165          return isset($this->attributes[$name]);
 166      }
 167  
 168      /**
 169       * Returns an attribute by its name.
 170       *
 171       * @param string $name
 172       * @param mixed  $default
 173       *
 174       * @return mixed
 175       */
 176      public function getAttribute($name, $default = null)
 177      {
 178          return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
 179      }
 180  
 181      /**
 182       * Returns all attributes.
 183       *
 184       * @return array
 185       */
 186      public function getAttributes()
 187      {
 188          return $this->attributes;
 189      }
 190  
 191      /**
 192       * Set an attribute.
 193       *
 194       * @param string $name
 195       * @param string $value
 196       *
 197       * @return $this
 198       */
 199      public function setAttribute($name, $value)
 200      {
 201          if ('q' === $name) {
 202              $this->quality = (float) $value;
 203          } else {
 204              $this->attributes[$name] = (string) $value;
 205          }
 206  
 207          return $this;
 208      }
 209  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1