CommonQuery::addJoinStatements PHP Method

addJoinStatements() private method

Statement can contain more tables (e.g. "table1.table2:table3:")
private addJoinStatements ( $clause, $statement, array $parameters = [] )
$clause
$statement
$parameters array
    private function addJoinStatements($clause, $statement, $parameters = array())
    {
        if ($statement === null) {
            $this->joins = array();
            return $this->resetClause('JOIN');
        }
        if (array_search(substr($statement, 0, -1), $this->joins) !== false) {
            return $this;
        }
        // match "tables AS alias"
        preg_match('/`?([a-z_][a-z0-9_\\.:]*)`?(\\s+AS)?(\\s+`?([a-z_][a-z0-9_]*)`?)?/i', $statement, $matches);
        $joinAlias = '';
        $joinTable = '';
        if ($matches) {
            $joinTable = $matches[1];
            if (isset($matches[4]) && !in_array(strtoupper($matches[4]), array('ON', 'USING'))) {
                $joinAlias = $matches[4];
            }
        }
        if (strpos(strtoupper($statement), ' ON ') || strpos(strtoupper($statement), ' USING')) {
            if (!$joinAlias) {
                $joinAlias = $joinTable;
            }
            if (in_array($joinAlias, $this->joins)) {
                return $this;
            } else {
                $this->joins[] = $joinAlias;
                $statement = " {$clause} {$statement}";
                return $this->addStatement('JOIN', $statement, $parameters);
            }
        }
        // $joinTable is list of tables for join e.g.: table1.table2:table3....
        if (!in_array(substr($joinTable, -1), array('.', ':'))) {
            $joinTable .= '.';
        }
        preg_match_all('/([a-z_][a-z0-9_]*[\\.:]?)/i', $joinTable, $matches);
        $mainTable = '';
        if (isset($this->statements['FROM'])) {
            $mainTable = $this->statements['FROM'];
        } elseif (isset($this->statements['UPDATE'])) {
            $mainTable = $this->statements['UPDATE'];
        }
        $lastItem = array_pop($matches[1]);
        array_push($matches[1], $lastItem);
        foreach ($matches[1] as $joinItem) {
            if ($mainTable == substr($joinItem, 0, -1)) {
                continue;
            }
            // use $joinAlias only for $lastItem
            $alias = '';
            if ($joinItem == $lastItem) {
                $alias = $joinAlias;
            }
            $newJoin = $this->createJoinStatement($clause, $mainTable, $joinItem, $alias);
            if ($newJoin) {
                $this->addStatement('JOIN', $newJoin, $parameters);
            }
            $mainTable = $joinItem;
        }
        return $this;
    }