<commit_before><?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use Illuminate\Http\Response as IlluminateResponse;
use League\Fractal\Manager;
use League\Fractal\Serializer\JsonApiSerializer;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;

class ApiController extends Controller
{
    /**
     * Status code
     * @var String
     */
    protected $statusCode = IlluminateResponse::HTTP_OK;

    /**
     * Return the status code
     * @return String
     */
    public function getStatusCode()
    {
        return $this->statusCode;
    }

    public function setStatusCode($statusCode)
    {
        $this->statusCode = $statusCode;

        return $this;
    }

    /**
     * Method to make a response in json.
     * @param  array $data
     * @param  array  $headers
     * @return \Illuminate\Http\Response
     */
    public function respond($data, $headers = [])
    {
        return response()->json($data, $this->getStatusCode(), $headers);
    }

    /**
     * Return a 401 unauthorized error.
     *
     * @param string $message
     * @return \Illuminate\Http\Response
     */
    public function respondUnauthorized($message = 'Unauthorized.')
    {
        return $this->setStatusCode(IlluminateResponse::HTTP_UNAUTHORIZED)->respondWithError($message);
    }

    /**
     * Return a 403 forbidden error.
     *
     * @param string $message
     * @return \Illuminate\Http\Response
     */
    public function respondForbidden($message = 'Forbidden.')
    {
        return $this->setStatusCode(IlluminateResponse::HTTP_FORBIDDEN)->respondWithError($message);
    }

    /**
     * Return a 404 not found error
     * @param  string $message
     * @return \Illuminate\Http\Response
     */
    public function respondNotFound($message = 'Not found.')
    {
        return $this->setStatusCode(IlluminateResponse::HTTP_NOT_FOUND)->respondWithError($message);
    }

    /**
     * Return a 204 no content response
     * @param  string $message
     * @return \Illuminate\Http\Response
     */
    public function respondNoContent($message = '')
    {
        return $this->setStatusCode(IlluminateResponse::HTTP_NO_CONTENT)->respond($message);
    }

    /**
     * Return an error response
     * @param String $message
     * @return \Illuminate\Http\Response
     */
    public function respondWithError($message)
    {
        return $this->respond([
            'error' => [
                'message' => $message,
                'status' => $this->getStatusCode()
            ]
        ]);
    }

    /**
     * Bind an item to a transformer and return data.
     *
     * @param object $item
     * @param object $transformer
     * @param String $resourceKey
     *
     * @return \Illuminate\Http\Response
     */
    public function item($item, $transformer, $resourceKey = null)
    {
        $resource = new Item($item, $transformer, $resourceKey);

        return $this->respond($this->createData($resource));
    }

    /**
     * Bind a collection to a transformer and return data.
     *
     * @param object $collection
     * @param object $transformer
     * @param String $resourceKey
     *
     * @return \Illuminate\Http\Response
     */
    public function collection($items, $transformer, $resourceKey = null)
    {
        $resource = new Collection($items, $transformer, $resourceKey);

        return $this->respond($this->createData($resource));
    }

    /**
     * Bind a paginated collection to a transformer and return data.
     *
     * @param object $item
     * @param object $transformer
     * @param String $resourceKey
     *
     * @return \Illuminate\Http\Response
     */
    public function paginatedCollection($paginator, $transformer, $resourceKey = null)
    {
        $resource = new Collection($paginator->getCollection(), $transformer, $resourceKey);
        $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));

        return $this->respond($this->createData($resource));
    }

    /**
     * Return data of a transformed resource.
     *
     * @param object $resource
     * @return Array
     */
    private function createData($resource)
    {
        $manager = new Manager();
        $manager->setSerializer(new JsonApiSerializer());

        return $manager->createData($resource)->toArray();
    }
}<commit_msg>Adding transformer as controller attribute with getter and setter methods<commit_after><?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use Illuminate\Http\Response as IlluminateResponse;
use League\Fractal\Manager;
use League\Fractal\Serializer\JsonApiSerializer;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;

class ApiController extends Controller
{
    /**
     * Status code
     * @var String
     */
    protected $statusCode = IlluminateResponse::HTTP_OK;

    /**
     * Resource Transformer
     */
    protected $transformer;

    /**
     * Return the status code
     * @return String
     */
    public function getStatusCode()
    {
        return $this->statusCode;
    }

    /**
     * Set the Status Code
     * @param string $statusCode
     */
    public function setStatusCode($statusCode)
    {
        $this->statusCode = $statusCode;

        return $this;
    }

    /**
     * Return the transfomer
     */
    public function getTransformer()
    {
        return $this->transformer;
    }

    /**
     * Set the transformer
     * @param $transfomer
     */
    public function setTransformer($transformer)
    {
        $this->transformer = $transformer;

        return $this;
    }

    /**
     * Method to make a response in json.
     * @param  array $data
     * @param  array  $headers
     * @return \Illuminate\Http\Response
     */
    public function respond($data, $headers = [])
    {
        return response()->json($data, $this->getStatusCode(), $headers);
    }

    /**
     * Return a 401 unauthorized error.
     *
     * @param string $message
     * @return \Illuminate\Http\Response
     */
    public function respondUnauthorized($message = 'Unauthorized.')
    {
        return $this->setStatusCode(IlluminateResponse::HTTP_UNAUTHORIZED)->respondWithError($message);
    }

    /**
     * Return a 403 forbidden error.
     *
     * @param string $message
     * @return \Illuminate\Http\Response
     */
    public function respondForbidden($message = 'Forbidden.')
    {
        return $this->setStatusCode(IlluminateResponse::HTTP_FORBIDDEN)->respondWithError($message);
    }

    /**
     * Return a 404 not found error
     * @param  string $message
     * @return \Illuminate\Http\Response
     */
    public function respondNotFound($message = 'Not found.')
    {
        return $this->setStatusCode(IlluminateResponse::HTTP_NOT_FOUND)->respondWithError($message);
    }

    /**
     * Return a 204 no content response
     * @param  string $message
     * @return \Illuminate\Http\Response
     */
    public function respondNoContent($message = '')
    {
        return $this->setStatusCode(IlluminateResponse::HTTP_NO_CONTENT)->respond($message);
    }

    /**
     * Return an error response
     * @param String $message
     * @return \Illuminate\Http\Response
     */
    public function respondWithError($message)
    {
        return $this->respond([
            'error' => [
                'message' => $message,
                'status' => $this->getStatusCode()
            ]
        ]);
    }

    /**
     * Bind an item to a transformer and return data.
     *
     * @param object $item
     * @param String $resourceKey
     *
     * @return \Illuminate\Http\Response
     */
    public function item($item, $resourceKey = null)
    {
        $resource = new Item($item, $this->getTransformer(), $resourceKey);

        return $this->respond($this->createData($resource));
    }

    /**
     * Bind a collection to a transformer and return data.
     *
     * @param object $collection
     * @param String $resourceKey
     *
     * @return \Illuminate\Http\Response
     */
    public function collection($items, $resourceKey = null)
    {
        $resource = new Collection($items, $this->getTransformer(), $resourceKey);

        return $this->respond($this->createData($resource));
    }

    /**
     * Bind a paginated collection to a transformer and return data.
     *
     * @param object $item
     * @param String $resourceKey
     *
     * @return \Illuminate\Http\Response
     */
    public function paginatedCollection($paginator, $resourceKey = null)
    {
        $resource = new Collection($paginator->getCollection(), $this->getTransformer(), $resourceKey);
        $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));

        return $this->respond($this->createData($resource));
    }

    /**
     * Return data of a transformed resource.
     *
     * @param object $resource
     * @return Array
     */
    private function createData($resource)
    {
        $manager = new Manager();
        $manager->setSerializer(new JsonApiSerializer());

        return $manager->createData($resource)->toArray();
    }
}
{'fraction_non_alphanumeric': 0.12140781401356152, 'fraction_numerical': 0.002798407060596276, 'mean_word_length': 2.4710496824803885, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 64, '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': '1878068', 'n_tokens_mistral': 2585, 'n_tokens_neox': 2613, 'n_words': 586}