/**
* @inheritdoc
* @throws InvalidArgumentException
*/
public function applyQuery(Fluent $fluent, IMapper $mapper)
{
// NOTE:
// $fluent is expected to have called method Fluent::from
// with pure table name as an argument. For example:
// $fluent->from('author');
//
// So something like
// $fluent->from('[author]');
// is not supported. If a Fluent::from method is called multiple
// times, the table name from the first call is used as
// the source table.
//
// The advantage of this way is that there is no need to explicitly
// specify $tableName when calling Query::applyQuery anymore.
$fromClause = $fluent->_export('FROM');
if (count($fromClause) < 3 || $fromClause[1] !== '%n') {
throw new InvalidArgumentException('Unsupported fluent from clause. Only pure table name as an argument of \\LeanMapper\\Fluent::from method is supported.');
}
$this->sourceTableName = $fromClause[2];
if (count($fromClause) > 3) {
// complicated from clause
$subFluent = clone $fluent;
// Reset fluent.
foreach (array_keys(\DibiFluent::$separators) as $separator) {
$fluent->removeClause($separator);
}
// If there are some joins, enwrap the original fluent to enable
// accessing columns from joined tables.
$fluent->select('*')->from($subFluent, $this->sourceTableName);
}
$this->fluent = $fluent;
$this->mapper = $mapper;
// Add source table name to tables aliases list to avoid error
// when joining to itself.
$this->tablesAliases = array($this->sourceTableName);
foreach ($this->queue as $call) {
list($method, $args) = $call;
call_user_func_array(array($this, $method), $args);
}
// Reset fluent.
$this->fluent = NULL;
return $fluent;
}