[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/willdurand/negotiation/src/Negotiation/ -> BaseAccept.php (source)

   1  <?php
   2  
   3  namespace Negotiation;
   4  
   5  abstract class BaseAccept
   6  {
   7      /**
   8       * @var float
   9       */
  10      private $quality = 1.0;
  11  
  12      /**
  13       * @var string
  14       */
  15      private $normalized;
  16  
  17      /**
  18       * @var string
  19       */
  20      private $value;
  21  
  22      /**
  23       * @var array
  24       */
  25      private $parameters;
  26  
  27      /**
  28       * @var string
  29       */
  30      protected $type;
  31  
  32      /**
  33       * @param string $value
  34       */
  35      public function __construct($value)
  36      {
  37          list($type, $parameters) = $this->parseParameters($value);
  38  
  39          if (isset($parameters['q'])) {
  40              $this->quality = (float) $parameters['q'];
  41              unset($parameters['q']);
  42          }
  43  
  44          $type = trim(strtolower($type));
  45  
  46          $this->value      = $value;
  47          $this->normalized = $type . ($parameters ? "; " . $this->buildParametersString($parameters) : '');
  48          $this->type       = $type;
  49          $this->parameters = $parameters;
  50      }
  51  
  52      /**
  53       * @return string
  54       */
  55      public function getNormalizedValue()
  56      {
  57          return $this->normalized;
  58      }
  59  
  60      /**
  61       * @return string
  62       */
  63      public function getValue()
  64      {
  65          return $this->value;
  66      }
  67  
  68      /**
  69       * @return string
  70       */
  71      public function getType()
  72      {
  73          return $this->type;
  74      }
  75  
  76      /**
  77       * @return float
  78       */
  79      public function getQuality()
  80      {
  81          return $this->quality;
  82      }
  83  
  84      /**
  85       * @return array
  86       */
  87      public function getParameters()
  88      {
  89          return $this->parameters;
  90      }
  91  
  92      /**
  93       * @param string $key
  94       * @param mixed  $default
  95       *
  96       * @return string|null
  97       */
  98      public function getParameter($key, $default = null)
  99      {
 100          return isset($this->parameters[$key]) ? $this->parameters[$key] : $default;
 101      }
 102  
 103      /**
 104       * @param string $key
 105       *
 106       * @return boolean
 107       */
 108      public function hasParameter($key)
 109      {
 110          return isset($this->parameters[$key]);
 111      }
 112  
 113      /**
 114       *
 115       * @param  string|null $acceptPart
 116       * @return array
 117       */
 118      private function parseParameters($acceptPart)
 119      {
 120          if ($acceptPart === null) {
 121              return ['', []];
 122          }
 123  
 124          $parts = explode(';', $acceptPart);
 125          $type  = array_shift($parts);
 126  
 127          $parameters = [];
 128          foreach ($parts as $part) {
 129              $part = explode('=', $part);
 130  
 131              if (2 !== count($part)) {
 132                  continue; // TODO: throw exception here?
 133              }
 134  
 135              $key = strtolower(trim($part[0])); // TODO: technically not allowed space around "=". throw exception?
 136              $parameters[$key] = trim($part[1], ' "');
 137          }
 138  
 139          return [ $type, $parameters ];
 140      }
 141  
 142      /**
 143       * @param string $parameters
 144       *
 145       * @return string
 146       */
 147      private function buildParametersString($parameters)
 148      {
 149          $parts = [];
 150  
 151          ksort($parameters);
 152          foreach ($parameters as $key => $val) {
 153              $parts[] = sprintf('%s=%s', $key, $val);
 154          }
 155  
 156          return implode('; ', $parts);
 157      }
 158  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer