You are here

public static function ConfigActionsTransform::parseWildcards in Config Actions 8

Test a string against a variable wildcard pattern.

Parameters

string $pattern: A string pattern with @var@ wildcards, and @var__value@ replacements

string $source: The source string to test.

array $data: Optional list of property values to test when using @property__value@ in pattern

Return value

array Returns a list of matching vars and values, or returns empty array if source doesn't match the pattern.

3 calls to ConfigActionsTransform::parseWildcards()
ConfigActionsPluginBase::execute in src/ConfigActionsPluginBase.php
Execute the action for this plugin.
ConfigActionsPluginBase::setOptions in src/ConfigActionsPluginBase.php
Process an Options array to set various internal variable defaults.
ConfigActionsTransformTest::testParseWildcards in tests/src/Unit/ConfigActionsTransformTest.php
@covers ::parseWildcards

File

src/ConfigActionsTransform.php, line 258

Class

ConfigActionsTransform
Perform transformations on data needed for config_actions plugins

Namespace

Drupal\config_actions

Code

public static function parseWildcards($pattern, $source, $data = []) {
  $result = [];

  // Pattern example: "@name@.@type@"
  // Get the variables from the pattern.
  $vars = [];
  preg_match_all('/\\@[a-zA-Z0-9_\\-]+?\\@/', $pattern, $matches);
  $matches = !empty($matches) ? $matches[0] : [];
  foreach ($matches as $match) {

    // Test property__value validation in supplied replacements
    if (strpos($match, '__') > 0) {
      list($property, $value) = explode('__', str_replace('@', '', $match));

      // If $data is provided AND
      // either property isn't found, or property is a string and doesn't match
      // Then fail test.
      if (!empty($data) && (!isset($data[$property]) || is_string($data[$property]) && $data[$property] !== $value)) {

        // Found a @property__value@ validation that failed, so no match
        return [];
      }

      // Remove the validation check from pattern but set variable value
      $pattern = str_replace($match, '', $pattern);
      $result['@' . $property . '@'] = $value;
      continue;
    }
    $vars[] = $match;
  }

  // $vars is array of variable names: [ '@name@', '@type@' ]
  // Convert pattern to regex.
  // First escape special chars.
  $regEx = preg_replace('/([^a-zA-Z0-9_\\@\\-])/', '\\\\$1', $pattern);

  // Next, replace variables with regex wildcards.
  $regEx = '/^' . preg_replace('/\\@[a-zA-Z0-9_\\-]+?\\@/', '([a-zA-Z0-9_\\-]+?)', $regEx) . '$/';

  // $regEx is like /^([a-zA-Z0-9_]+?)\.([a-zA-Z0-9_]+?)$/
  // Run the regex to extract the data corresponding to the variables.
  preg_match($regEx, $source, $matches);

  // $matches should now contain array like: [ 'node.blog', 'node', 'blog' ]
  // Loop through matches and assign values to variables
  if (!empty($matches) && count($matches) == count($vars) + 1) {
    array_shift($matches);
    foreach ($matches as $key => $match) {
      $result[$vars[$key]] = $match;
    }
  }
  else {

    // Didn't match all the vars.
    $result = [];
  }
  return $result;
}