<commit_before><?php

namespace rjapi\extension;

use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use League\Fractal\Resource\Collection;
use rjapi\helpers\ConfigOptions;
use rjapi\blocks\EntitiesTrait;
use rjapi\types\HTTPMethodsInterface;
use rjapi\types\JwtInterface;
use rjapi\types\ModelsInterface;
use rjapi\helpers\Json;
use rjapi\types\PhpInterface;

class ApiController extends Controller implements JSONApiInterface
{
    use BaseRelationsTrait,
        OptionsTrait,
        EntitiesTrait,
        JWTTrait,
        FsmTrait,
        SpellCheckTrait,
        BitMaskTrait,
        CacheTrait;

    // JSON API support enabled by default
    protected $jsonApi = true;

    protected $props = [];
    protected $entity;
    /** @var BaseModel $model */
    protected $model;
    /** @var EntitiesTrait $modelEntity */
    private   $modelEntity;
    protected $formRequest;
    private   $relsRemoved    = false;
    private   $defaultOrderBy = [];
    /** @var ConfigOptions $configOptions */
    protected $configOptions;
    /** @var CustomSql $customSql */
    protected $customSql;
    /** @var BitMask $bitMask */
    private $bitMask;

    private $jsonApiMethods = [
        JSONApiInterface::URI_METHOD_INDEX,
        JSONApiInterface::URI_METHOD_VIEW,
        JSONApiInterface::URI_METHOD_CREATE,
        JSONApiInterface::URI_METHOD_UPDATE,
        JSONApiInterface::URI_METHOD_DELETE,
        JSONApiInterface::URI_METHOD_RELATIONS,
    ];

    /**
     * BaseControllerTrait constructor.
     *
     * @param Route $route
     */
    public function __construct(Route $route)
    {
        // add relations to json api methods array
        $this->addRelationMethods();
        $actionName   = $route->getActionName();
        $calledMethod = substr($actionName, strpos($actionName, PhpInterface::AT) + 1);
        if ($this->jsonApi === false && in_array($calledMethod, $this->jsonApiMethods)) {
            Json::outputErrors(
                [
                    [
                        JSONApiInterface::ERROR_TITLE  => 'JSON API support disabled',
                        JSONApiInterface::ERROR_DETAIL => 'JSON API method ' . $calledMethod
                            .
                            ' was called. You can`t call this method while JSON API support is disabled.',
                    ],
                ]
            );
        }
        $this->setEntities();
        $this->setDefaults();
        $this->setConfigOptions($calledMethod);
    }

    /**
     * Responds with header of an allowed/available http methods
     * @return mixed
     */
    public function options()
    {
        return response()->header('Allow', HTTPMethodsInterface::HTTP_METHODS_AVAILABLE);
    }

    /**
     * GET Output all entries for this Entity with page/limit pagination support
     *
     * @param Request $request<commit_msg>#130. Add Content-Type headers to options http method + fixes<commit_after><?php

namespace rjapi\extension;

use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use League\Fractal\Resource\Collection;
use rjapi\helpers\ConfigOptions;
use rjapi\blocks\EntitiesTrait;
use rjapi\types\HTTPMethodsInterface;
use rjapi\types\JwtInterface;
use rjapi\types\ModelsInterface;
use rjapi\helpers\Json;
use rjapi\types\PhpInterface;

class ApiController extends Controller implements JSONApiInterface
{
    use BaseRelationsTrait,
        OptionsTrait,
        EntitiesTrait,
        JWTTrait,
        FsmTrait,
        SpellCheckTrait,
        BitMaskTrait,
        CacheTrait;

    // JSON API support enabled by default
    protected $jsonApi = true;

    protected $props = [];
    protected $entity;
    /** @var BaseModel $model */
    protected $model;
    /** @var EntitiesTrait $modelEntity */
    private   $modelEntity;
    protected $formRequest;
    private   $relsRemoved    = false;
    private   $defaultOrderBy = [];
    /** @var ConfigOptions $configOptions */
    protected $configOptions;
    /** @var CustomSql $customSql */
    protected $customSql;
    /** @var BitMask $bitMask */
    private $bitMask;

    private $jsonApiMethods = [
        JSONApiInterface::URI_METHOD_INDEX,
        JSONApiInterface::URI_METHOD_VIEW,
        JSONApiInterface::URI_METHOD_CREATE,
        JSONApiInterface::URI_METHOD_UPDATE,
        JSONApiInterface::URI_METHOD_DELETE,
        JSONApiInterface::URI_METHOD_RELATIONS,
    ];

    /**
     * BaseControllerTrait constructor.
     *
     * @param Route $route
     */
    public function __construct(Route $route)
    {
        // add relations to json api methods array
        $this->addRelationMethods();
        $actionName   = $route->getActionName();
        $calledMethod = substr($actionName, strpos($actionName, PhpInterface::AT) + 1);
        if ($this->jsonApi === false && in_array($calledMethod, $this->jsonApiMethods)) {
            Json::outputErrors(
                [
                    [
                        JSONApiInterface::ERROR_TITLE  => 'JSON API support disabled',
                        JSONApiInterface::ERROR_DETAIL => 'JSON API method ' . $calledMethod
                            .
                            ' was called. You can`t call this method while JSON API support is disabled.',
                    ],
                ]
            );
        }
        $this->setEntities();
        $this->setDefaults();
        $this->setConfigOptions($calledMethod);
    }

    /**
     * Responds with header of an allowed/available http methods
     * @return mixed
     */
    public function options()
    {
        // this seems like needless params passed by default, but they needed for backward compatibility in Laravel prev versions
        return response('', 200)->withHeaders([
            'Allow'                               => HTTPMethodsInterface::HTTP_METHODS_AVAILABLE,
            JSONApiInterface::HEADER_CONTENT_TYPE => JSONApiInterface::HEADER_CONTENT_TYPE_VALUE,
        ]);
    }

    /**
     * GET Output all entries for this Entity with page/limit pagination support
     *
     * @param Request $request
{'fraction_non_alphanumeric': 0.0975174428038293, 'fraction_numerical': 0.0012980691221807562, 'mean_word_length': 2.353645266594124, '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': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4497251', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1655, 'n_words': 387}