class CurrencyConstraintValidator in Price 3.0.x
Same name and namespace in other branches
- 8 src/Plugin/Validation/Constraint/CurrencyConstraintValidator.php \Drupal\price\Plugin\Validation\Constraint\CurrencyConstraintValidator
- 3.x src/Plugin/Validation/Constraint/CurrencyConstraintValidator.php \Drupal\price\Plugin\Validation\Constraint\CurrencyConstraintValidator
- 2.0.x src/Plugin/Validation/Constraint/CurrencyConstraintValidator.php \Drupal\price\Plugin\Validation\Constraint\CurrencyConstraintValidator
- 2.x src/Plugin/Validation/Constraint/CurrencyConstraintValidator.php \Drupal\price\Plugin\Validation\Constraint\CurrencyConstraintValidator
Validates the currency constraint.
Hierarchy
- class \Drupal\price\Plugin\Validation\Constraint\CurrencyConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator implements ContainerInjectionInterface
Expanded class hierarchy of CurrencyConstraintValidator
File
- src/
Plugin/ Validation/ Constraint/ CurrencyConstraintValidator.php, line 16
Namespace
Drupal\price\Plugin\Validation\ConstraintView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CurrencyConstraintValidator:: |
protected | property | The entity type manager. | |
CurrencyConstraintValidator:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
CurrencyConstraintValidator:: |
public | function | ||
CurrencyConstraintValidator:: |
public | function | Constructs a new CurrencyConstraintValidator object. |