Condition.php in Business Rules 8
File
src/Entity/Condition.php
View source
<?php
namespace Drupal\business_rules\Entity;
use Drupal\business_rules\BusinessRulesItemObject;
use Drupal\business_rules\ConditionInterface;
use Drupal\business_rules\Events\BusinessRulesEvent;
class Condition extends BusinessRulesItemBase implements ConditionInterface {
protected $fail_items = [];
protected $reverse;
protected $success_items = [];
public function __construct(array $values, $entity_type = 'business_rules_condition') {
parent::__construct($values, $entity_type);
}
public function getItemManager() {
return \Drupal::getContainer()
->get('plugin.manager.business_rules.condition');
}
public function getBusinessRuleItemType() {
return 'condition';
}
public function getBusinessRuleItemTranslatedType() {
return t('Condition');
}
public function save() {
$action = Action::load($this
->id());
if (!empty($action)) {
$this->id = 'c_' . $this
->id();
}
return parent::save();
}
public function isReverse() {
return (bool) $this->reverse;
}
public function getSuccessItems() {
$success_items = BusinessRulesItemObject::itemsArrayToItemsObject($this->success_items);
return $success_items;
}
public function getFailItems() {
$fail_items = BusinessRulesItemObject::itemsArrayToItemsObject($this->fail_items);
return $fail_items;
}
public function removeSuccessItem(BusinessRulesItemObject $item) {
unset($this->success_items[$item
->getId()]);
}
public function removeFailItem(BusinessRulesItemObject $item) {
unset($this->fail_items[$item
->getId()]);
}
public function filterContextAvailableItems(array $items) {
$entity_type = $this
->getTargetEntityType();
$bundle = $this
->getTargetBundle();
$available_items = [];
foreach ($items as $key => $value) {
if ((($value
->getTargetEntityType() == $entity_type || empty($value
->getTargetEntityType())) && ($value
->getTargetBundle() == $bundle || empty($value
->getTargetBundle())) || !$value
->isContextDependent()) && $this
->id() != $value
->id()) {
$available_items[$key] = $value;
}
}
return $available_items;
}
public function getMaxItemWeight($success = TRUE) {
if ($success) {
$items = $this->success_items;
}
else {
$items = $this->fail_items;
}
$max = -10;
if (is_array($items)) {
foreach ($items as $item) {
if ($max < $item['weight']) {
$max = $item['weight'];
}
}
}
return $max;
}
public function addSuccessItem(BusinessRulesItemObject $item) {
$item_array = $item
->toArray();
$this->success_items[$item
->getId()] = $item_array[$item
->getId()];
}
public function addFailItem(BusinessRulesItemObject $item) {
$item_array = $item
->toArray();
$this->fail_items[$item
->getId()] = $item_array[$item
->getId()];
}
public function process(BusinessRulesEvent $event) {
$condition_type = $this->itemManager
->getDefinition($this
->getType());
$reflection = new \ReflectionClass($condition_type['class']);
$defined_condition = $reflection
->newInstance($condition_type, $condition_type['id'], $condition_type);
$condition = Condition::load($this
->id());
$defined_condition
->processTokens($condition, $event);
return $defined_condition
->process($condition, $event);
}
public function calculateDependencies() {
parent::calculateDependencies();
foreach (array_merge($this
->getSuccessItems(), $this
->getFailItems()) as $item) {
$this
->addDependency('config', $item
->loadEntity()
->getConfigDependencyName());
}
$items = $this
->getSettings('items');
if (is_array($items)) {
foreach (BusinessRulesItemObject::itemsArrayToItemsObject($items) as $item) {
$this
->addDependency('config', $item
->loadEntity()
->getConfigDependencyName());
}
}
return $this;
}
}