You are here

class PriceModifierConstraintValidator 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
  2. 3.x src/Plugin/Validation/Constraint/PriceModifierConstraintValidator.php \Drupal\price\Plugin\Validation\Constraint\PriceModifierConstraintValidator

Validates the price modifier constraint.

Hierarchy

Expanded class hierarchy of PriceModifierConstraintValidator

File

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

Namespace

Drupal\price\Plugin\Validation\Constraint
View source
class PriceModifierConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new PriceModifierConstraintValidator object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'));
  }

  /**
   * {@inheritdoc}
   */
  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();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PriceModifierConstraintValidator::$entityTypeManager protected property The entity type manager.
PriceModifierConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
PriceModifierConstraintValidator::validate public function
PriceModifierConstraintValidator::__construct public function Constructs a new PriceModifierConstraintValidator object.