You are here

protected function DataProviderPlug::applySort in RESTful 7.2

Sorts plugins on the list based on the request options.

Parameters

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

Return value

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

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

File

src/Plugin/resource/DataProvider/DataProviderPlug.php, line 200
Contains \Drupal\restful\Plugin\resource\DataProvider\DataProviderPlug.

Class

DataProviderPlug
Class DataProviderPlug.

Namespace

Drupal\restful\Plugin\resource\DataProvider

Code

protected function applySort(array $plugins) {
  if ($sorts = $this
    ->parseRequestForListSort()) {
    uasort($plugins, function ($plugin1, $plugin2) use ($sorts) {
      $interpreter1 = new DataInterpreterPlug($this
        ->getAccount(), new PluginWrapper($plugin1));
      $interpreter2 = new DataInterpreterPlug($this
        ->getAccount(), new PluginWrapper($plugin2));
      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 $plugins;
}