final class ConditionGroup in Commerce Core 8.2
Represents a condition group.
Meant to be instantiated directly.
Hierarchy
- class \Drupal\commerce\ConditionGroup
Expanded class hierarchy of ConditionGroup
6 files declare their use of ConditionGroup
- BuyXGetY.php in modules/
promotion/ src/ Plugin/ Commerce/ PromotionOffer/ BuyXGetY.php - CombinationOffer.php in modules/
promotion/ src/ Plugin/ Commerce/ PromotionOffer/ CombinationOffer.php - ConditionGroupTest.php in tests/
src/ Unit/ ConditionGroupTest.php - OrderItemQuantity.php in modules/
promotion/ src/ Plugin/ Commerce/ Condition/ OrderItemQuantity.php - PaymentGateway.php in modules/
payment/ src/ Entity/ PaymentGateway.php
File
- src/
ConditionGroup.php, line 12
Namespace
Drupal\commerceView source
final class ConditionGroup {
/**
* The conditions.
*
* @var \Drupal\commerce\Plugin\Commerce\Condition\ConditionInterface[]
*/
protected $conditions;
/**
* The operator.
*
* Possible values: AND, OR.
*
* @var string
*/
protected $operator;
/**
* Constructs a new ConditionGroup object.
*
* @param \Drupal\commerce\Plugin\Commerce\Condition\ConditionInterface[] $conditions
* The conditions.
* @param string $operator
* The operator. Possible values: AND, OR.
*
* @throws \InvalidArgumentException
* Thrown when an invalid operator is given.
*/
public function __construct(array $conditions, string $operator) {
if (!in_array($operator, [
'AND',
'OR',
])) {
throw new \InvalidArgumentException(sprintf('Invalid operator "%s" given, expecting "AND" or "OR".', $operator));
}
$this->conditions = $conditions;
$this->operator = $operator;
}
/**
* Gets the conditions.
*
* @return \Drupal\commerce\Plugin\Commerce\Condition\ConditionInterface[]
* The conditions.
*/
public function getConditions() : array {
return $this->conditions;
}
/**
* Gets the operator.
*
* @return string
* The operator. Possible values: AND, OR.
*/
public function getOperator() : string {
return $this->operator;
}
/**
* Evaluates the condition group.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity.
*
* @return bool
* TRUE if the condition group has passed, FALSE otherwise.
*/
public function evaluate(EntityInterface $entity) : bool {
if (empty($this->conditions)) {
return TRUE;
}
$boolean = $this->operator == 'AND' ? FALSE : TRUE;
foreach ($this->conditions as $condition) {
if ($condition
->evaluate($entity) == $boolean) {
return $boolean;
}
}
return !$boolean;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConditionGroup:: |
protected | property | The conditions. | |
ConditionGroup:: |
protected | property | The operator. | |
ConditionGroup:: |
public | function | Evaluates the condition group. | |
ConditionGroup:: |
public | function | Gets the conditions. | |
ConditionGroup:: |
public | function | Gets the operator. | |
ConditionGroup:: |
public | function | Constructs a new ConditionGroup object. |