You are here

public static function DataProvider::processFilterInput in RESTful 7.2

Processes the input for a filter and adds the appropriate defaults.

Parameters

mixed $filter: The input value for the filter.

string $public_field: The public name for the filter.

Return value

array The processed filter item with all of the defaults.

Throws

\Drupal\restful\Exception\BadRequestException

Overrides DataProviderInterface::processFilterInput

4 calls to DataProvider::processFilterInput()
CacheDecoratedDataProvider::processFilterInput in src/Plugin/resource/DataProvider/CacheDecoratedDataProvider.php
Processes the input for a filter and adds the appropriate defaults.
DataProvider::parseRequestForListFilter in src/Plugin/resource/DataProvider/DataProvider.php
Filter the query for list.
DataProviderDecorator::processFilterInput in src/Plugin/resource/DataProvider/DataProviderDecorator.php
Processes the input for a filter and adds the appropriate defaults.
ResourceFieldEntity::nestedDottedFilters in src/Plugin/resource/Field/ResourceFieldEntity.php
Process the filter query string for the relevant sub-query.

File

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

Class

DataProvider

Namespace

Drupal\restful\Plugin\resource\DataProvider

Code

public static function processFilterInput($filter, $public_field) {

  // Filtering can be achieved in different ways:
  // 1. filter[foo]=bar
  // 2. filter[foo][0]=bar&filter[foo][1]=baz
  // 3. filter[foo][value]=bar
  // 4. filter[foo][value][0]=bar&filter[foo][value][1]=baz
  if (!is_array($filter)) {

    // Request uses the shorthand form for filter. For example
    // filter[foo]=bar would be converted to filter[foo][value] = bar.
    $filter = array(
      'value' => $filter,
    );
  }
  if (!isset($filter['value'])) {
    throw new BadRequestException(sprintf('Value not present for the "%s" filter. Please check the URL format.', $public_field));
  }
  if (!is_array($filter['value'])) {
    $filter['value'] = array(
      $filter['value'],
    );
  }

  // Add the property.
  $filter['public_field'] = $public_field;

  // Set default operator.
  $filter += array(
    'operator' => array_fill(0, count($filter['value']), '='),
  );
  if (!is_array($filter['operator'])) {
    $filter['operator'] = array(
      $filter['operator'],
    );
  }

  // Make sure that we have the same amount of operators than values.
  $first_operator = strtoupper($filter['operator'][0]);
  if (!in_array($first_operator, array(
    'IN',
    'NOT IN',
    'BETWEEN',
  )) && count($filter['value']) != count($filter['operator'])) {
    throw new BadRequestException('The number of operators and values has to be the same.');
  }

  // Make sure that the BETWEEN operator gets only 2 values.
  if ($first_operator == 'BETWEEN' && count($filter['value']) != 2) {
    throw new BadRequestException('The BETWEEN operator takes exactly 2 values.');
  }
  $filter += array(
    'conjunction' => 'AND',
  );

  // Clean the operator in case it came from the URL.
  // e.g. filter[minor_version][operator][0]=">="
  // str_replace will process all the elements in the array.
  $filter['operator'] = str_replace(array(
    '"',
    "'",
  ), '', $filter['operator']);
  static::isValidOperatorsForFilter($filter['operator']);
  static::isValidConjunctionForFilter($filter['conjunction']);
  return $filter;
}