[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  namespace Negotiation;
   4  
   5  class Negotiator extends AbstractNegotiator
   6  {
   7      /**
   8       * {@inheritdoc}
   9       */
  10      protected function acceptFactory($accept)
  11      {
  12          return new Accept($accept);
  13      }
  14  
  15      /**
  16       * {@inheritdoc}
  17       */
  18      protected function match(AcceptHeader $accept, AcceptHeader $priority, $index)
  19      {
  20          if (!$accept instanceof Accept || !$priority instanceof Accept) {
  21              return null;
  22          }
  23  
  24          $acceptBase = $accept->getBasePart();
  25          $priorityBase = $priority->getBasePart();
  26  
  27          $acceptSub = $accept->getSubPart();
  28          $prioritySub = $priority->getSubPart();
  29  
  30          $intersection = array_intersect_assoc($accept->getParameters(), $priority->getParameters());
  31  
  32          $baseEqual = !strcasecmp($acceptBase, $priorityBase);
  33          $subEqual  = !strcasecmp($acceptSub, $prioritySub);
  34  
  35          if (($acceptBase === '*' || $baseEqual)
  36              && ($acceptSub === '*' || $subEqual)
  37              && count($intersection) === count($accept->getParameters())
  38          ) {
  39              $score = 100 * $baseEqual + 10 * $subEqual + count($intersection);
  40  
  41              return new AcceptMatch($accept->getQuality() * $priority->getQuality(), $score, $index);
  42          }
  43  
  44          if (!strstr($acceptSub, '+') || !strstr($prioritySub, '+')) {
  45              return null;
  46          }
  47  
  48          // Handle "+" segment wildcards
  49          list($acceptSub, $acceptPlus) = $this->splitSubPart($acceptSub);
  50          list($prioritySub, $priorityPlus) = $this->splitSubPart($prioritySub);
  51  
  52          // If no wildcards in either the subtype or + segment, do nothing.
  53          if (!($acceptBase === '*' || $baseEqual)
  54              || !($acceptSub === '*' || $prioritySub === '*' || $acceptPlus === '*' || $priorityPlus === '*')
  55          ) {
  56              return null;
  57          }
  58  
  59          $subEqual  = !strcasecmp($acceptSub, $prioritySub);
  60          $plusEqual = !strcasecmp($acceptPlus, $priorityPlus);
  61  
  62          if (($acceptSub === '*' || $prioritySub === '*' || $subEqual)
  63              && ($acceptPlus === '*' || $priorityPlus === '*' || $plusEqual)
  64              && count($intersection) === count($accept->getParameters())
  65          ) {
  66              $score = 100 * $baseEqual + 10 * $subEqual + $plusEqual + count($intersection);
  67  
  68              return new AcceptMatch($accept->getQuality() * $priority->getQuality(), $score, $index);
  69          }
  70  
  71          return null;
  72      }
  73  
  74      /**
  75       * Split a subpart into the subpart and "plus" part.
  76       *
  77       * For media-types of the form "application/vnd.example+json", matching
  78       * should allow wildcards for either the portion before the "+" or
  79       * after. This method splits the subpart to allow such matching.
  80       */
  81      protected function splitSubPart($subPart)
  82      {
  83          if (!strstr($subPart, '+')) {
  84              return [$subPart, ''];
  85          }
  86  
  87          return explode('+', $subPart, 2);
  88      }
  89  }


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