<commit_before><?php

namespace Antarctica\JsonExceptionFormatter\Formatter;

use Exception;
use Radweb\JsonExceptionFormatter\FormatterInterface;

class JsonExceptionFormatter implements FormatterInterface {

    public function formatDebug(Exception $exception)
    {
        // Base error object
        $error = [
            "exception" => get_class($exception),
            "kind" => $this->getExceptionKind($exception),
            "message" => $this->formatJsonExceptionMessage($exception),
            "file" => $exception->getFile(),
            "line" => $exception->getLine(),
            "stack_trace" => $exception->getTrace(),
        ];

        // Some exceptions provide additional data so react to the exception class
        // If this gets large enough, abstract this out a bit using something like call user function
        switch (get_class($exception))
        {
            case "Laracasts\\Validation\\FormValidationException":

                $error['details'] = $exception->getErrors();
                break;
            case "Lions\\Exception\\Token\\MissingTokenException":

                $error['details'] = [
                    "authentication_error" => [
                        "No authentication token was given and no authentication session exists."
                    ]
                ];
                break;
            case "Lions\\Exception\\Token\\InvalidTokenException":

                $error['details'] = [
                    "authentication_error" => [
                        "The authentication token given is not valid or is malformed."
                    ]
                ];
                break;
            case "Lions\\Exception\\Token\\ExpiredTokenException":

                $error['details'] = [
                    "authentication_error" => [
                        "The authentication token given has expired and is no longer valid."
                    ]
                ];
                break;
            case "Lions\\Exception\\Token\\UnknownSubjectTokenException":

                $error['details'] = [
                    "authentication_error" => [
                        "The subject for the authentication token is unknown."
                    ]
                ];
                break;
            case "Lions\\Exception\\Token\\BlacklistedTokenException":

                $error['details'] = [
                    "authentication_error" => [
                        "The authentication token has been blacklisted and can no longer be used."
                    ]
                ];
                break;
            case "Lions\\Exception\\Token\\TokenException":

                $error['details'] = [
                    "authentication_error" => [
                        "There is something wrong with the token authentication."
                    ]
                ];
                break;
            case "Lions\\Exception\\Auth\\AuthenticationException":

                $error['details'] = [
                    "authentication_error" => [
                        "Incorrect username and password."
                    ]
                ];
                break;
        }

        // Where no message is given (the exception type is enough), prevent an empty array value being returned
        if ($error['message'] === '')
        {
            unset($error['message']);
        }

        // Where no exception kind is given (not one of our exceptions), prevent an empty array value being returned
        if ($error['kind'] === false)
        {
            unset($error['kind']);
        }

        return ['errors' => [$error]];
    }

    public function formatPlain(Exception $exception)
    {
        // Base error object
        $error = [
            "message" => $this->formatJsonExceptionMessage($exception),
            "kind" => $this->getExceptionKind($exception),
        ];

        // Some exceptions provide additional data so react to the exception class
        // If this gets large enough, abstract this out a bit using something like call user function
        switch (get_class($exception))
        {
            case "Laracasts\\Validation\\FormValidationException":

                $error['details'] = $exception->getErrors();
                break;
            case "Lions\\Exception\\Token\\MissingTokenException":

                $error['details'] = [
                    "authentication_error" => [
                        "No authentication token was given and no authentication session exists."
                    ]
                ];
                break;
            case "Lions\\Exception\\Token\\InvalidTokenException":

                $error['details'] = [
                    "authentication_error" => [
                        "The authentication token given is not valid or is malformed."
                    ]
                ];
                break;
            case "Lions\\Exception\\Token\\ExpiredTokenException":

                $error['details'] = [
                    "authentication_error" => [
                        "The authentication token given has expired and is no longer valid."
                    ]
                ];
                break;
            case "Lions\\Exception\\Token\\UnknownSubjectTokenException":

                $error['details'] = [
                    "authentication_error" => [
                        "The subject for the authentication token is unknown."
                    ]
                ];
                break;
            case "Lions\\Exception\\Token\\BlacklistedTokenException":

                $error['details'] = [
                    "authentication_error" => [
                        "The authentication token has been blacklisted and can no longer be used."
                    ]
                ];
                break;
            case "Lions\\Exception\\Token\\TokenException":

                $error['details'] = [
                    "authentication_error" => [
                        "There is something wrong with the token authentication."
                    ]
                ];
                break;
            case "Lions\\Exception\\Auth\\AuthenticationException":

                $error['details'] = [
                    "authentication_error" => [
                        "Incorrect username and password."
                    ]
                ];
                break;
        }

        // Where no exception message is given (the exception type is enough), prevent an empty array value being returned
        if ($error['message'] === '')
        {
            unset($error['message']);
        }

        // Where no exception kind is given (not one of our exceptions), prevent an empty array value being returned
        if ($error['kind'] === false)
        {
            unset($error['kind']);
        }

        return ['errors' => [$error]];
    }

    /**<commit_msg>Refactoring custom data for specific exception types<commit_after><?php

namespace Antarctica\JsonExceptionFormatter\Formatter;

use Exception;
use Radweb\JsonExceptionFormatter\FormatterInterface;

class JsonExceptionFormatter implements FormatterInterface {

    protected $error = [];

    public function formatDebug(Exception $exception)
    {
        // Base error object
        $this->error = [
            "exception" => get_class($exception),
            "kind" => $this->getExceptionKind($exception),
            "message" => $this->formatJsonExceptionMessage($exception),
            "file" => $exception->getFile(),
            "line" => $exception->getLine(),
            "stack_trace" => $exception->getTrace(),
        ];

        $this->formatErrorType($exception, $debug_mode = true);

        // Where no message is given (the exception type is enough), prevent an empty array value being returned
        if ($this->error['message'] === '')
        {
            unset($this->error['message']);
        }

        // Where no exception kind is given (not one of our exceptions), prevent an empty array value being returned
        if ($this->error['kind'] === false)
        {
            unset($this->error['kind']);
        }

        return ['errors' => [$this->error]];
    }

    public function formatPlain(Exception $exception)
    {
        // Base error object
        $this->error = [
            "message" => $this->formatJsonExceptionMessage($exception),
            "kind" => $this->getExceptionKind($exception),
        ];

        $this->formatErrorType($exception, $debug_mode = false);

    /**
     * @param Exception $exception
     * @param bool $debug_mode
     */
    protected function formatErrorType(Exception $exception, $debug_mode)
    {
        // Some exceptions provide additional data so react to the exception class
        // TODO: If this gets large enough, abstract this out a bit using something like call user function
        switch (get_class($exception))
        {
            case "Laracasts\\Validation\\FormValidationException":

                $this->error['details'] = $exception->getErrors();
                break;
            case "Lions\\Exception\\Token\\MissingTokenException":

                $this->error['details'] = [
                    "authentication_error" => [
                        "No authentication token was given and no authentication session exists."
                    ]
                ];
                break;
            case "Lions\\Exception\\Token\\InvalidTokenException":

                $this->error['details'] = [
                    "authentication_error" => [
                        "The authentication token given is not valid or is malformed."
                    ]
                ];
                break;
            case "Lions\\Exception\\Token\\ExpiredTokenException":

                $this->error['details'] = [
                    "authentication_error" => [
                        "The authentication token given has expired and is no longer valid."
                    ]
                ];
                break;
            case "Lions\\Exception\\Token\\UnknownSubjectTokenException":

                $this->error['details'] = [
                    "authentication_error" => [
                        "The subject for the authentication token is unknown."
                    ]
                ];
                break;
            case "Lions\\Exception\\Token\\BlacklistedTokenException":

                $this->error['details'] = [
                    "authentication_error" => [
                        "The authentication token has been blacklisted and can no longer be used."
                    ]
                ];
                break;
            case "Lions\\Exception\\Token\\TokenException":

                $this->error['details'] = [
                    "authentication_error" => [
                        "There is something wrong with the token authentication."
                    ]
                ];
                break;
            case "Lions\\Exception\\Auth\\AuthenticationException":

                $this->error['details'] = [
                    "authentication_error" => [
                        "Incorrect username and password."
                    ]
                ];
                break;
        }

        // Where no exception message is given (the exception type is enough), prevent an empty array value being returned
        if ($error['message'] === '')
        {
            unset($error['message']);
        }

        // Where no exception kind is given (not one of our exceptions), prevent an empty array value being returned
        if ($error['kind'] === false)
        {
            unset($error['kind']);
        }

        return ['errors' => [$error]];
    }

    /**
{'fraction_non_alphanumeric': 0.10614430186113707, 'fraction_numerical': 0.0, 'mean_word_length': 1.426892142709837, 'pattern_counts': {'":': 24, '<': 5, '<?xml version=': 0, '>': 78, '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': '3003818', 'n_tokens_mistral': 2657, 'n_tokens_neox': 2468, 'n_words': 690}