/** * Extends the {@link $query} with Limits, Offsets, and Orderbys specified in the DTO * * @param Database $db The database this query will be used on (used to escape values) * @param Query $query The query object that we'll extend with the limits, offsets and orderbys * @param DTO $dto The DTO object that contains our limits, offsets, and orderbys * @param array $defaultSorts An array of the form [name => direction] that defines default sorts (used if none are in the DTO) * * @return void */ public function buildLimitOffsetOrderbys($db, $query, $dto, array $defaultSorts) { if ($dto->getLimit() != null) { $query->LIMIT($dto->getLimit()); } if ($dto->getOffset() != null) { $query->OFFSET($dto->getOffset()); } $query->ORDERBY(); $sorts = array(); $arr = func_num_args() == 5 ? func_get_arg(4) : null; $arr = is_array($arr) ? $arr : array_slice(func_get_args(), 4); $dtoSorts = $dto->getOrderBys(); if ($dtoSorts == null) { $dtoSorts = $defaultSorts; } $diff = array_diff(array_keys($dtoSorts), array_keys($arr)); $merged = array_merge($diff, $arr); $sorts = array_unique($merged); foreach ($sorts as $name => $column) { if (is_int($name)) { $name = $column; } if (isset($dtoSorts[$name])) { $direction = $dtoSorts[$name]; if (strcasecmp($name, 'FIELD') === 0) { if (count($dto->getParameter($direction)) == 0 || array_sum($dto->getParameter($direction)) == 0) { continue; } $query->ORDERBY('FIELD(' . $direction . ',' . $db->joinQuote($dto->getParameter($direction)) . ')'); } else { $query->ORDERBY("{$column} {$direction}"); } } } }