<commit_before><?php namespace App\Services;

use Illuminate\Pagination\Paginator;
use Illuminate\Support\Contracts\ArrayableInterface;
use League\Fractal\Manager;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Serializer\SerializerAbstract;
use League\Fractal\TransformerAbstract;

class Transformer
{
    /**
     * Fractal manager.
     *
     * @var \League\Fractal\Manager
     */
    protected $manager;

    /**
     * Initialize the transform manager.
     *
     * @param  SerializerAbstract $serializer
     * @return void
     */
    public function __construct(SerializerAbstract $serializer)
    {
        $this->manager = new Manager();
        $this->manager->setSerializer($serializer);
    }

    /**
     * Parse Include String.
     *
     * @param array|string $includes Array or csv string of resources to include
     *
     * @return $this
     */
    public function parseIncludes($includes)
    {
        $this->manager->parseIncludes($includes);
    }

    /**
     * Create transformed data from a collection.
     *
     * @param  object  $data
     * @param  League\Fractal\TransformerAbstract  $transformer
     * @param  string  $resourceKey
     * @return array
     */
    public function collection($data, $transformer = null, $resourceKey = null)
    {
        $resource = new Collection($data, $this->getTransformer($transformer), $resourceKey);

        return $this->manager->createData($resource)->toArray()['data'];
    }

    /**
     * Create transformed data from an item.
     *
     * @param  object  $data
     * @param  League\Fractal\TransformerAbstract  $transformer
     * @param  string  $resourceKey
     * @return array
     */
    public function item($data, $transformer = null, $resourceKey = null)
    {
        $resource = new Item($data, $this->getTransformer($transformer), $resourceKey);

        return $this->manager->createData($resource)->toArray();
    }

    /**
     * Create paginated transformed data from a collection.
     *
     * @param  Paginator  $paginator
     * @param  League\Fractal\TransformerAbstract  $transformer
     * @param  string  $resourceKey
     * @return array
     */
    public function paginatedCollection(Paginator $paginator, $transformer = null, $resourceKey = null)
    {
        $paginator->appends(\Request::query());

        $resource = new Collection($paginator->getCollection(), $this->getTransformer($transformer), $resourceKey);

        $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));

        return $this->manager->createData($resource)->toArray();
    }

    /**
     * Get the transformer to use.
     *
     * @param  TransformerAbstract  $transformer
     * @return TransformerAbstract|callback
     */
    protected function getTransformer($transformer = null)
    {
        return $transformer ?: function($data) {

            if($data instanceof ArrayableInterface) {
                return $data->toArray();
            }

            return (array) $data;
        };
    }
}<commit_msg>Adjusts service for 5.1<commit_after><?php namespace App\Services;

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\Support\Arrayable;
use League\Fractal\Manager;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Serializer\SerializerAbstract;
use League\Fractal\TransformerAbstract;

class Transformer
{
    /**
     * Fractal manager.
     *
     * @var \League\Fractal\Manager
     */
    protected $manager;

    /**
     * Initialize the transform manager.
     *
     * @param  SerializerAbstract  $serializer
     * @return void
     */
    public function __construct(SerializerAbstract $serializer)
    {
        $this->manager = new Manager();
        $this->manager->setSerializer($serializer);
    }

    /**
     * Parse Include String.
     *
     * @param  array|string  $includes
     *
     * @return $this
     */
    public function parseIncludes($includes)
    {
        $this->manager->parseIncludes($includes);
    }

    /**
     * Create transformed data from a collection.
     *
     * @param  object  $data
     * @param  League\Fractal\TransformerAbstract  $transformer
     * @param  string  $resourceKey
     * @return array
     */
    public function collection($data, $transformer = null, $resourceKey = null)
    {
        $resource = new Collection($data, $this->getTransformer($transformer), $resourceKey);

        return $this->manager->createData($resource)->toArray()['data'];
    }

    /**
     * Create transformed data from an item.
     *
     * @param  object  $data
     * @param  League\Fractal\TransformerAbstract  $transformer
     * @param  string  $resourceKey
     * @return array
     */
    public function item($data, $transformer = null, $resourceKey = null)
    {
        $resource = new Item($data, $this->getTransformer($transformer), $resourceKey);

        return $this->manager->createData($resource)->toArray();
    }

    /**
     * Create paginated transformed data from a collection.
     *
     * @param  LengthAwarePaginator  $paginator
     * @param  League\Fractal\TransformerAbstract  $transformer
     * @param  string  $resourceKey
     * @return array
     */
    public function paginatedCollection(LengthAwarePaginator $paginator, $transformer = null, $resourceKey = null)
    {
        $paginator->appends(\Request::query());

        $resource = new Collection($paginator->getCollection(), $this->getTransformer($transformer), $resourceKey);

        $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));

        return $this->manager->createData($resource)->toArray();
    }

    /**
     * Get the transformer to use.
     *
     * @param  TransformerAbstract  $transformer
     * @return TransformerAbstract|callback
     */
    protected function getTransformer($transformer = null)
    {
        return $transformer ?: function ($data) {

            if ($data instanceof Arrayable) {
                return $data->toArray();
            }

            return (array) $data;
        };
    }
}
{'fraction_non_alphanumeric': 0.11820217530390276, 'fraction_numerical': 0.0003198976327575176, 'mean_word_length': 2.7220238095238094, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, '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': '3200005', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1753, 'n_words': 371}