Skip to content

Commit

Permalink
Merge pull request #79 from SteveTalbot/#77
Browse files Browse the repository at this point in the history
Add support for quality-of-source factor [#77]
  • Loading branch information
willdurand committed Sep 4, 2016
2 parents 1ebc51d + 292065e commit e1dcf01
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 6 deletions.
14 changes: 12 additions & 2 deletions README.md
Expand Up @@ -44,7 +44,7 @@ Usage Examples
$negotiator = new \Negotiation\Negotiator();

$acceptHeader = 'text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8';
$priorities = array('text/html; charset=UTF-8', 'application/json');
$priorities = array('text/html; charset=UTF-8', 'application/json', 'application/xml;q=0.5');

$mediaType = $negotiator->getBest($acceptHeader, $priorities);

Expand Down Expand Up @@ -93,7 +93,17 @@ The `EncodingNegotiator` returns an instance of `AcceptEncoding`.
<?php

$negotiator = new \Negotiation\CharsetNegotiator();
$charset = $negotiator->getBest($acceptHeader, $priorities);

$acceptCharsetHeader = 'ISO-8859-1, UTF-8; q=0.9';
$priorities = array('iso-8859-1;q=0.3', 'utf-8;q=0.9', 'utf-16;q=1.0');

$bestCharset = $negotiator->getBest($acceptCharsetHeader, $priorities);

$type = $bestCharset->getType();
// $type == 'utf-8';

$quality = $bestCharset->getQuality();
// $quality == 0.81
```

The `CharsetNegotiator` returns an instance of `AcceptCharset`.
Expand Down
2 changes: 1 addition & 1 deletion src/Negotiation/AbstractNegotiator.php
Expand Up @@ -74,7 +74,7 @@ protected function match(AcceptHeader $header, AcceptHeader $priority, $index)
if ($equal || $ac === '*') {
$score = 1 * $equal;

return new Match($header->getQuality(), $score, $index);
return new Match($header->getQuality() * $priority->getQuality(), $score, $index);
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Negotiation/LanguageNegotiator.php
Expand Up @@ -33,7 +33,7 @@ protected function match(AcceptHeader $acceptLanguage, AcceptHeader $priority, $
if (($ab == '*' || $baseEqual) && ($as === null || $subEqual)) {
$score = 10 * $baseEqual + $subEqual;

return new Match($acceptLanguage->getQuality(), $score, $index);
return new Match($acceptLanguage->getQuality() * $priority->getQuality(), $score, $index);
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Negotiation/Negotiator.php
Expand Up @@ -35,7 +35,7 @@ protected function match(AcceptHeader $accept, AcceptHeader $priority, $index)
if (($ab === '*' || $baseEqual) && ($as === '*' || $subEqual) && count($intersection) === count($accept->getParameters())) {
$score = 100 * $baseEqual + 10 * $subEqual + count($intersection);

return new Match($accept->getQuality(), $score, $index);
return new Match($accept->getQuality() * $priority->getQuality(), $score, $index);
}

return null;
Expand Down
12 changes: 11 additions & 1 deletion tests/Negotiation/Tests/CharsetNegotiatorTest.php
Expand Up @@ -69,7 +69,10 @@ public static function dataProviderForTestGetBest()
array($pearCharset2, array( 'Big5', 'shift-jis',), 'Big5'),
array('utf-8;q=0.6,iso-8859-5;q=0.9', array( 'iso-8859-5', 'utf-8',), 'iso-8859-5'),
array('', array( 'iso-8859-5', 'utf-8',), null),
array('en, *;q=0.9', array('fr'), 'fr')
array('en, *;q=0.9', array('fr'), 'fr'),
# Quality of source factors
array($pearCharset, array('iso-8859-1;q=0.5', 'utf-8', 'utf-16;q=1.0'), 'utf-8'),
array($pearCharset, array('iso-8859-1;q=0.8', 'utf-8', 'utf-16;q=1.0'), 'iso-8859-1;q=0.8'),
);
}

Expand All @@ -89,6 +92,13 @@ public function testGetBestDoesNotMatchPriorities()
$this->assertNull($this->negotiator->getBest($acceptCharset, $priorities));
}

public function testGetBestRespectsQualityOfSource()
{
$accept = $this->negotiator->getBest('utf-8;q=0.5,iso-8859-1', array('iso-8859-1;q=0.3', 'utf-8;q=0.9', 'utf-16;q=1.0'));
$this->assertInstanceOf('Negotiation\AcceptCharset', $accept);
$this->assertEquals('utf-8', $accept->getType());
}

/**
* @dataProvider dataProviderForTestParseHeader
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/Negotiation/Tests/EncodingNegotiatorTest.php
Expand Up @@ -43,9 +43,18 @@ public static function dataProviderForTestGetBest()
array('gzip;q=1.0, identity; q=0.5, *;q=0', array('identity'), 'identity'),
array('gzip;q=0.5, identity; q=0.5, *;q=0.7', array('bzip', 'foo'), 'bzip'),
array('gzip;q=0.7, identity; q=0.5, *;q=0.7', array('gzip', 'foo'), 'gzip'),
# Quality of source factors
array('gzip;q=0.7,identity', array('identity;q=0.5', 'gzip;q=0.9'), 'gzip;q=0.9'),
);
}

public function testGetBestRespectsQualityOfSource()
{
$accept = $this->negotiator->getBest('gzip;q=0.7,identity', array('identity;q=0.5', 'gzip;q=0.9'));
$this->assertInstanceOf('Negotiation\AcceptEncoding', $accept);
$this->assertEquals('gzip', $accept->getType());
}

/**
* @dataProvider dataProviderForTestParseAcceptHeader
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/Negotiation/Tests/LanguageNegotiatorTest.php
Expand Up @@ -49,9 +49,18 @@ public static function dataProviderForTestGetBest()
array('en; q=0.1, fr; q=0.4, fu; q=0.9, de; q=0.2', array('en', 'fu'), 'fu'),
array('', array('en', 'fu'), new InvalidArgument('The header string should not be empty.')),
array('fr, zh-Hans-CN;q=0.3', array('fr'), 'fr'),
# Quality of source factors
array('en;q=0.5,de', array('de;q=0.3', 'en;q=0.9'), 'en;q=0.9'),
);
}

public function testGetBestRespectsQualityOfSource()
{
$accept = $this->negotiator->getBest('en;q=0.5,de', array('de;q=0.3', 'en;q=0.9'));
$this->assertInstanceOf('Negotiation\AcceptLanguage', $accept);
$this->assertEquals('en', $accept->getType());
}

/**
* @dataProvider dataProviderForTestParseHeader
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/Negotiation/Tests/NegotiatorTest.php
Expand Up @@ -92,9 +92,18 @@ public static function dataProviderForTestGetBest()
array('text/html', array( 'application/rss'), null),
# IE8 Accept header
array('image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, */*', array( 'text/html', 'application/xhtml+xml'), array('text/html', array())),
# Quality of source factors
array($rfcHeader, array('text/html;q=0.4', 'text/plain'), array('text/plain', array())),
);
}

public function testGetBestRespectsQualityOfSource()
{
$accept = $this->negotiator->getBest('text/html,text/*;q=0.7', array('text/html;q=0.5', 'text/plain;q=0.9'));
$this->assertInstanceOf('Negotiation\Accept', $accept);
$this->assertEquals('text/plain', $accept->getType());
}

/**
* @dataProvider dataProviderForTestParseHeader
*/
Expand Down

0 comments on commit e1dcf01

Please sign in to comment.