Get relation array

@param Repository $repository
@param array $relations
@return array
541504
private function getRelations(Repository $repository, array $relations = [])
    {
        // if we have relation array passed, ignore request input
        if (count($relations) === 0) {
            $relations = Input::get('with');

            if (!is_array($relations)) {
                $relations = $this->inputService->decode('with');
            }
        }

        $result = [];
        $repo_relations = $repository->relations();
        foreach ($relations as $key => $value) {
            if (in_array($key, $repo_relations)) {
                $result[] = $key;
            } elseif (in_array($value, $repo_relations)) {
                $result[] = $value;
            }
        }

        return $result;
    }
475723
public function get(string $relation): RelationInterface
    {
        if (isset($this->relations[$relation]) && $this->relations[$relation] instanceof RelationInterface) {
            return $this->relations[$relation];
        }

        $instance = $this->orm->makeRelation($this->class, $relation);
        if (array_key_exists($relation, $this->relations)) {
            //Indicating that relation is loaded
            $instance = $instance->withContext($this->parent, true, $this->relations[$relation]);
        } else {
            //Not loaded relation
            $instance = $instance->withContext($this->parent, false);
        }

        return $this->relations[$relation] = $instance;
    }
1037874
public function getRelations():array
    {
        $relations = array_unique(array_merge($this->getAvailableIncludes(), $this->relations));

        return array_filter($relations, function($relation) {
            return $relation !== '*';
        });
    }
178503
public function initRelation(array $models, $relation)
    {
        foreach ($models as $model) {
            $model->setRelation($relation, []);
        }
        return $models;
    }
106795
public function initRelation(array $models, $relation)
    {
        foreach ($models as $model)
        {
            // In the case of fetching nested relations, we will get an array
            // with the first key being the model we need, and the other being
            // the related model so we'll just take the first model out of the array.
            if (is_array($model)) $model = reset($model);

            $model->setRelation($relation, $this->related->newCollection());
        }

        return $models;
    }
585297
public function getRepositoryEntities($repository)
    {
        if (!isset($this->_repository_entities[$repository])) {
            $this->_repository_entities[$repository] =
                $this->getService('anahita:domain.entityset', array('repository' => $this));
        }

        return $this->_repository_entities[$repository];
    }
1344269
protected function convertDotNotatedRelations($relations)
    {
        $dottedRelations = [];
        foreach ($relations as $name => $joryData) {
            $exploded = explode('.', $name);

            if (count($exploded) > 1) {
                // There was a dot, add it to the subRelations
                $firstRelation = $exploded[0];
                unset($exploded[0]);

                // implode all next layers, they will be handled in the next parser
                $dottedRelations[$firstRelation][implode('.', $exploded)] = $joryData;

                unset($relations[$name]);
            }
        }

        foreach ($dottedRelations as $name => $subRelations) {
            if (!array_key_exists($name, $relations)) {
                // This relation doesn't already exists, create it
                $relations[$name] = ['rlt' => []];
            }

            foreach ($subRelations as $subName => $joryData) {
                if (array_key_exists('rlt', $relations[$name])) {
                    $relations[$name]['rlt'][$subName] = $joryData;
                } else {
                    $relations[$name]['relations'][$subName] = $joryData;
                }
            }
        }

        return $relations;
    }
464397
public function getRelations()
    {
        if ($this->def->getTarget() === null) {
            return [];
        }

        $relations = [];
        foreach ($this->def->getTarget()->getRelations() as $relation) {
            $relations[$relation->getLocalProperty()] = $relation;
        }

        return $relations;
    }
184656
public function match(array $results, $relation)
    {
        $entities = $this->getEager();

        // TODO; optimize this operation
        $dictionary = $this->buildDictionary($entities);

        $keyName = $this->parentMap->getKeyName();

        $cache = $this->parentMapper->getEntityCache();

        $host = $this;

        // Once we have an array dictionary of child objects we can easily match the
        // children back to their parent using the dictionary and the keys on the
        // the parent models. Then we will return the hydrated models back out.
        return array_map(function ($result) use ($dictionary, $keyName, $cache, $relation, $host) {
            $key = $result[$keyName];

            if (isset($dictionary[$key])) {
                $collection = $host->relatedMap->newCollection($dictionary[$key]);

                $result[$relation] = $collection;

                // TODO Refactor this
                $cache->cacheLoadedRelationResult($key, $relation, $collection, $this);
            } else {
                $result[$relation] = $host->relatedMap->newCollection();
            }

            return $result;
        }, $results);
    }
1107086
protected function parseWithRelations(array $relations)
    {
        $results = [];

        foreach ($relations as $name => $constraints) {
            // If the "relation" value is actually a numeric key, we can assume that no
            // constraints have been specified for the eager load and we'll just put
            // an empty Closure with the loader so that we can treat all the same.
            if (is_numeric($name)) {
                $name = $constraints;

                list($name, $constraints) = Str::contains($name, ':')
                    ? $this->createSelectWithConstraint($name)
                    : [$name, function () {
                        //
                    }];
            }

            // We need to separate out any nested includes. Which allows the developers
            // to load deep relationships using "dots" without stating each level of
            // the relationship with its own key in the array of eager load names.
            $results = $this->addNestedWiths($name, $results);

            $results[$name] = $constraints;
        }

        return $results;
    }
160978
public function relations(array $relations)
    {
        foreach ($relations as $column1 => $column2) {
            $this->relation($column1, $column2);
        }

        return $this;
    }
569306
protected function joinParameters(Relation $relation)
    {
        $relatedTable = $relation->getRelated()->getTable();

        if ($relation instanceof BelongsTo) {
            return [
                $relatedTable,
                $relation->getForeignKey(),
                '=',
                $relatedTable.'.'.$relation->getRelated()->getKeyName(),
            ];
        }

        if ($relation instanceof HasOne) {
            return [
                $relatedTable,
                $relation->getQualifiedParentKeyName(),
                '=',
                $relation->getQualifiedForeignKeyName(),
            ];
        }

        throw new \Exception('Related sortable only support `HasOne` and `BelongsTo` relation.');
    }
1267983
protected function getMigrationPaths(Repository $repository)
    {
        $slug  = $this->argument('slug');
        $paths = [];

        if ($slug) {
            $paths[] = module_path($slug, 'Database/Migrations', $repository->location);
        } else {
            foreach ($repository->all() as $module) {
                $paths[] = module_path($module['slug'], 'Database/Migrations', $repository->location);
            }
        }

        return $paths;
    }
122477
protected function cacheRelation($results, $relation)
    {
        $cache = $this->parentMapper->getEntityCache();

        $cache->cacheLoadedRelationResult($this->parent->getEntityKeyName(), $relation, $results, $this);
    }
1057476
public function findRelation($entity, $relationName, $orderColumn = null, $orderType = null) {
        $this->load();
        $mapper = MapperBuilder::buildFromClassName($this->mapping, get_class($entity));
        $relation = $mapper->getRelation($relationName);
        $relationMapper = MapperBuilder::buildFromName($this->mapping, $relation->getEntity());

        $selectQuery = "SELECT tmain.* FROM `" . $relationMapper->getTable() . "` tmain ";
        $joinQuery = QueryBuilder::buildJoinRelationQuery($relation, "tmain", "j1");
        $whereQuery = "WHERE j1." . $relation->getLocalColumn() . " = '" . $entity->getId() . "'";

        $orderQuery = "";
        if (!is_null($orderColumn)) {
            $orderQuery .= "ORDER BY $orderColumn ";
            if (!is_null($orderType)) {
                $orderQuery .= "$orderType";
            } else {
                $orderQuery .= "ASC";
            }
        }

        $query = $selectQuery . $joinQuery . $whereQuery . $orderQuery;
        $queryResult = $this->getDataSource()->query($query);
        if ($queryResult) {
            $collection = new Collection($relationMapper->getClass(), $queryResult, $relationMapper);
        } else {
            $collection = new Collection($relationMapper->getClass(), array(), $relationMapper);
        }

        return $collection;
    }
347185
protected function matchOneOrMany(array $results, $relation, $type)
    {
        $entities = $this->getEager();

        $dictionary = $this->buildDictionary($entities);

        $cache = $this->parentMapper->getEntityCache();

        $host = $this;

        // Once we have the dictionary we can simply spin through the parent models to
        // link them up with their children using the keyed dictionary to make the
        // matching very convenient and easy work. Then we'll just return them.
        return array_map(function ($result) use ($dictionary, $cache, $type, $relation, $host) {
            $key = $result[$host->localKey];

            if (isset($dictionary[$key])) {
                $value = $host->getRelationValue($dictionary, $key, $type);

                $result[$relation] = $value;

                // TODO : Refactor This
                $cache->cacheLoadedRelationResult($key, $relation, $value, $this);
            } else {
                $result[$relation] = $type === 'many' ? $this->relatedMap->newCollection() : null;
            }

            return $result;
        }, $results);
    }
390427
public static function findRelation($row, $relation): array
    {
        $model = $row->getTable()->getModel();

        /** @var \Bluz\Db\Table $relationTable */
        $relationTable = self::getModelClass($relation);
        $relationTable::getInstance();

        if (!$relations = self::getRelations($model, $relation)) {
            throw new RelationNotFoundException(
                "Relations between model `$model` and `$relation` is not defined"
            );
        }

        // check many-to-many relations
        if (\count($relations) === 1) {
            $relations = self::getRelations($model, current($relations));
        }

        $field = $relations[$model];
        $key = $row->{$field};

        return self::findRelations($model, $relation, [$key]);
    }