Parse a transformer class and set relations.

@param  \Flugg\Responder\Transformer|callable $transformer
@param  \Illuminate\Database\ELoquent\Model   $model
@return \Flugg\Responder\Transformer|callable
@throws \InvalidTransformerException
7314
protected function parseTransformer($transformer, Model $model)
    {
        if ($transformer instanceof Transformer) {
            $relations = $transformer->allRelationsAllowed() ? $this->resolveRelations($model) : $transformer->getRelations();
            $transformer = $transformer->setRelations($relations);

        } elseif (! is_callable($transformer)) {
            throw new InvalidTransformerException($model);
        }

        return $transformer;
    }
545678
protected function fireTransformer($transformer, $data)
    {
        $includedData = [];

        if (is_callable($transformer)) {
            $transformedData = call_user_func($transformer, $data);
        } else {
            $transformer->setCurrentScope($this);
            $transformedData = $transformer->transform($data);
        }

        if ($this->transformerHasIncludes($transformer)) {
            $includedData = $this->fireIncludedTransformers($transformer, $data);
            $transformedData = $this->manager->getSerializer()->mergeIncludes($transformedData, $includedData);
        }

        //Stick only with requested fields
        $transformedData = $this->filterFieldsets($transformedData);

        return [$transformedData, $includedData];
    }
553419
public function addTransform($transform, $value)
    {
        if (is_string($transform) === false) {
            throw new \InvalidArgumentException(
                'Additional transforms must be non empty strings.'
            );
        }
        if (!array_key_exists($transform, $this->VALID_TRANSFORMS)) {
            throw new \InvalidArgumentException(
                'Invalid transform.'
            );
        }
        if($this->VALID_TRANSFORMS[$transform] == 'numeric' && !is_numeric($value))
        {
            throw new \InvalidArgumentException(
                'Transform '.$transform.' must have a numeric value.'
            );
        }
        if(is_array($this->VALID_TRANSFORMS[$transform]) && !in_array($value, $this->VALID_TRANSFORMS[$transform]))
        {
            throw new \InvalidArgumentException(
                'Transform '.$transform.' has an invalid value.'
            );
        }

        $this->transforms[] = array($transform, $value);

        return $this;
    }
751723
public function model(Model $model)
    {
        // Check if the model is searchable before setting the query builder model
        $traits = class_uses_recursive(get_class($model));

        if (!isset($traits[Searchable::class])) {
            throw new InvalidArgumentException(get_class($model).' does not use the searchable trait');
        }

        $this->type($model->getDocumentType());

        if ($index = $model->getDocumentIndex()) {
            $this->index($index);
        }

        $this->model = $model;

        return $this;
    }
858261
protected function replaceClasses($stub, $model, $transformer = null): string
    {
        if ($transformer) {
            $transformer = str_replace('/', '\\', $transformer);

            $namespaceTransformer = $this->rootNamespace().'\Transformers\\'.$transformer;

            if (starts_with($transformer, '\\')) {
                $stub = str_replace('NamespacedDummyTransformer', trim($transformer, '\\'), $stub);
            } else {
                $stub = str_replace('NamespacedDummyTransformer', $namespaceTransformer, $stub);
            }

            $stub = str_replace(
                "use {$namespaceTransformer};\nuse {$namespaceTransformer};", "use {$namespaceTransformer};", $stub
            );

            $transformer = class_basename(trim($transformer, '\\'));

            $stub = str_replace('DummyTransformer', $transformer, $stub);

            $stub = str_replace('dummyTransformer', camel_case($transformer), $stub);
        }

        $model = str_replace('/', '\\', $model);

        $namespaceModel = $this->rootNamespace().'\Models\\'.$model;

        if (starts_with($model, '\\')) {
            $stub = str_replace('NamespacedDummyModel', trim($model, '\\'), $stub);
        } else {
            $stub = str_replace('NamespacedDummyModel', $namespaceModel, $stub);
        }

        $stub = str_replace(
            "use {$namespaceModel};\nuse {$namespaceModel};", "use {$namespaceModel};", $stub
        );

        $model = class_basename(trim($model, '\\'));

        $stub = str_replace('DummyModel', $model, $stub);

        return str_replace('dummyModel', camel_case($model), $stub);
    }
803726
protected function parseModel($model)
    {
        if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {
            throw new InvalidArgumentException('Model name contains invalid characters.');
        }

        $model = trim(str_replace('/', '\\', $model), '\\');

        $namespace =
            trim($this->laravel->getNamespace(), '\\')
            . $this->getModuleNamespace()
            . config('stubs.namespaces.model') . '\\';

        if (!Str::startsWith($model, $namespace)) {
            $model = $namespace . $model;
        }

        return $model;
    }
202763
public function setTransformation($transformation)
    {
        if (is_callable($transformation)) {
            $this->transformationMethod = $transformation;

            return $this;
        }

        // replace just to avoid wrongly passing the name containing "Transformation".
        $methodName = str_replace('Transformation', '', $transformation) . "Transformation";

        if (! method_exists($this, $methodName)) {
            throw new TransformerException("No such transformation as $methodName defined.");
        }

        $this->transformationMethod = [$this, $methodName];

        return $this;
    }
379302
protected function makeTransformerInstance($transformerClass)
    {
        if ($transformerClass instanceof TransformerAbstract) {
            return $transformerClass;
        }

        $instance = app($transformerClass);

        if ( ! ($instance instanceof TransformerAbstract)) {
            throw new \UnexpectedValueException("{$transformerClass} is not a TransformerAbstract");
        }

        return $instance;
    }
127954
public function trees(StandardSitemapProvider $provider, TreeCollectionTransformer $transformer)
    {
        // Extract the tree from the sitemap provider
        $tree = $provider->getTreeCollection();

        // Return a resource
        return $this->app->make(Item::class, [$tree, $transformer]);
    }
1008014
public function make($transformer)
    {
        // try to create a transformer by class name, if we can autoload
        // the class from the namespace we will create a new class and
        // return the new transformer.
        if(is_string($transformer)) {
            return $this->makeTransformerClass($transformer);
        }

        // if we get a callable, then we will create a callable transformer
        // that warps the callable and implements a transform function so
        // any resource will know a single interface for transforming.
        else if(is_callable($transformer)) {
            return $this->makeCallableTransformer($transformer);
        }

        // in case we get an object check if it is an instance of a transformer
        // that implements the transform method.
        else if(is_object($transformer) && method_exists($transformer, 'transform')) {
            return $transformer;
        }

        throw new ResolveTransformerException("Cannot resolve transformer");
    }
1290327
public function recreateForNewResponseTransformer(ResponseTransformerInterface $responseTransformer): self
    {
        return new self($this->client, $responseTransformer, $this->apiExceptionTransformer, $this->accessToken);
    }
1107653
public function process(ContainerBuilder $container)
    {
        $tag        = 'dataset.transformer';
        $interface  = DataSetTransformerInterface::class;
        $definition = $container->getDefinition('dataset.transformer.collection');

        foreach ($container->findTaggedServiceIds($tag) as $id => $attributes) {
            $itemDefinition = $container->getDefinition($id);
            $refClass       = new \ReflectionClass($itemDefinition->getClass());

            if (!$refClass->implementsInterface($interface)) {
                throw new \InvalidArgumentException(
                    sprintf('DataSetTransformer "%s" tagged with "%s" must implement interface "%s".', $id, $tag, $interface)
                );
            }

            $definition->addMethodCall('add', [
                $attributes[0]['alias'],
                new Reference($id)
            ]);
        }
    }
126540
public function assertModel(AbstractModel $model)
    {
        if (! $this->isModel($model)) {
            throw new InvalidArgumentException(
                sprintf(
                    'Model must be instance of %s, but was %s',
                    $this->getRootConfig()->getModelClass(),
                    get_class($model)
                )
            );
        }
    }
511898
public function addTransformer(TransformerInterface $transformer, string $key = "", string $prefix = NULL) {
        if(!in_array($transformer, $this->transformers)) {
            $id = $prefix ? "$prefix.$key" : $key;
            $this->transformers[$id][] = $transformer;
        }
    }
647730
final public function addTransformer(Transformer $transformer)
    {
        if ($this->hasTransformer($transformer)) {
            throw new DuplicateTransformerException('Transformer already added.');
        }

        $this->transformers[spl_object_hash($transformer)] = $transformer;

        return $this;
    }
1229435
public function getCurrencyTransformer($language)
    {
        if (!array_key_exists($language, $this->currencyTransformers)) {
            throw new \InvalidArgumentException(sprintf(
                'Currency transformer for "%s" language is not implemented.',
                $language
            ));
        }

        return new $this->currencyTransformers[$language];
    }
110556
protected function transformerHasIncludes($transformer)
    {
        if (! $transformer instanceof TransformerAbstract) {
            return false;
        }

        $defaultIncludes = $transformer->getDefaultIncludes();
        $availableIncludes = $transformer->getAvailableIncludes();

        return ! empty($defaultIncludes) || ! empty($availableIncludes);
    }