You are here

protected function DataProviderVariable::applySort in RESTful 7.2

Sorts plugins on the list based on the request options.

Parameters

\Drupal\restful\Plugin\resource\ResourceInterface[] $variables: The array of resource plugins keyed by instance ID.

Return value

\Drupal\restful\Plugin\resource\ResourceInterface[] The sorted array.

Throws

\Drupal\restful\Exception\BadRequestException

\Drupal\restful\Exception\ServiceUnavailableException

1 call to DataProviderVariable::applySort()
DataProviderVariable::getIndexIds in modules/restful_example/src/Plugin/resource/variables/DataProviderVariable.php
Returns the ID to render for the current index GET request.

File

modules/restful_example/src/Plugin/resource/variables/DataProviderVariable.php, line 200
Contains \Drupal\restful_example\Plugin\resource\variables\DataProviderVariable.

Class

DataProviderVariable
Class DataProviderVariable.

Namespace

Drupal\restful_example\Plugin\resource\variables

Code

protected function applySort(array $variables) {
  if ($sorts = $this
    ->parseRequestForListSort()) {
    uasort($variables, function ($variable1, $variable2) use ($sorts) {
      $interpreter1 = $this
        ->initDataInterpreter($variable1['name']);
      $interpreter2 = $this
        ->initDataInterpreter($variable2['name']);
      foreach ($sorts as $key => $order) {
        $property = $this->fieldDefinitions
          ->get($key)
          ->getProperty();
        $value1 = $interpreter1
          ->getWrapper()
          ->get($property);
        $value2 = $interpreter2
          ->getWrapper()
          ->get($property);
        if ($value1 == $value2) {
          continue;
        }
        return ($order == 'DESC' ? -1 : 1) * strcmp($value1, $value2);
      }
      return 0;
    });
  }
  return $variables;
}