You are here

protected function TextComparison::doEvaluate in Rules 8.3

Evaluate the text comparison.

Parameters

string $text: The supplied text string.

string $operator: Text comparison operator. One of:

  • contains: (default) Evaluate if $text contains $match.
  • starts: Evaluate if $text starts with $match.
  • ends: Evaluate if $text ends with $match.
  • regex: Evaluate if a regular expression in $match matches $text.

Values that do not match one of these operators default to "contains".

string $match: The string to be compared against $text.

Return value

bool The evaluation of the condition.

File

src/Plugin/Condition/TextComparison.php, line 54

Class

TextComparison
Provides a 'Text comparison' condition.

Namespace

Drupal\rules\Plugin\Condition

Code

protected function doEvaluate($text, $operator, $match) {
  $operator = $operator ? $operator : 'contains';
  switch ($operator) {
    case 'starts':
      return strpos($text, $match) === 0;
    case 'ends':
      return strrpos($text, $match) === strlen($text) - strlen($match);
    case 'regex':
      return (bool) preg_match('/' . str_replace('/', '\\/', $match) . '/', $text);
    case 'contains':
    default:

      // Default operator "contains".
      return strpos($text, $match) !== FALSE;
  }
}