class PrimitiveTypeConstraintValidator in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidator.php \Drupal\Core\Validation\Plugin\Validation\Constraint\PrimitiveTypeConstraintValidator
Validates the PrimitiveType constraint.
Hierarchy
- class \Symfony\Component\Validator\ConstraintValidator implements ConstraintValidatorInterface
- class \Drupal\Core\Validation\Plugin\Validation\Constraint\PrimitiveTypeConstraintValidator uses TypedDataAwareValidatorTrait
Expanded class hierarchy of PrimitiveTypeConstraintValidator
1 file declares its use of PrimitiveTypeConstraintValidator
- PrimitiveTypeConstraintValidatorTest.php in core/
tests/ Drupal/ Tests/ Core/ Validation/ Plugin/ Validation/ Constraint/ PrimitiveTypeConstraintValidatorTest.php - Contains \Drupal\Tests\Core\Validation\Plugin\Validation\Constraint\PrimitiveTypeConstraintValidatorTest.
File
- core/
lib/ Drupal/ Core/ Validation/ Plugin/ Validation/ Constraint/ PrimitiveTypeConstraintValidator.php, line 26 - Contains \Drupal\Core\Validation\Plugin\Validation\Constraint\PrimitiveTypeConstraintValidator.
Namespace
Drupal\Core\Validation\Plugin\Validation\ConstraintView source
class PrimitiveTypeConstraintValidator extends ConstraintValidator {
use TypedDataAwareValidatorTrait;
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint) {
if (!isset($value)) {
return;
}
$typed_data = $this
->getTypedData();
$valid = TRUE;
if ($typed_data instanceof BinaryInterface && !is_resource($value)) {
$valid = FALSE;
}
if ($typed_data instanceof BooleanInterface && !(is_bool($value) || $value === 0 || $value === '0' || $value === 1 || $value == '1')) {
$valid = FALSE;
}
if ($typed_data instanceof FloatInterface && filter_var($value, FILTER_VALIDATE_FLOAT) === FALSE) {
$valid = FALSE;
}
if ($typed_data instanceof IntegerInterface && filter_var($value, FILTER_VALIDATE_INT) === FALSE) {
$valid = FALSE;
}
if ($typed_data instanceof StringInterface && !is_scalar($value) && !$value instanceof MarkupInterface) {
$valid = FALSE;
}
// Ensure that URIs comply with http://tools.ietf.org/html/rfc3986, which
// requires:
// - That it is well formed (parse_url() returns FALSE if not).
// - That it contains a scheme (parse_url(, PHP_URL_SCHEME) returns NULL if
// not).
if ($typed_data instanceof UriInterface && in_array(parse_url($value, PHP_URL_SCHEME), [
NULL,
FALSE,
], TRUE)) {
$valid = FALSE;
}
// @todo: Move those to separate constraint validators.
try {
if ($typed_data instanceof DateTimeInterface && $typed_data
->getDateTime() && $typed_data
->getDateTime()
->hasErrors()) {
$valid = FALSE;
}
if ($typed_data instanceof DurationInterface && $typed_data
->getDuration() && !$typed_data
->getDuration() instanceof \DateInterval) {
$valid = FALSE;
}
} catch (\Exception $e) {
// Invalid durations or dates might throw exceptions.
$valid = FALSE;
}
if (!$valid) {
// @todo: Provide a good violation message for each problem.
$this->context
->addViolation($constraint->message, array(
'%value' => is_object($value) ? get_class($value) : (is_array($value) ? 'Array' : (string) $value),
));
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConstraintValidator:: |
protected | property | 3 | |
ConstraintValidator:: |
protected | function | Wrapper for {@link ExecutionContextInterface::buildViolation} that supports the 2.4 context API. | |
ConstraintValidator:: |
protected | function | Wrapper for {@link ExecutionContextInterface::buildViolation} that supports the 2.4 context API. | |
ConstraintValidator:: |
protected | function | Returns a string representation of the type of the value. | |
ConstraintValidator:: |
protected | function | Returns a string representation of the value. | |
ConstraintValidator:: |
protected | function | Returns a string representation of a list of values. | |
ConstraintValidator:: |
public | function |
Initializes the constraint validator. Overrides ConstraintValidatorInterface:: |
1 |
ConstraintValidator:: |
constant | Whether to cast objects with a "__toString()" method to strings. | ||
ConstraintValidator:: |
constant | Whether to format {@link \DateTime} objects as RFC-3339 dates ("Y-m-d H:i:s"). | ||
PrimitiveTypeConstraintValidator:: |
public | function |
Checks if the passed value is valid. Overrides ConstraintValidatorInterface:: |
|
TypedDataAwareValidatorTrait:: |
public | function | Gets the typed data object for the validated value. |