public function items(ItemsEvent $event)
{
if (is_array($event->target) && count($event->target) > 1) {
if (isset($_GET[$event->options['sortFieldParameterName']])) {
$this->sortDirection = isset($_GET[$event->options['sortDirectionParameterName']]) && strtolower($_GET[$event->options['sortDirectionParameterName']]) === 'asc' ? 'asc' : 'desc';
// TODO add whitelist
// if (isset($event->options['sortFieldWhitelist'])) {
// if (!in_array($_GET[$event->options['sortFieldParameterName']], $event->options['sortFieldWhitelist'])) {
// throw new \UnexpectedValueException("Cannot sort by: [{$_GET[$event->options['sortFieldParameterName']]}] this field is not in whitelist");
// }
// }
$sortFieldParameterName = explode('.', $_GET[$event->options['sortFieldParameterName']]);
if (isset($sortFieldParameterName[1])) {
// Capitalize first letter in order to prepare getter construction
$sortFieldName = ucfirst($sortFieldParameterName[1]);
$this->currentSortingFieldGetter = "get{$sortFieldName}";
// Getter detection
$class = new ReflectionClass(get_class($event->target[0]));
if ($class->hasMethod($this->currentSortingFieldGetter)) {
// Sort
usort($event->target, array($this, "sort" . ucfirst($this->sortDirection)));
}
}
}
}
}