queryBuilder = $queryBuilder; } public function add(array $row) : self { if ($this->fieldCount === null) { $this->fieldCount = count($row); } # index starts at '1' for Pdo params $this->rows[] = array_combine(range(1, count($row)), array_values($row)); return $this; } public function render() : string { $this->queryBuilder->addValues($this->flattenRowsArray()); return $this->renderSegments([ 'VALUES', $this->renderParameterPlaceholders(), ]); } public function renderParameterPlaceholders() : string { $return = []; foreach($this->rows as $row) { if ($this->fieldCount === null) { } else { if (count($row) !== $this->fieldCount) { throw new \Exception("Insert statement must contains the same number of values for each rows."); } } $return[] = implode(',', array_fill(0, $this->fieldCount, '?')); } return "(" . implode('),(', $return) . ")"; } public function flattenRowsArray() : array { return \iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->rows))); } }