class RulesConditionalCase in Conditional Rules 8
Same name and namespace in other branches
- 7 includes/rules_conditional.plugin.inc \RulesConditionalCase
Switch case.
Hierarchy
- class \RulesConditionalElement extends \RulesActionContainer implements \RulesActionInterface
- class \RulesConditionalCase
Expanded class hierarchy of RulesConditionalCase
1 string reference to 'RulesConditionalCase'
- rules_conditional_rules_plugin_info in ./
rules_conditional.rules.inc - Implements hook_rules_plugin_info().
File
- includes/
rules_conditional.plugin.inc, line 240 - Rules plugin implementation.
View source
class RulesConditionalCase extends RulesConditionalElement {
protected $itemName = 'case';
/**
* @var RulesPlugin
*/
protected $condition;
/**
* Evaluated condition results.
* @var array
*/
protected $conditionResultCache = array();
public function __construct($settings = array(), $fallThrough = FALSE) {
parent::__construct();
if (isset($settings['value'])) {
$this->settings['value'] = $settings['value'];
}
elseif (isset($settings['value:select'])) {
$this->settings['value:select'] = $settings['value:select'];
}
$this->settings += array(
'fall_through' => $fallThrough,
);
}
public function __destruct() {
unset($this->condition);
unset($this->conditionResultCache);
}
public function forceSetUp() {
parent::forceSetUp();
$this
->setUpCondition();
}
public function pluginParameterInfo() {
$parameterInfo = array(
'value' => array(
'type' => '*',
'label' => t('Data value'),
'description' => t('The value to compare the data with.'),
'allow null' => TRUE,
),
'fall_through' => array(
'type' => 'boolean',
'label' => t('Fall through'),
'description' => t('Fall through to next case when complete. If this option is checked, the next case is automatically executed (regardless of the case value) when this case is finished. If not, the switch will terminate when the case is finished.'),
'optional' => TRUE,
'default value' => FALSE,
'restriction' => 'input',
),
);
// Derive parameter info from switch variable selector.
$dataSelector = isset($this->parent->settings['data:select']) ? $this->parent->settings['data:select'] : NULL;
if ($wrapper = $this
->applyDataSelector($dataSelector)) {
$parameterInfo['value']['type'] = $wrapper
->type();
}
return $parameterInfo;
}
public function stateVariables($element = NULL) {
$this
->forceSetUp();
if (!isset($element) || $element === $this->condition) {
return parent::stateVariables();
}
else {
// Add assertions from the condition.
$variables = parent::stateVariables($element);
if (isset($this->condition) && ($assertions = $this->condition
->call('variableInfoAssertions'))) {
$variables = RulesData::addMetadataAssertions($variables, $assertions);
}
return $variables;
}
}
protected function setUpCondition() {
if (!isset($this->condition) && isset($this->parent)) {
// Prepare settings for 'data_is' condition.
$settings = array(
'data:select' => $this->parent->settings['data:select'],
'op' => '==',
);
if (isset($this->settings['value:select'])) {
$settings['value:select'] = $this->settings['value:select'];
}
elseif (isset($this->settings['value'])) {
$settings['value'] = $this->settings['value'];
}
else {
// Abort if settings are incomplete.
return;
}
// Set up 'data_is'.
$this->condition = rules_condition('data_is', $settings);
$this->condition->parent = $this;
$this->condition
->processSettings();
}
}
/**
* Returns whether this case should fall through.
*/
public function fallThrough() {
return !empty($this->settings['fall_through']);
}
/**
* Determines whether this branch can be evaluated.
*/
public function canEvaluate(RulesState $state) {
$this
->forceSetUp();
// Check if this element has fallen through.
if ($previous = $this
->getPreviousSibling()) {
/** @var $previous self */
if ($previous instanceof self && $previous
->fallThrough() && $previous
->canEvaluate($state)) {
return TRUE;
}
}
// Evaluate condition for the given state once.
$this->conditionResultCache += array(
'state' => array(),
'result' => array(),
);
if (empty($this->conditionResultCache['state']) || !($cacheKey = array_search($state, $this->conditionResultCache['state'], TRUE))) {
$cacheKey = count($this->conditionResultCache['state']);
$this->conditionResultCache['state'][$cacheKey] = $state;
$this->conditionResultCache['result'][$cacheKey] = $this->condition
->evaluate($state);
}
return !empty($this->conditionResultCache['result'][$cacheKey]);
}
public function resetInternalCache() {
parent::resetInternalCache();
$this->condition = NULL;
$this->conditionResultCache = array();
}
}