<commit_before><?php

namespace Spark\Responder;

use Negotiation\NegotiatorInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Spark\Adr\PayloadInterface;
use Spark\Adr\ResponderInterface;
use Spark\Formatter\AbstractFormatter;
use Spark\Resolver\ResolverInterface;

class FormattedResponder implements ResponderInterface
{
    /**
     * @var NegotiatorInterface
     */
    private $negotiator;

    /**
     * @var ResolverInterface
     */
    private $resolver;

    /**
     * @var array
     */
    private $formatters = [
        'Spark\Formatter\JsonFormatter' => 1.0,
    ];

    /**
     * @param NegotiatorInterface $negotiator
     * @param ResolverInterface   $resolver
     */
    public function __construct(
        NegotiatorInterface $negotiator,
        ResolverInterface   $resolver
    ) {
        $this->negotiator = $negotiator;
        $this->resolver   = $resolver;
    }

    /**
     * Retrieve available formatters.
     *
     * @return array
     */
    public function getFormatters()
    {
        return $this->formatters;
    }

    /**
     * Return an instance with the specified formatters.
     *
     * @param  array $formatters
     * @return self
     */
    public function withFormatters(array $formatters)
    {
        arsort($formatters);

        $self = clone $this;
        $self->formatters = $formatters;
        return $self;
    }

    public function __invoke(
        ServerRequestInterface $request,
        ResponseInterface      $response,
        PayloadInterface       $payload
    ) {
        $formatter = $this->formatter($request);
        $response  = $this->format($response, $formatter, $payload);

        return $response;

    }

    /**
     * Retrieve a map of accepted priorities with the responsible formatter.
     *
     * @return array
     */
    protected function priorities()
    {
        $priorities = [];
        $formatters = array_keys($this->formatters);

        foreach ($formatters as $spec) {
            foreach ($spec::accepts() as $type) {
                $priorities[$type] = $spec;
            }
        }

        return $priorities;
    }

    /**
     * Retrieve the formatter to use for the current request.
     *
     * Uses content negotiation to find the best available output format for
     * the requested content type.
     *
     * @param  ServerRequestInterface $request
     * @return AbstractFormatter
     */
    protected function formatter(ServerRequestInterface $request)
    {
        $accept     = current($request->getHeader('Accept'));
        $priorities = $this->priorities();
        $preferred  = $this->negotiator->getBest($accept, array_keys($priorities));

        if ($preferred) {
            $formatter = $priorities[$preferred->getValue()];
        } else {
            $formatter = array_shift($priorities);
        }

        return call_user_func($this->resolver, $formatter);
    }

    /**
     * Update the response by formatting the payload.
     *
     * @param  ResponseInterface $response
     * @param  AbstractFormatter $formatter
     * @param  PayloadInterface  $payload
     * @return ResponseInterface
     */
    protected function format(
        ResponseInterface $response,
        AbstractFormatter $formatter,
        PayloadInterface  $payload
    ) {
        $response = $response->withStatus($formatter->status($payload));
        $response = $response->withHeader('Content-Type', $formatter->type());

        // Overwrite the body instead of making a copy and dealing with the stream.
        $response->getBody()->write($formatter->body($payload));

        return $response;
    }
}
<commit_msg>Fix whitespace<commit_after><?php

namespace Spark\Responder;

use Negotiation\NegotiatorInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Spark\Adr\PayloadInterface;
use Spark\Adr\ResponderInterface;
use Spark\Formatter\AbstractFormatter;
use Spark\Resolver\ResolverInterface;

class FormattedResponder implements ResponderInterface
{
    /**
     * @var NegotiatorInterface
     */
    private $negotiator;

    /**
     * @var ResolverInterface
     */
    private $resolver;

    /**
     * @var array
     */
    private $formatters = [
        'Spark\Formatter\JsonFormatter' => 1.0,
    ];

    /**
     * @param NegotiatorInterface $negotiator
     * @param ResolverInterface   $resolver
     */
    public function __construct(
        NegotiatorInterface $negotiator,
        ResolverInterface   $resolver
    ) {
        $this->negotiator = $negotiator;
        $this->resolver   = $resolver;
    }

    /**
     * Retrieve available formatters.
     *
     * @return array
     */
    public function getFormatters()
    {
        return $this->formatters;
    }

    /**
     * Return an instance with the specified formatters.
     *
     * @param  array $formatters
     * @return self
     */
    public function withFormatters(array $formatters)
    {
        arsort($formatters);

        $self = clone $this;
        $self->formatters = $formatters;
        return $self;
    }

    public function __invoke(
        ServerRequestInterface $request,
        ResponseInterface      $response,
        PayloadInterface       $payload
    ) {
        $formatter = $this->formatter($request);
        $response  = $this->format($response, $formatter, $payload);

        return $response;
    }

    /**
     * Retrieve a map of accepted priorities with the responsible formatter.
     *
     * @return array
     */
    protected function priorities()
    {
        $priorities = [];
        $formatters = array_keys($this->formatters);

        foreach ($formatters as $spec) {
            foreach ($spec::accepts() as $type) {
                $priorities[$type] = $spec;
            }
        }

        return $priorities;
    }

    /**
     * Retrieve the formatter to use for the current request.
     *
     * Uses content negotiation to find the best available output format for
     * the requested content type.
     *
     * @param  ServerRequestInterface $request
     * @return AbstractFormatter
     */
    protected function formatter(ServerRequestInterface $request)
    {
        $accept     = current($request->getHeader('Accept'));
        $priorities = $this->priorities();
        $preferred  = $this->negotiator->getBest($accept, array_keys($priorities));

        if ($preferred) {
            $formatter = $priorities[$preferred->getValue()];
        } else {
            $formatter = array_shift($priorities);
        }

        return call_user_func($this->resolver, $formatter);
    }

    /**
     * Update the response by formatting the payload.
     *
     * @param  ResponseInterface $response
     * @param  AbstractFormatter $formatter
     * @param  PayloadInterface  $payload
     * @return ResponseInterface
     */
    protected function format(
        ResponseInterface $response,
        AbstractFormatter $formatter,
        PayloadInterface  $payload
    ) {
        $response = $response->withStatus($formatter->status($payload));
        $response = $response->withHeader('Content-Type', $formatter->type());

        // Overwrite the body instead of making a copy and dealing with the stream.
        $response->getBody()->write($formatter->body($payload));

        return $response;
    }
}

{'fraction_non_alphanumeric': 0.10934125269978402, 'fraction_numerical': 0.0005399568034557236, 'mean_word_length': 2.3149888143176733, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6596330', 'n_tokens_mistral': 2082, 'n_tokens_neox': 1950, 'n_words': 463}