<commit_before><?php

/**
 * Copyright 2018 Cloud Creativity Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace CloudCreativity\LaravelJsonApi\Routing;

use CloudCreativity\LaravelJsonApi\Contracts\Resolver\ResolverInterface;
use CloudCreativity\LaravelJsonApi\Utils\Str;
use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Routing\Route;
use Illuminate\Support\Fluent;

/**
 * Class ResourceGroup
 *
 * @package CloudCreativity\LaravelJsonApi
 */
class ResourceGroup
{

    use RegistersResources;

    /**
     * @var ResolverInterface
     */
    private $resolver;

    /**
     * ResourceGroup constructor.
     *
     * @param string $resourceType
     * @param ResolverInterface $resolver
     * @param Fluent $options
     */
    public function __construct($resourceType, ResolverInterface $resolver, Fluent $options)
    {
        $this->resourceType = $resourceType;
        $this->resolver = $resolver;
        $this->options = $options;
    }

    /**
     * @param Registrar $router
     */
    public function addResource(Registrar $router)
    {
        $router->group($this->groupAction(), function (Registrar $router) {
            /** Primary resource routes. */
            $router->group(['middleware' => 'json-api.validate'], function ($router) {
                $this->addResourceRoutes($router);
            });

            /** Resource relationship Routes */
            $this->addRelationshipRoutes($router);
        });
    }

    /**
     * @return array
     */
    protected function groupAction()
    {
        return [
            'middleware' => $this->middleware(),
            'as' => "{$this->resourceType}.",
            'prefix' => Str::dasherize($this->resourceType),
        ];
    }

    /**
     * @return array
     */
    protected function middleware()
    {
        $middleware = (array) $this->options->get('middleware');
        $authorizer = $this->authorizer();

        return array_merge($middleware, array_filter([
            $authorizer ? "json-api.authorize:$authorizer" : null,
            'json-api.bindings',
        ]));
    }

    /**
     * @return string|null
     */
    protected function authorizer()
    {
        if ($authorizer = $this->options->get('authorizer')) {
            return $authorizer;
        }

        $authorizer = $this->resolver->getAuthorizerByResourceType($this->resourceType);

        return class_exists($authorizer) ?: $this->options->get('default-authorizer');
    }

    /**
     * @param Registrar $router
     */
    protected function addResourceRoutes(Registrar $router)
    {<commit_msg>[Bugfix] Ensure authorizor is correctly registered<commit_after><?php

/**
 * Copyright 2018 Cloud Creativity Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace CloudCreativity\LaravelJsonApi\Routing;

use CloudCreativity\LaravelJsonApi\Contracts\Resolver\ResolverInterface;
use CloudCreativity\LaravelJsonApi\Utils\Str;
use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Routing\Route;
use Illuminate\Support\Fluent;

/**
 * Class ResourceGroup
 *
 * @package CloudCreativity\LaravelJsonApi
 */
class ResourceGroup
{

    use RegistersResources;

    /**
     * @var ResolverInterface
     */
    private $resolver;

    /**
     * ResourceGroup constructor.
     *
     * @param string $resourceType
     * @param ResolverInterface $resolver
     * @param Fluent $options
     */
    public function __construct($resourceType, ResolverInterface $resolver, Fluent $options)
    {
        $this->resourceType = $resourceType;
        $this->resolver = $resolver;
        $this->options = $options;
    }

    /**
     * @param Registrar $router
     */
    public function addResource(Registrar $router)
    {
        $router->group($this->groupAction(), function (Registrar $router) {
            /** Primary resource routes. */
            $router->group(['middleware' => 'json-api.validate'], function ($router) {
                $this->addResourceRoutes($router);
            });

            /** Resource relationship Routes */
            $this->addRelationshipRoutes($router);
        });
    }

    /**
     * @return array
     */
    protected function groupAction()
    {
        return [
            'middleware' => $this->middleware(),
            'as' => "{$this->resourceType}.",
            'prefix' => Str::dasherize($this->resourceType),
        ];
    }

    /**
     * @return array
     */
    protected function middleware()
    {
        $middleware = (array) $this->options->get('middleware');
        $authorizer = $this->authorizer();

        return array_merge($middleware, array_filter([
            $authorizer ? "json-api.authorize:$authorizer" : null,
            'json-api.bindings',
        ]));
    }

    /**
     * @return string|null
     */
    protected function authorizer()
    {
        if ($authorizer = $this->options->get('authorizer')) {
            return $authorizer;
        }

        $authorizer = $this->resolver->getAuthorizerByResourceType($this->resourceType);

        return class_exists($authorizer) ? $authorizer : $this->options->get('default-authorizer');
    }

    /**
     * @param Registrar $router
     */
    protected function addResourceRoutes(Registrar $router)
    {
{'fraction_non_alphanumeric': 0.1234509056244042, 'fraction_numerical': 0.002542103590721322, 'mean_word_length': 2.613662456946039, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7633643', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1711, 'n_words': 418}