Skip to content

Commit

Permalink
moving to more flexible negotiation
Browse files Browse the repository at this point in the history
  • Loading branch information
winmillwill committed Jul 27, 2012
1 parent 87c3af4 commit 9f80653
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 66 deletions.
43 changes: 2 additions & 41 deletions lib/BadFaith/Negotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ function headersFromArg(array $arg)

function variantsFromArg(array $arg)
{
foreach ($arg as $key => $val) {
$this->variants[$key] = new Variant($val);
}
$this->variants = new VariantList($arg);
}

/**
Expand Down Expand Up @@ -125,44 +123,7 @@ function listClass($type)
return __NAMESPACE__ . '\\' . $class;
}

/**
* @param string|null
* @return string|array
*/
function getPreferred($type = null)
{
if (null === $type) {
$return = array();
foreach ($this->headerLists as $name => $list) {
$return[$name] = $list->getPreferred()->getPref();
}
} else {
$return = $this->headerLists["accept_{$type}"]->getPreferred()->getPref();
}

return $return;
}

/**
* @param string
*/
function getBestVariant($type)
function apacheNegotiate()
{
$lookup = "accept_{$type}";

if (!isset($this->headerLists[$lookup])) {
throw new \UnexpectedValueException("{$type} not found");
}

foreach ($this->headerLists[$lookup]->items as $item) {
foreach ($this->variants[$lookup]->items as $varItem) {
if ($item->getPref() == $varItem->getPref()) {
return $item->getPref();
}
}
}

// If the client and server can't negotiate, return the services preference
return $this->variants["accept_{$type}"]->getPreferred()->getPref();
}
}
9 changes: 9 additions & 0 deletions lib/BadFaith/VariantList.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public function getPrefHash($dimension)
return $this->prefHashes[$dimension];
}

public function getPrefHashes()
{
$hashes = array();
foreach ($this->prefHashes as $dimension => $hash) {
$hashes[$dimension] = $this->getPrefHash($dimension);
}
return $hashes;
}

/**
* {@inheritdoc}
*/
Expand Down
25 changes: 0 additions & 25 deletions tests/BadFaith/Tests/NegotiatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,4 @@ public function testFaultTolerence()

$this->assertGreaterThan(count($missing), count($negotiator->headerLists));
}

public function testGetPreferredString()
{
$negotiator = new Negotiator($this->headers);

$this->assertEquals('gzip', $negotiator->getPreferred('encoding'));
}

public function testGetPreferredArray()
{
$negotiator = new Negotiator($this->headers);

$expected = array(
'accept' => 'text/html'
, 'accept_charset' => 'ISO-8859-1'
, 'accept_encoding' => 'gzip'
, 'accept_language' => 'en-US'
);

$this->assertEquals($expected, $negotiator->getPreferred());
}

public function testModNegotiation()
{
}
}

0 comments on commit 9f80653

Please sign in to comment.