<commit_before><?php

namespace Laravel\Lumen\Routing;

use Dingo\Api\Routing\Helpers as BaseHelpers;

trait Helpers
{
    use BaseHelpers;

    /**
     * Transform and serialize the instance.
     *
     * @param  \Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection  $instance
     * @param  string  $name
     * @param  string|null  $serializer
     *
     * @return mixed
     */
    protected function transform($instance, $transformer, $serializer = null)
    {
        if (is_null($serializer)) {
            $serializer = $transformer;
        }

        return $this->serializeWith($this->transformWith($instance, $transformer), $serializer);
    }

    /**
     * Transform the instance.
     *
     * @param  \Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection  $instance
     * @param  string  $name
     *
     * @return mixed
     */
    protected function transformWith($instance, $name)
    {
        $transformer = $this->getVersionedResourceClassName($name);

        if (class_exists($transformer)) {
            return $instance->transform(app($transformer));
        }

        return $instance;
    }

    /**
     * Transform the instance.
     *
     * @param  mixed  $instance
     * @param  string  $name
     *
     * @return mixed
     */
    protected function serializeWith($instance, $name)
    {
        $serializer = $this->getVersionedResourceClassName($name);

        if (class_exists($serializer)) {
            return call_user_func(app($serializer), $instance);
        }

        return $instance;
    }

    /**
     * Get versioned resource class name.
     *
     * @param  string  $name
     *
     * @return string
     */
    protected function getVersionedResourceClassName($name)
    {
        $class   = str_replace('.', '\\', $name);
        $version = $this->getVersionNamespace();

        return sprintf('%s\%s\%s', $this->namespace, $version, $class);
    }

    /**
     * Get the version namespace.
     *
     * @return string<commit_msg>Serialization should always return array.<commit_after><?php

namespace Laravel\Lumen\Routing;

use Illuminate\Contracts\Support\Arrayable;
use Dingo\Api\Routing\Helpers as BaseHelpers;

trait Helpers
{
    use BaseHelpers;

    /**
     * Transform and serialize the instance.
     *
     * @param  \Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection  $instance
     * @param  string  $name
     * @param  string|null  $serializer
     *
     * @return array
     */
    protected function transform($instance, $transformer, $serializer = null)
    {
        if (is_null($serializer)) {
            $serializer = $transformer;
        }

        return $this->serializeWith(
            $this->transformWith($instance, $transformer), $serializer
        );
    }

    /**
     * Transform the instance.
     *
     * @param  \Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection  $instance
     * @param  string  $name
     *
     * @return mixed
     */
    protected function transformWith($instance, $name)
    {
        $transformer = $this->getVersionedResourceClassName($name);

        if (class_exists($transformer)) {
            return $instance->transform(app($transformer));
        }

        return $instance;
    }

    /**
     * Serialize the instance.
     *
     * @param  mixed  $instance
     * @param  string  $name
     *
     * @return array
     */
    protected function serializeWith($instance, $name)
    {
        $serializer = $this->getVersionedResourceClassName($name);

        if (class_exists($serializer)) {
            return call_user_func(app($serializer), $instance);
        }

        return $instance instanceof Arrayable ? $collection->toArray() : $collection;
    }

    /**
     * Get versioned resource class name.
     *
     * @param  string  $name
     *
     * @return string
     */
    protected function getVersionedResourceClassName($name)
    {
        $class   = str_replace('.', '\\', $name);
        $version = $this->getVersionNamespace();

        return sprintf('%s\%s\%s', $this->namespace, $version, $class);
    }

    /**
     * Get the version namespace.
     *
     * @return string
{'fraction_non_alphanumeric': 0.12535680304471933, 'fraction_numerical': 0.0, 'mean_word_length': 2.2546439628482973, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, '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': '886632', 'n_tokens_mistral': 1213, 'n_tokens_neox': 1220, 'n_words': 244}