<commit_before><?php

namespace Helldar\ApiResponse\Services;

use Illuminate\Container\Container;
use Illuminate\Contracts\Support\Responsable;
use Symfony\Component\HttpFoundation\JsonResponse;

class ResponseService
{
    /** @var array */
    protected $with = [];

    /** @var null|string|int|array|object */
    protected $data = null;

    /** @var array */
    protected $headers = [];

    /** @var int */
    protected $status_code = 200;

    /**
     * @return ResponseService
     */
    public static function init()
    {
        return new self();
    }

    /**
     * @param int $status
     *
     * @return $this
     */
    public function status(int $status = 200): self
    {
        $this->status_code = $status;

        return $this;
    }

    /**
     * @param mixed $data
     *
     * @return $this
     */
    public function data($data = null): self
    {
        $this->data = $data instanceof Responsable
            ? $this->toResponse($data)
            : $data;

        return $this;
    }

    /**
     * @param array $with
     *
     * @return $this
     */
    public function with(array $with = []): self
    {
        $this->with = array_merge($this->with, $with);

        return $this;
    }

    /**
     * @param array $headers
     *
     * @return $this
     */
    public function headers(array $headers = []): self
    {
        $this->headers = array_merge($this->headers, $headers);

        return $this;
    }

    /**
     * @return JsonResponse
     */
    public function response(): JsonResponse
    {
        return JsonResponse::create($this->getData(), $this->status_code, $this->headers);
    }

    protected function isError(): bool
    {
        return $this->status_code >= 400;
    }

    private function e($value = null, $doubleEncode = true)
    {
        if (! is_string($value) || null === $value) {
            return $value;
        }

        return $value !== ''
            ? htmlspecialchars($value, ENT_QUOTES, 'UTF-8', $doubleEncode)
            : null;
    }

    private function getData()
    {
        $this->splitData();

        $data = $this->isError()
            ? $this->getErrorData()
            : $this->getSuccessData();

        return $this->mergeDataWith($data);
    }

    private function getErrorData(): array
    {
        return [
            'error' => [
                'code' => $this->status_code,
                'data' => $this->e($this->data),
            ],
        ];
    }

    private function getSuccessData(): array
    {
        return [
            'data' => $this->e($this->data),
        ];
    }

    private function mergeDataWith(array $data = []): array
    {
        return $this->with
            ? array_merge($data, $this->with)
            : $data;
    }

    private function splitData(): void
    {
        if (! is_array($this->data) && ! is_object($this->data)) {
            return;
        }

        $data = is_object($this->data)
            ? get_object_vars($this->data)
            : $this->data;

        if (isset($data['data'])) {
            $with = array_filter($data, function ($key) {
                return $key !== 'data';
            }, ARRAY_FILTER_USE_KEY);

            $this->data = $data['data'];
            $this->with($with);
        }
    }

    private function toResponse(Responsable $content)
    {
        return $content
            ->toResponse(Container::getInstance()->make('request'))
            ->getData();
    }
}
<commit_msg>Apply fixes from StyleCI<commit_after><?php

namespace Helldar\ApiResponse\Services;

use Illuminate\Container\Container;
use Illuminate\Contracts\Support\Responsable;
use Symfony\Component\HttpFoundation\JsonResponse;

class ResponseService
{
    /** @var array */
    protected $with = [];

    /** @var null|string|int|array|object */
    protected $data = null;

    /** @var array */
    protected $headers = [];

    /** @var int */
    protected $status_code = 200;

    /**
     * @return ResponseService
     */
    public static function init()
    {
        return new self();
    }

    /**
     * @param int $status
     *
     * @return $this
     */
    public function status(int $status = 200): self
    {
        $this->status_code = $status;

        return $this;
    }

    /**
     * @param mixed $data
     *
     * @return $this
     */
    public function data($data = null): self
    {
        $this->data = $data instanceof Responsable
            ? $this->toResponse($data)
            : $data;

        return $this;
    }

    /**
     * @param array $with
     *
     * @return $this
     */
    public function with(array $with = []): self
    {
        $this->with = array_merge($this->with, $with);

        return $this;
    }

    /**
     * @param array $headers
     *
     * @return $this
     */
    public function headers(array $headers = []): self
    {
        $this->headers = array_merge($this->headers, $headers);

        return $this;
    }

    /**
     * @return JsonResponse
     */
    public function response(): JsonResponse
    {
        return JsonResponse::create($this->getData(), $this->status_code, $this->headers);
    }

    protected function isError(): bool
    {
        return $this->status_code >= 400;
    }

    private function e($value = null, $doubleEncode = true)
    {
        if (!is_string($value) || null === $value) {
            return $value;
        }

        return $value !== ''
            ? htmlspecialchars($value, ENT_QUOTES, 'UTF-8', $doubleEncode)
            : null;
    }

    private function getData()
    {
        $this->splitData();

        $data = $this->isError()
            ? $this->getErrorData()
            : $this->getSuccessData();

        return $this->mergeDataWith($data);
    }

    private function getErrorData(): array
    {
        return [
            'error' => [
                'code' => $this->status_code,
                'data' => $this->e($this->data),
            ],
        ];
    }

    private function getSuccessData(): array
    {
        return [
            'data' => $this->e($this->data),
        ];
    }

    private function mergeDataWith(array $data = []): array
    {
        return $this->with
            ? array_merge($data, $this->with)
            : $data;
    }

    private function splitData(): void
    {
        if (!is_array($this->data) && !is_object($this->data)) {
            return;
        }

        $data = is_object($this->data)
            ? get_object_vars($this->data)
            : $this->data;

        if (isset($data['data'])) {
            $with = array_filter($data, function ($key) {
                return $key !== 'data';
            }, ARRAY_FILTER_USE_KEY);

            $this->data = $data['data'];
            $this->with($with);
        }
    }

    private function toResponse(Responsable $content)
    {
        return $content
            ->toResponse(Container::getInstance()->make('request'))
            ->getData();
    }
}

{'fraction_non_alphanumeric': 0.1509703196347032, 'fraction_numerical': 0.0028538812785388126, 'mean_word_length': 1.747549980399843, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 79, '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': '4018810', 'n_tokens_mistral': 2088, 'n_tokens_neox': 2020, 'n_words': 417}