You are here

protected function PlaceholderResolver::parseMainPlaceholderPart in Typed Data API enhancements 8

Parses the main placeholder part.

Main placeholder parts look like 'property.property|filter(arg)|filter'.

Parameters

string $main_part: The main placeholder part.

string $placeholder: The full placeholder string.

Return value

array[] An numerically indexed arrays containing:

  • The numerically indexed array of property sub-paths.
  • The numerically indexed array of parsed filter expressions, where each entry is another numerically indexed array containing two items: the the filter id and the array of filter arguments.

Throws

\Drupal\typed_data\Exception\InvalidArgumentException Thrown if in invalid placeholders are to be parsed.

1 call to PlaceholderResolver::parseMainPlaceholderPart()
PlaceholderResolver::resolvePlaceholders in src/PlaceholderResolver.php
Replaces all placeholder tokens in a given string with appropriate values.

File

src/PlaceholderResolver.php, line 126

Class

PlaceholderResolver
Resolver for placeholder tokens based upon typed data.

Namespace

Drupal\typed_data

Code

protected function parseMainPlaceholderPart($main_part, $placeholder) {
  if (!$main_part) {
    return [
      [],
      [],
    ];
  }
  $properties = explode('.', $main_part);
  $last_part = array_pop($properties);
  $filter_expressions = array_filter(explode('|', $last_part));

  // If there is a property, the first part, before the first |, is it.
  // Also be sure to remove potential whitespace after the last property.
  if ($main_part[0] != '|') {
    $properties[] = rtrim(array_shift($filter_expressions));
  }
  $filters = [];
  foreach ($filter_expressions as $expression) {

    // Look for filter arguments.
    $matches = [];
    preg_match_all('/
      ([^\\(]+)
      \\(             # ( - pattern start
       (.+)
      \\)             # ) - pattern end
      /x', $expression, $matches);
    $filter_id = isset($matches[1][0]) ? $matches[1][0] : $expression;

    // Be sure to remove all whitespaces.
    $filter_id = str_replace(' ', '', $filter_id);
    $args = array_map(function ($arg) {

      // Remove surrounding whitespaces and then quotes.
      return trim(trim($arg), "'");
    }, explode(',', isset($matches[2][0]) ? $matches[2][0] : ''));
    $filters[] = [
      $filter_id,
      $args,
    ];
  }
  return [
    $properties,
    $filters,
  ];
}