/** * Negotiate the PSR-7 request using the given configuration. * * If the negotiation fails a NegotiationException will be thrown. If the * accept header is empty the default value (with highest priority) will * be returned (if required by argument). * * The return value will never be <code>null</code>. * * @param request ServerRequestInterface the PSR-7 request * @param $conf Configuration negotiation configuration * @param $supplyDefault bool whether default value is supplied * @return BaseAccept negotiation result * @throws NegotiationException negotiation failed */ public function negotiate(ServerRequestInterface $request, Configuration $conf, $supplyDefault) { $headerLine = $request->getHeaderLine($conf->getHeaderName()); if (empty($headerLine)) { // accept header is empty $result = $this->handleNoInput($conf, $supplyDefault); } else { // accept header is available $result = $this->handleInput($conf, $headerLine); } if (!is_null($result)) { // negotiation result available return $result; } throw new NegotiationException('accept header refused'); }
function testGetters() { $headerName = 'the name'; $negotiator = new LanguageNegotiator(); $priorities = ['one', 'two']; $acceptFactory = function () { }; $c = new Configuration(); $c->setHeaderName($headerName); $c->setPriorities($priorities); $c->setNegotiator($negotiator); $c->setAcceptFactory($acceptFactory); $this->assertSame($headerName, $c->getHeaderName()); $this->assertSame($negotiator, $c->getNegotiator()); $this->assertSame($priorities, $c->getPriorities()); }