You are here

public function PriceModifierConstraintValidator::validate in Price 3.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/Validation/Constraint/PriceModifierConstraintValidator.php \Drupal\price\Plugin\Validation\Constraint\PriceModifierConstraintValidator::validate()
  2. 3.x src/Plugin/Validation/Constraint/PriceModifierConstraintValidator.php \Drupal\price\Plugin\Validation\Constraint\PriceModifierConstraintValidator::validate()

File

src/Plugin/Validation/Constraint/PriceModifierConstraintValidator.php, line 45

Class

PriceModifierConstraintValidator
Validates the price modifier constraint.

Namespace

Drupal\price\Plugin\Validation\Constraint

Code

public function validate($value, Constraint $constraint) {

  //    if (!($value instanceof PriceItem)) {
  //      throw new UnexpectedTypeException($value, PriceItem::class);
  //    }
  $price_modified_item = $value;
  $modifier = $price_modified_item
    ->get('modifier')
    ->getValue();
  if ($modifier === NULL || $modifier === '') {
    return;
  }
  $modifiers = $this->entityTypeManager
    ->getStorage('price_modifier')
    ->loadMultiple();
  if (!isset($modifiers[$modifier])) {
    $this->context
      ->buildViolation($constraint->invalidMessage)
      ->atPath('modifier')
      ->setParameter('%value', $this
      ->formatValue($modifier))
      ->addViolation();
    return;
  }
  $available_modifiers = $constraint->availableModifiers;
  if (!empty($available_modifiers) && !in_array($modifier, $available_modifiers)) {
    $this->context
      ->buildViolation($constraint->notAvailableMessage)
      ->atPath('modifier')
      ->setParameter('%value', $this
      ->formatValue($modifier))
      ->addViolation();
  }
}