LeanMapper\Result::getReferencedResult PHP Method

getReferencedResult() private method

private getReferencedResult ( string $table, string $viaColumn, Filtering $filtering = null ) : self
$table string
$viaColumn string
$filtering Filtering
return self
    private function getReferencedResult($table, $viaColumn, Filtering $filtering = null)
    {
        if ($this->isDetached) {
            throw new InvalidStateException('Cannot get referenced Result for detached Result.');
        }
        $key = "{$table}({$viaColumn})";
        if (isset($this->referenced[$forcedKey = $key . '#' . self::KEY_FORCED])) {
            $ids = $this->extractIds($viaColumn);
            $primaryKey = $this->mapper->getPrimaryKey($table);
            foreach ($this->referenced[$forcedKey] as $filteringResult) {
                if ($filteringResult->isValidFor($ids, $filtering->getArgs())) {
                    return $filteringResult->getResult();
                }
            }
        }
        if (isset($this->referenced[$preloadedKey = $key . '#' . self::KEY_PRELOADED])) {
            return $this->referenced[$preloadedKey];
        }
        if ($filtering === null) {
            if (!isset($this->referenced[$key])) {
                if (!isset($ids)) {
                    $ids = $this->extractIds($viaColumn);
                    $primaryKey = $this->mapper->getPrimaryKey($table);
                }
                $data = [];
                if (!empty($ids)) {
                    $data = $this->createTableSelection($table, $ids)->where('%n.%n IN %in', $table, $primaryKey, $ids)->execute()->setRowClass(null)->fetchAll();
                }
                $this->referenced[$key] = self::createInstance($data, $table, $this->connection, $this->mapper);
            }
            return $this->referenced[$key];
        }
        // $filtering !== null
        if (!isset($ids)) {
            $ids = $this->extractIds($viaColumn);
            $primaryKey = $this->mapper->getPrimaryKey($table);
        }
        $statement = $this->createTableSelection($table, $ids)->where('%n.%n IN %in', $table, $primaryKey, $ids);
        $filteringResult = $this->applyFiltering($statement, $filtering);
        if ($filteringResult instanceof FilteringResultDecorator) {
            if (!isset($this->referenced[$forcedKey])) {
                $this->referenced[$forcedKey] = [];
            }
            $this->referenced[$forcedKey][] = $filteringResult;
            return $filteringResult->getResult();
        }
        $args = $statement->_export();
        $key .= '#' . $this->calculateArgumentsHash($args);
        if (!isset($this->referenced[$key])) {
            $data = $this->connection->query($args)->setRowClass(null)->fetchAll();
            $this->referenced[$key] = self::createInstance($data, $table, $this->connection, $this->mapper);
        }
        return $this->referenced[$key];
    }