<commit_before><?php

namespace Flugg\Responder\Transformers\Concerns;

use Flugg\Responder\Contracts\Resources\ResourceFactory;
use Illuminate\Contracts\Container\Container;
use Illuminate\Database\Eloquent\Model;
use League\Fractal\Resource\ResourceInterface;
use LogicException;

/**
 * A trait to be used by a transformer to make related resources.
 *
 * @package flugger/laravel-responder
 * @author  <NAME> <<EMAIL>>
 * @license The MIT License
 */
trait MakesResources
{
    /**
     * A list of cached related resources.
     *
     * @var \League\Fractal\ResourceInterface[]
     */
    protected $resources = [];

    /**
     * Make a resource.
     *
     * @param  null                                                           $data
     * @param  \Flugg\Responder\Transformers\Transformer|string|callable|null $transformer
     * @param  string|null                                                    $resourceKey
     * @return \League\Fractal\Resource\ResourceInterface
     */
    protected function resource($data = null, $transformer = null, string $resourceKey = null): ResourceInterface
    {
        $resourceFactory = $this->resolveContainer()->make(ResourceFactory::class);

        return $resourceFactory->make($data, $transformer, $resourceKey);
    }

    /**
     * Include a related resource.
     *
     * @param  string $identifier
     * @param  mixed  $data
     * @param  array  $parameters
     * @return \League\Fractal\Resource\ResourceInterface
     * @throws \LogicException
     */
    protected function includeResource(string $identifier, $data, array $parameters): ResourceInterface
    {
        if (method_exists($this, $method = 'include' . ucfirst($identifier))) {
            $resource = $this->$method($data, $parameters);
        } elseif ($data instanceof Model) {
            $resource = $this->includeResourceFromModel($data, $identifier);
        } else {
            throw new LogicException('Relation [' . $identifier . '] not found in [' . get_class($this) . '].');
        }

        return $resource;
    }

    /**
     * Include a related resource from a model.
     *
     * @param \Illuminate\Database\Eloquent\Model $model
     * @param string                              $identifier
     * @return \League\Fractal\Resource\ResourceInterface
     */
    protected function includeResourceFromModel(Model $model, string $identifier): ResourceInterface
    {
        $data = $this->resolveRelation($model, $identifier);

        if (! count($data)) {
            return $this->resource($data, null, $identifier);
        } elseif (key_exists($identifier, $this->resources)) {
            return $this->resources[$identifier]->setData($data);
        }

        return $this->resources[$identifier] = $this->resource($data, null, $identifier);<commit_msg>Fix count() usage in MakesResources when running on PHP7.2<commit_after><?php

namespace Flugg\Responder\Transformers\Concerns;

use Countable;
use Flugg\Responder\Contracts\Resources\ResourceFactory;
use Illuminate\Contracts\Container\Container;
use Illuminate\Database\Eloquent\Model;
use League\Fractal\Resource\ResourceInterface;
use LogicException;

/**
 * A trait to be used by a transformer to make related resources.
 *
 * @package flugger/laravel-responder
 * @author  <NAME> <<EMAIL>>
 * @license The MIT License
 */
trait MakesResources
{
    /**
     * A list of cached related resources.
     *
     * @var \League\Fractal\ResourceInterface[]
     */
    protected $resources = [];

    /**
     * Make a resource.
     *
     * @param  null                                                           $data
     * @param  \Flugg\Responder\Transformers\Transformer|string|callable|null $transformer
     * @param  string|null                                                    $resourceKey
     * @return \League\Fractal\Resource\ResourceInterface
     */
    protected function resource($data = null, $transformer = null, string $resourceKey = null): ResourceInterface
    {
        $resourceFactory = $this->resolveContainer()->make(ResourceFactory::class);

        return $resourceFactory->make($data, $transformer, $resourceKey);
    }

    /**
     * Include a related resource.
     *
     * @param  string $identifier
     * @param  mixed  $data
     * @param  array  $parameters
     * @return \League\Fractal\Resource\ResourceInterface
     * @throws \LogicException
     */
    protected function includeResource(string $identifier, $data, array $parameters): ResourceInterface
    {
        if (method_exists($this, $method = 'include' . ucfirst($identifier))) {
            $resource = $this->$method($data, $parameters);
        } elseif ($data instanceof Model) {
            $resource = $this->includeResourceFromModel($data, $identifier);
        } else {
            throw new LogicException('Relation [' . $identifier . '] not found in [' . get_class($this) . '].');
        }

        return $resource;
    }

    /**
     * Include a related resource from a model.
     *
     * @param \Illuminate\Database\Eloquent\Model $model
     * @param string                              $identifier
     * @return \League\Fractal\Resource\ResourceInterface
     */
    protected function includeResourceFromModel(Model $model, string $identifier): ResourceInterface
    {
        $data = $this->resolveRelation($model, $identifier);

        if (! is_array($data) && ! $data instanceof Countable) {
            return $this->resource($data, null, $identifier);
        } elseif (key_exists($identifier, $this->resources)) {
            return $this->resources[$identifier]->setData($data);
        }

        return $this->resources[$identifier] = $this->resource($data, null, $identifier);
{'fraction_non_alphanumeric': 0.12175126460840746, 'fraction_numerical': 0.00034885749171463456, 'mean_word_length': 2.4984746796827335, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5386070', 'n_tokens_mistral': 1532, 'n_tokens_neox': 1506, 'n_words': 348}