[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

/vendor/symfony/http-foundation/ -> AcceptHeader.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.
  16   *
  17   * An accept header is compound with a list of items,
  18   * sorted by descending quality.
  19   *
  20   * @author Jean-Fran├žois Simon <contact@jfsimon.fr>
  21   */
  22  class AcceptHeader
  23  {
  24      /**
  25       * @var AcceptHeaderItem[]
  26       */
  27      private $items = [];
  28  
  29      /**
  30       * @var bool
  31       */
  32      private $sorted = true;
  33  
  34      /**
  35       * @param AcceptHeaderItem[] $items
  36       */
  37      public function __construct(array $items)
  38      {
  39          foreach ($items as $item) {
  40              $this->add($item);
  41          }
  42      }
  43  
  44      /**
  45       * Builds an AcceptHeader instance from a string.
  46       *
  47       * @param string $headerValue
  48       *
  49       * @return self
  50       */
  51      public static function fromString($headerValue)
  52      {
  53          $index = 0;
  54  
  55          return new self(array_map(function ($itemValue) use (&$index) {
  56              $item = AcceptHeaderItem::fromString($itemValue);
  57              $item->setIndex($index++);
  58  
  59              return $item;
  60          }, preg_split('/\s*(?:,*("[^"]+"),*|,*(\'[^\']+\'),*|,+)\s*/', $headerValue, 0, \PREG_SPLIT_NO_EMPTY | \PREG_SPLIT_DELIM_CAPTURE)));
  61      }
  62  
  63      /**
  64       * Returns header value's string representation.
  65       *
  66       * @return string
  67       */
  68      public function __toString()
  69      {
  70          return implode(',', $this->items);
  71      }
  72  
  73      /**
  74       * Tests if header has given value.
  75       *
  76       * @param string $value
  77       *
  78       * @return bool
  79       */
  80      public function has($value)
  81      {
  82          return isset($this->items[$value]);
  83      }
  84  
  85      /**
  86       * Returns given value's item, if exists.
  87       *
  88       * @param string $value
  89       *
  90       * @return AcceptHeaderItem|null
  91       */
  92      public function get($value)
  93      {
  94          return isset($this->items[$value]) ? $this->items[$value] : null;
  95      }
  96  
  97      /**
  98       * Adds an item.
  99       *
 100       * @return $this
 101       */
 102      public function add(AcceptHeaderItem $item)
 103      {
 104          $this->items[$item->getValue()] = $item;
 105          $this->sorted = false;
 106  
 107          return $this;
 108      }
 109  
 110      /**
 111       * Returns all items.
 112       *
 113       * @return AcceptHeaderItem[]
 114       */
 115      public function all()
 116      {
 117          $this->sort();
 118  
 119          return $this->items;
 120      }
 121  
 122      /**
 123       * Filters items on their value using given regex.
 124       *
 125       * @param string $pattern
 126       *
 127       * @return self
 128       */
 129      public function filter($pattern)
 130      {
 131          return new self(array_filter($this->items, function (AcceptHeaderItem $item) use ($pattern) {
 132              return preg_match($pattern, $item->getValue());
 133          }));
 134      }
 135  
 136      /**
 137       * Returns first item.
 138       *
 139       * @return AcceptHeaderItem|null
 140       */
 141      public function first()
 142      {
 143          $this->sort();
 144  
 145          return !empty($this->items) ? reset($this->items) : null;
 146      }
 147  
 148      /**
 149       * Sorts items by descending quality.
 150       */
 151      private function sort()
 152      {
 153          if (!$this->sorted) {
 154              uasort($this->items, function (AcceptHeaderItem $a, AcceptHeaderItem $b) {
 155                  $qA = $a->getQuality();
 156                  $qB = $b->getQuality();
 157  
 158                  if ($qA === $qB) {
 159                      return $a->getIndex() > $b->getIndex() ? 1 : -1;
 160                  }
 161  
 162                  return $qA > $qB ? -1 : 1;
 163              });
 164  
 165              $this->sorted = true;
 166          }
 167      }
 168  }


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1