protected function CompareDataWithToken::doEvaluate in Rules Token 1.x
Same name and namespace in other branches
- 8 src/Plugin/Condition/CompareDataWithToken.php \Drupal\rules_token\Plugin\Condition\CompareDataWithToken::doEvaluate()
- 2.x src/Plugin/Condition/CompareDataWithToken.php \Drupal\rules_token\Plugin\Condition\CompareDataWithToken::doEvaluate()
Get values of two tokens and compare it with each other.
Parameters
mixed $data: The data to be compared with $token.
string $operation: Data comparison operation. Typically one of:
- "=="
- "<"
- ">"
- "contains" (for strings or arrays)
- "IN" (for arrays or lists).
string $token: The token to be compared with $data.
mixed $token_entity: The entity from the context used in token.
Return value
bool The evaluation of the condition.
File
- src/
Plugin/ Condition/ CompareDataWithToken.php, line 59
Class
- CompareDataWithToken
- Provides a 'Compare Data with Token' condition.
Namespace
Drupal\rules_token\Plugin\ConditionCode
protected function doEvaluate($data, $operation, $token, $token_entity) {
// Set flag for removing token from the final text if no replacement value
// can be generated.
// For, instance, if a node body is empty then token [node:body] will return
// '[node:body]' string. Setting 'clear' to TRUE prevents such behaviour.
$token_options = [
'clear' => TRUE,
];
// Get the value of the token 1.
if ($token && $token_entity) {
// Extract entity name from a token, for instance if token
// is [node:created] then entity name will be 'node'.
$entity_name = mb_substr($token, 1, strpos($token, ':') - 1);
$token_data = [
$entity_name => $token_entity,
];
$value = \Drupal::token()
->replace($token, $token_data, $token_options);
}
elseif ($token) {
$value = \Drupal::token()
->replace($token, [], $token_options);
}
// The following code is based on the code from the 'DataComparison' action
// of 'Rules' module.
$operation = $operation ? strtolower($operation) : '==';
switch ($operation) {
case '<':
return $data < $value;
case '>':
return $data > $value;
case 'contains':
return is_string($data) && strpos($data, $value) !== FALSE || is_array($data) && in_array($value, $data);
case 'in':
return is_array($value) && in_array($data, $value);
case '==':
default:
// In case both values evaluate to FALSE, further differentiate between
// NULL values and values evaluating to FALSE.
if (!$data && !$value) {
return isset($data) && isset($value) || !isset($data) && !isset($value);
}
return $data == $value;
}
}