You are here

protected static function Filter::expand in JSON:API 8.2

Expands any filter parameters using shorthand notation.

Parameters

array $original: The unexpanded filter data.

Return value

array The expanded filter data.

1 call to Filter::expand()
Filter::createFromQueryParameter in src/Query/Filter.php
Creates a Sort object from a query parameter.

File

src/Query/Filter.php, line 175

Class

Filter
Gathers information about the filter parameter.

Namespace

Drupal\jsonapi\Query

Code

protected static function expand(array $original) {
  $expanded = [];
  foreach ($original as $key => $item) {

    // Allow extreme shorthand filters, f.e. `?filter[promote]=1`.
    if (!is_array($item)) {
      $item = [
        EntityCondition::VALUE_KEY => $item,
      ];
    }

    // Throw an exception if the query uses the reserved filter id for the
    // root group.
    if ($key == static::ROOT_ID) {
      $msg = sprintf("'%s' is a reserved filter id.", static::ROOT_ID);
      throw new \UnexpectedValueException($msg);
    }

    // Add a memberOf key to all items.
    if (isset($item[static::CONDITION_KEY][static::MEMBER_KEY])) {
      $item[static::MEMBER_KEY] = $item[static::CONDITION_KEY][static::MEMBER_KEY];
      unset($item[static::CONDITION_KEY][static::MEMBER_KEY]);
    }
    elseif (isset($item[static::GROUP_KEY][static::MEMBER_KEY])) {
      $item[static::MEMBER_KEY] = $item[static::GROUP_KEY][static::MEMBER_KEY];
      unset($item[static::GROUP_KEY][static::MEMBER_KEY]);
    }
    else {
      $item[static::MEMBER_KEY] = static::ROOT_ID;
    }

    // Add the filter id to all items.
    $item['id'] = $key;

    // Expands shorthand filters.
    $expanded[$key] = static::expandItem($key, $item);
  }
  return $expanded;
}