You are here

class CurrencyConstraintValidator in Price 3.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/Validation/Constraint/CurrencyConstraintValidator.php \Drupal\price\Plugin\Validation\Constraint\CurrencyConstraintValidator
  2. 3.x src/Plugin/Validation/Constraint/CurrencyConstraintValidator.php \Drupal\price\Plugin\Validation\Constraint\CurrencyConstraintValidator
  3. 2.0.x src/Plugin/Validation/Constraint/CurrencyConstraintValidator.php \Drupal\price\Plugin\Validation\Constraint\CurrencyConstraintValidator
  4. 2.x src/Plugin/Validation/Constraint/CurrencyConstraintValidator.php \Drupal\price\Plugin\Validation\Constraint\CurrencyConstraintValidator

Validates the currency constraint.

Hierarchy

Expanded class hierarchy of CurrencyConstraintValidator

File

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

Namespace

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

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

  /**
   * Constructs a new CurrencyConstraintValidator 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_item = $value;
    $currency_code = $price_item
      ->get('currency_code')
      ->getValue();
    if ($currency_code === NULL || $currency_code === '') {
      return;
    }
    $currencies = $this->entityTypeManager
      ->getStorage('price_currency')
      ->loadMultiple();
    if (!isset($currencies[$currency_code])) {
      $this->context
        ->buildViolation($constraint->invalidMessage)
        ->atPath('currency_code')
        ->setParameter('%value', $this
        ->formatValue($currency_code))
        ->addViolation();
      return;
    }
    $available_currencies = $constraint->availableCurrencies;
    if (!empty($available_currencies) && !in_array($currency_code, $available_currencies)) {
      $this->context
        ->buildViolation($constraint->notAvailableMessage)
        ->atPath('currency_code')
        ->setParameter('%value', $this
        ->formatValue($currency_code))
        ->addViolation();
    }
  }

}

Members

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