- md5
- 078b30364a4306df2735fdb3a8c854cc
- sha1
- 15d9238b768bd46a3448a2d624083a0878805b03
- sha256
- 23745dc22da5e1f9741b5061b5a1f66713bd6ba5ba2db6ed5ce1ee1b45be45e3
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
<?php
namespace Negotiation;
class Negotiator extends AbstractNegotiator
{
/**
* {@inheritdoc}
*/
protected function acceptFactory($accept)
{
return new Accept($accept);
}
/**
* {@inheritdoc}
*/
protected function match(AcceptHeader $accept, AcceptHeader $priority, $index)
{
if (!$accept instanceof Accept || !$priority instanceof Accept) {
return null;
}
$acceptBase = $accept->getBasePart();
$priorityBase = $priority->getBasePart();
$acceptSub = $accept->getSubPart();
$prioritySub = $priority->getSubPart();
$intersection = array_intersect_assoc($accept->getParameters(), $priority->getParameters());
$baseEqual = !strcasecmp($acceptBase, $priorityBase);
$subEqual = !strcasecmp($acceptSub, $prioritySub);
if (($acceptBase === '*' || $baseEqual)
&& ($acceptSub === '*' || $subEqual)
&& count($intersection) === count($accept->getParameters())
) {
$score = 100 * $baseEqual + 10 * $subEqual + count($intersection);
return new AcceptMatch($accept->getQuality() * $priority->getQuality(), $score, $index);
}
if (!strstr($acceptSub, '+') || !strstr($prioritySub, '+')) {
return null;
}
// Handle "+" segment wildcards
list($acceptSub, $acceptPlus) = $this->splitSubPart($acceptSub);
list($prioritySub, $priorityPlus) = $this->splitSubPart($prioritySub);
// If no wildcards in either the subtype or + segment, do nothing.
if (!($acceptBase === '*' || $baseEqual)
|| !($acceptSub === '*' || $prioritySub === '*' || $acceptPlus === '*' || $priorityPlus === '*')
) {
return null;
}
$subEqual = !strcasecmp($acceptSub, $prioritySub);
$plusEqual = !strcasecmp($acceptPlus, $priorityPlus);
if (($acceptSub === '*' || $prioritySub === '*' || $subEqual)
&& ($acceptPlus === '*' || $priorityPlus === '*' || $plusEqual)
&& count($intersection) === count($accept->getParameters())
) {
$score = 100 * $baseEqual + 10 * $subEqual + $plusEqual + count($intersection);
return new AcceptMatch($accept->getQuality() * $priority->getQuality(), $score, $index);
}
return null;
}
/**
* Split a subpart into the subpart and "plus" part.
*
* For media-types of the form "application/vnd.example+json", matching
* should allow wildcards for either the portion before the "+" or
* after. This method splits the subpart to allow such matching.
*/
protected function splitSubPart($subPart)
{
if (!strstr($subPart, '+')) {
return [$subPart, ''];
}
return explode('+', $subPart, 2);
}
}
The same content is in 282 releases, Joomla 4.0.0-rc1 to 6.3.0-alpha1. See them all, or every version of this file.