<commit_before><?php

namespace App\Middleware;

use Zend\Diactoros\ServerRequest as Request;
use Zend\Diactoros\Response;

/**
 * JSON Middleware
 */
class JsonMiddleware
{
    /**
     * Attribute
     *
     * @var string Attribute
     */
    const ATTRIBUTE = 'json';

    /**
     * Invoke middleware.
     *
     * @param Request $request The request.
     * @param Response $response The response.
     * @param callable $next Callback to invoke the next middleware.
     * @return Response A response
     */
    public function __invoke(Request $request, Response $response, callable $next)
    {
        if (!$this->isJson($request)) {
            return $next($request, $response);
        }

        // Disable browser cache
        $response = $this->getResponseWithHeader($response);

        $jsonRequest = $this->getJsonData($request);

        // Add json request
        $request = $request->withAttribute(static::ATTRIBUTE, $jsonRequest);

        return $next($request, $response);
    }

    /**
     * Update response header.
     *
     * @param Response $response
     * @return Response
     */
    public function getResponseWithHeader(Response $response)
    {
        $response = $response->withHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
        $response = $response->withHeader('Pragma', 'no-cache');
        $response = $response->withHeader('Expires', '0');
        return $response;
    }

    /**
     * Get JSON request as array
     *
     * @param Request $request
     * @return array Data
     */
    protected function getJsonData(Request $request)
    {
        $requestContent = $request->getBody()->__toString();
        return json_decode($requestContent, true);
    }

    /**
     * Returns true if a JSON request has been received.
     *
     * @param Request $request Request
     * @return bool Status
     */
    public function isJson(Request $request)
    {
        $type = $request->getHeader('content-type');
        return !empty($type[0]) && (strpos($type[0], 'application/json') !== false);
    }
}<commit_msg>Added json middleware<commit_after><?php

namespace App\Middleware;

use Zend\Diactoros\ServerRequest as Request;
use Zend\Diactoros\Response;

/**
 * JSON Middleware
 */
class JsonMiddleware
{
    /**
     * Attribute
     *
     * @var string Attribute
     */
    const ATTRIBUTE = 'json';

    /**
     * Invoke middleware.
     *
     * @param Request $request The request.
     * @param Response $response The response.
     * @param callable $next Callback to invoke the next middleware.
     * @return Response A response
     */
    public function __invoke(Request $request, Response $response, callable $next)
    {
        if (!$this->isJson($request)) {
            return $next($request, $response);
        }

        $jsonRequest = $this->getJsonData($request);

        // Add json request
        $request = $request->withAttribute(static::ATTRIBUTE, $jsonRequest);

        $response = $next($request, $response);

        // Disable browser cache
        $response = $this->getResponseWithHeader($response);

        return $response;
    }

    /**
     * Get JSON request as array
     *
     * @param Request $request
     * @return array Data
     */
    protected function getJsonData(Request $request)
    {
        $requestContent = $request->getBody()->__toString();
        return json_decode($requestContent, true);
    }

    /**
     * Returns true if a JSON request has been received.
     *
     * @param Request $request Request
     * @return bool Status
     */
    public function isJson(Request $request)
    {
        $type = $request->getHeader('content-type');
        return !empty($type[0]) && (strpos($type[0], 'application/json') !== false);
    }

    /**
     * Update response header.
     *
     * @param Response $response
     * @return Response
     */
    public function getResponseWithHeader(Response $response)
    {
        $response = $response->withHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
        $response = $response->withHeader('Pragma', 'no-cache');
        $response = $response->withHeader('Expires', '0');
        return $response;
    }
}
{'fraction_non_alphanumeric': 0.13175355450236967, 'fraction_numerical': 0.0014218009478672985, 'mean_word_length': 2.457002457002457, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '2470856', 'n_tokens_mistral': 1185, 'n_tokens_neox': 1168, 'n_words': 296}