<commit_before><?php namespace Flarum\Api\Actions;

use Flarum\Api\Request;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Flarum\Core\Exceptions\ValidationFailureException;
use Flarum\Core\Exceptions\PermissionDeniedException;
use Zend\Diactoros\Response;

abstract class JsonApiAction implements ActionInterface
{
    /**
     * Handle an API request and return an API response, handling any relevant
     * (API-related) exceptions that are thrown.
     *
     * @param \Flarum\Api\Request $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function handle(Request $request)
    {
        try {
            return $this->respond($request);
        } catch (ValidationFailureException $e) {
            $errors = [];
            foreach ($e->getErrors()->getMessages() as $field => $messages) {
                $errors[] = [
                    'detail' => implode("\n", $messages),
                    'path' => $field
                ];
            }
            return $this->json(['errors' => $errors], 422);
        } catch (PermissionDeniedException $e) {
            return $this->json(null, 401);
        } catch (ModelNotFoundException $e) {
            return $this->json(null, 404);
        }
    }

    protected function json($data = null, $status = 200)
    {
        if ($data === null) {
            $data = new \ArrayObject();
        }

        $data = json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);

        return new Response($data, $status);
    }

    /**
     * Handle an API request and return an API response.
     *<commit_msg>Fix responses returned by JSON helper.<commit_after><?php namespace Flarum\Api\Actions;

use Flarum\Api\Request;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Flarum\Core\Exceptions\ValidationFailureException;
use Flarum\Core\Exceptions\PermissionDeniedException;
use Zend\Diactoros\Response;

abstract class JsonApiAction implements ActionInterface
{
    /**
     * Handle an API request and return an API response, handling any relevant
     * (API-related) exceptions that are thrown.
     *
     * @param \Flarum\Api\Request $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function handle(Request $request)
    {
        try {
            return $this->respond($request);
        } catch (ValidationFailureException $e) {
            $errors = [];
            foreach ($e->getErrors()->getMessages() as $field => $messages) {
                $errors[] = [
                    'detail' => implode("\n", $messages),
                    'path' => $field
                ];
            }
            return $this->json(['errors' => $errors], 422);
        } catch (PermissionDeniedException $e) {
            return $this->json(null, 401);
        } catch (ModelNotFoundException $e) {
            return $this->json(null, 404);
        }
    }

    protected function json($data = null, $status = 200)
    {
        if ($data === null) {
            $data = new \ArrayObject();
        }

        $data = json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);

        $response = new Response('php://memory', $status);
        $response->getBody()->write($data);

        return $response;
    }

    /**
     * Handle an API request and return an API response.
     *
{'fraction_non_alphanumeric': 0.12448132780082988, 'fraction_numerical': 0.007113218731475993, 'mean_word_length': 2.2327586206896552, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '5811098', 'n_tokens_mistral': 973, 'n_tokens_neox': 925, 'n_words': 226}