<commit_before><?php
/**
 * This file is part of stubbles.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @package  net\stubbles\webapp
 */
namespace net\stubbles\webapp\response;
use net\stubbles\input\web\WebRequest;
use net\stubbles\ioc\Injector;
/**
 * Negotiates correct response for request.
 *
 * @since  2.0.0
 */
class ResponseNegotiator
{
    /**
     * base response implementation
     *
     * @type  Response
     */
    private $response;
    /**
     * injector instance
     *
     * @type  Injector
     */
    private $injector;

    /**
     * constructor
     *
     * @param  Response  $response  base response implementation
     * @param  Injector  $injector  injector to create required formatter with
     * @Inject
     */
    public function __construct(Response $response,
                                Injector $injector)
    {
        $this->response = $response;
        $this->injector = $injector;
    }

    /**
     * negotiates http response for http version
     *
     * Forces 505 HTTP Version Not Supported response in case the request
     * has a non-supported protocol version and cancels the request.
     *
     * @param   WebRequest  $request
     * @param   string      $responseClass
     * @return  Response
     */
    public static function negotiateHttpVersion(WebRequest $request, $responseClass = 'net\stubbles\webapp\response\WebResponse')
    {
        $httpVersion = $request->getProtocolVersion();
        if (null === $httpVersion) {
            $response = new $responseClass();
            return $response->httpVersionNotSupported();
        }

        return new $responseClass($httpVersion);
    }

    /**
     * negotiates mime type based on accept header and configured mime types
     *
     * Forces a 406 Not Acceptable response in case none of the accepted user
     * agent mime types is supported. Forces a 500 Internal Server Error
     * response in case a mime type but no suitable formatter was found.
     *
     * @param   WebRequest  $request
     * @param   Routing     $routing
     * @return  Response
     */
    public function negotiateMimeType(WebRequest $request, SupportedMimeTypes $supportedMimeTypes)
    {
        if ($supportedMimeTypes->isContentNegotationDisabled()) {
            return $this->response;
        }

        $mimeType = $supportedMimeTypes->findMatch($request->readHeader('HTTP_ACCEPT')
                                                           ->applyFilter(new \net\stubbles\input\filter\AcceptFilter())
                    );
        if (null === $mimeType) {
            return $this->response->notAcceptable($supportedMimeTypes->asArray());
        }

        $formatter = $this->createFormatter($mimeType, $supportedMimeTypes);
        if (null === $formatter) {
            return $this->response->internalServerError('No formatter defined for negotiated content type ' . $mimeType);
        }

        return new FormattingResponse($this->response, $formatter, $mimeType);
    }

    /**
     * creates formatter instance
     *
     * @param   string  $mimeType
     * @return  format\Formatter
     */
    private function createFormatter($mimeType, SupportedMimeTypes $supportedMimeTypes)
    {
        if ($supportedMimeTypes->hasFormatter($mimeType)) {
            return $this->injector->getInstance($supportedMimeTypes->getFormatter($mimeType));
        }

        if ($this->injector->hasBinding('net\stubbles\webapp\response\format\Formatter', $mimeType)) {
            return $this->injector->getInstance('net\stubbles\webapp\response\format\Formatter', $mimeType);
        }

        return null;
    }
}
<commit_msg>fix doc comment<commit_after><?php
/**
 * This file is part of stubbles.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @package  net\stubbles\webapp
 */
namespace net\stubbles\webapp\response;
use net\stubbles\input\web\WebRequest;
use net\stubbles\ioc\Injector;
/**
 * Negotiates correct response for request.
 *
 * @since  2.0.0
 */
class ResponseNegotiator
{
    /**
     * base response implementation
     *
     * @type  Response
     */
    private $response;
    /**
     * injector instance
     *
     * @type  Injector
     */
    private $injector;

    /**
     * constructor
     *
     * @param  Response  $response  base response implementation
     * @param  Injector  $injector  injector to create required formatter with
     * @Inject
     */
    public function __construct(Response $response,
                                Injector $injector)
    {
        $this->response = $response;
        $this->injector = $injector;
    }

    /**
     * negotiates http response for http version
     *
     * Forces 505 HTTP Version Not Supported response in case the request
     * has a non-supported protocol version and cancels the request.
     *
     * @param   WebRequest  $request
     * @param   string      $responseClass
     * @return  Response
     */
    public static function negotiateHttpVersion(WebRequest $request, $responseClass = 'net\stubbles\webapp\response\WebResponse')
    {
        $httpVersion = $request->getProtocolVersion();
        if (null === $httpVersion) {
            $response = new $responseClass();
            return $response->httpVersionNotSupported();
        }

        return new $responseClass($httpVersion);
    }

    /**
     * negotiates mime type based on accept header and configured mime types
     *
     * Forces a 406 Not Acceptable response in case none of the accepted user
     * agent mime types is supported. Forces a 500 Internal Server Error
     * response in case a mime type but no suitable formatter was found.
     *
     * @param   WebRequest          $request
     * @param   SupportedMimeTypes  $supportedMimeTypes
     * @return  Response
     */
    public function negotiateMimeType(WebRequest $request, SupportedMimeTypes $supportedMimeTypes)
    {
        if ($supportedMimeTypes->isContentNegotationDisabled()) {
            return $this->response;
        }

        $mimeType = $supportedMimeTypes->findMatch($request->readHeader('HTTP_ACCEPT')
                                                           ->applyFilter(new \net\stubbles\input\filter\AcceptFilter())
                    );
        if (null === $mimeType) {
            return $this->response->notAcceptable($supportedMimeTypes->asArray());
        }

        $formatter = $this->createFormatter($mimeType, $supportedMimeTypes);
        if (null === $formatter) {
            return $this->response->internalServerError('No formatter defined for negotiated content type ' . $mimeType);
        }

        return new FormattingResponse($this->response, $formatter, $mimeType);
    }

    /**
     * creates formatter instance
     *
     * @param   string  $mimeType
     * @return  format\Formatter
     */
    private function createFormatter($mimeType, SupportedMimeTypes $supportedMimeTypes)
    {
        if ($supportedMimeTypes->hasFormatter($mimeType)) {
            return $this->injector->getInstance($supportedMimeTypes->getFormatter($mimeType));
        }

        if ($this->injector->hasBinding('net\stubbles\webapp\response\format\Formatter', $mimeType)) {
            return $this->injector->getInstance('net\stubbles\webapp\response\format\Formatter', $mimeType);
        }

        return null;
    }
}

{'fraction_non_alphanumeric': 0.09890251775338928, 'fraction_numerical': 0.0030987734021949644, 'mean_word_length': 2.1411192214111923, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6271834', 'n_tokens_mistral': 2143, 'n_tokens_neox': 2007, 'n_words': 514}