<commit_before><?php

/**
 * Copyright 2016 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\Http\Responses;

use CloudCreativity\JsonApi\Exceptions\MutableErrorCollection as Errors;
use CloudCreativity\LaravelJsonApi\Contracts\Pagination\PaginatorInterface;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Http\Response;
use Neomerx\JsonApi\Contracts\Document\ErrorInterface;
use Neomerx\JsonApi\Contracts\Http\ResponsesInterface;
use Neomerx\JsonApi\Exceptions\ErrorCollection;

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

    /**
     * @var ResponsesInterface
     */
    private $responses;

    /**
     * @var PaginatorInterface
     */
    private $paginator;

    /**
     * ResponseFactory constructor.
     * @param ResponsesInterface $responses
     * @param PaginatorInterface $paginator
     */
    public function __construct(ResponsesInterface $responses, PaginatorInterface $paginator)
    {
        $this->responses = $responses;
        $this->paginator = $paginator;
    }

    /**
     * @param $statusCode
     * @param array $headers
     * @return Response
     */
    public function statusCode($statusCode, array $headers = [])
    {
        /** @var Response $response */
        return $this->responses->getCodeResponse($statusCode, $headers);
    }

    /**
     * @param array $headers
     * @return Response
     */
    public function noContent(array $headers = [])
    {
        return $this->statusCode(Response::HTTP_NO_CONTENT, $headers);
    }

    /**
     * @param mixed $meta
     * @param int $statusCode
     * @param array $headers
     * @return Response
     */
    public function meta($meta, $statusCode = Response::HTTP_OK, array $headers = [])
    {
        return $this->responses->getMetaResponse($meta, $statusCode, $headers);
    }

    /**
     * @param mixed $data
     * @param array $links
     * @param mixed|null $meta
     * @param int $statusCode
     * @param array $headers
     * @return Response
     */
    public function content(
        $data,
        array $links = [],
        $meta = null,
        $statusCode = Response::HTTP_OK,
        array $headers = []
    ) {
        if ($data instanceof Paginator) {
            $meta = $this->paginator->addMeta($data, $meta);
            $links = $this->paginator->addLinks($data, $links);
        }

        return $this->responses->getContentResponse($data, $statusCode, $links, $meta, $headers);
    }

    /**
     * @param object $resource
     * @param array $links
     * @param mixed|null $meta
     * @param array $headers
     * @return Response
     */
    public function created($resource, array $links = [], $meta = null, array $headers = [])
    {
        return $this->responses->getCreatedResponse($resource, $links, $meta, $headers);
    }

    /**
     * @param $data
     * @param array $links
     * @param mixed|null $meta
     * @param int $statusCode
     * @param array $headers
     * @return Response
     */
    public function relationship(
        $data,
        array $links = [],
        $meta = null,
        $statusCode = Response::HTTP_OK,
        array $headers = []
    ) {
        if ($data instanceof Paginator) {
            $meta = $this->paginator->addMeta($data, $meta);
            $links = $this->paginator->addLinks($data, $links);
        }

        return $this->responses->getIdentifiersResponse($data, $statusCode, $links, $meta, $headers);
    }

    /**
     * @param ErrorInterface $error
     * @param int|string|null $statusCode
     * @param array $headers
     * @return Response
     */
    public function error(ErrorInterface $error, $statusCode = null, array $headers = [])
    {
        return $this->errors($error, $statusCode, $headers);
    }

    /**
     * @param ErrorInterface|ErrorInterface[]|ErrorCollection $errors
     * @param int|string|null $statusCode
     * @param array $headers
     * @return Response
     */
    public function errors($errors, $statusCode = null, array $headers = [])
    {
        if (is_null($statusCode)) {<commit_msg>[Feature] Allow error response using string error key<commit_after><?php

/**
 * Copyright 2016 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\Http\Responses;

use CloudCreativity\JsonApi\Contracts\Repositories\ErrorRepositoryInterface;
use CloudCreativity\JsonApi\Exceptions\MutableErrorCollection as Errors;
use CloudCreativity\LaravelJsonApi\Contracts\Pagination\PaginatorInterface;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Http\Response;
use InvalidArgumentException;
use Neomerx\JsonApi\Contracts\Document\ErrorInterface;
use Neomerx\JsonApi\Contracts\Http\ResponsesInterface;
use Neomerx\JsonApi\Exceptions\ErrorCollection;

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

    /**
     * @var ResponsesInterface
     */
    private $responses;

    /**
     * @var PaginatorInterface
     */
    private $paginator;

    /**
     * @var ErrorRepositoryInterface
     */
    private $errors;

    /**
     * ResponseFactory constructor.
     * @param ResponsesInterface $responses
     * @param PaginatorInterface $paginator
     * @param ErrorRepositoryInterface $errors
     */
    public function __construct(
        ResponsesInterface $responses,
        PaginatorInterface $paginator,
        ErrorRepositoryInterface $errors
    ) {
        $this->responses = $responses;
        $this->paginator = $paginator;
        $this->errors = $errors;
    }

    /**
     * @param $statusCode
     * @param array $headers
     * @return Response
     */
    public function statusCode($statusCode, array $headers = [])
    {
        /** @var Response $response */
        return $this->responses->getCodeResponse($statusCode, $headers);
    }

    /**
     * @param array $headers
     * @return Response
     */
    public function noContent(array $headers = [])
    {
        return $this->statusCode(Response::HTTP_NO_CONTENT, $headers);
    }

    /**
     * @param mixed $meta
     * @param int $statusCode
     * @param array $headers
     * @return Response
     */
    public function meta($meta, $statusCode = Response::HTTP_OK, array $headers = [])
    {
        return $this->responses->getMetaResponse($meta, $statusCode, $headers);
    }

    /**
     * @param mixed $data
     * @param array $links
     * @param mixed|null $meta
     * @param int $statusCode
     * @param array $headers
     * @return Response
     */
    public function content(
        $data,
        array $links = [],
        $meta = null,
        $statusCode = Response::HTTP_OK,
        array $headers = []
    ) {
        if ($data instanceof Paginator) {
            $meta = $this->paginator->addMeta($data, $meta);
            $links = $this->paginator->addLinks($data, $links);
        }

        return $this->responses->getContentResponse($data, $statusCode, $links, $meta, $headers);
    }

    /**
     * @param object $resource
     * @param array $links
     * @param mixed|null $meta
     * @param array $headers
     * @return Response
     */
    public function created($resource, array $links = [], $meta = null, array $headers = [])
    {
        return $this->responses->getCreatedResponse($resource, $links, $meta, $headers);
    }

    /**
     * @param $data
     * @param array $links
     * @param mixed|null $meta
     * @param int $statusCode
     * @param array $headers
     * @return Response
     */
    public function relationship(
        $data,
        array $links = [],
        $meta = null,
        $statusCode = Response::HTTP_OK,
        array $headers = []
    ) {
        if ($data instanceof Paginator) {
            $meta = $this->paginator->addMeta($data, $meta);
            $links = $this->paginator->addLinks($data, $links);
        }

        return $this->responses->getIdentifiersResponse($data, $statusCode, $links, $meta, $headers);
    }

    /**
     * @param ErrorInterface|string $error
     *      the error object or a string error key to get the error from the repository.
     * @param int|string|null $statusCode
     * @param array $headers
     * @return Response
     */
    public function error($error, $statusCode = null, array $headers = [])
    {
        if (is_string($error) && !empty($error)) {
            $error = $this->errors->error($error);
        }

        if (!$error instanceof ErrorInterface) {
            throw new InvalidArgumentException('Expecting an error object or a string error key.');
        }

        return $this->errors($error, $statusCode, $headers);
    }

    /**
     * @param ErrorInterface|ErrorInterface[]|ErrorCollection $errors
     * @param int|string|null $statusCode
     * @param array $headers
     * @return Response
     */
    public function errors($errors, $statusCode = null, array $headers = [])
    {
        if (is_null($statusCode)) {
{'fraction_non_alphanumeric': 0.1210829036096787, 'fraction_numerical': 0.0015866719555731853, 'mean_word_length': 2.683345507669832, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 50, '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': '2634487', 'n_tokens_mistral': 2852, 'n_tokens_neox': 2808, 'n_words': 749}