function SerializedConstraintValidator::validate

File

core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/SerializedConstraintValidator.php, line 16

Class

SerializedConstraintValidator
Validates the Serialized constraint.

Namespace

Drupal\Core\Validation\Plugin\Validation\Constraint

Code

public function validate($value, Constraint $constraint) : void {
  if (!isset($value)) {
    return;
  }
  if (!is_string($value)) {
    $this->context
      ->addViolation($constraint->wrongTypeMessage, [
      '{type}' => gettype($value),
    ]);
    return;
  }
  // Unserialize will return false if unserializing "false". It will also
  // return false if unserialization fails. Handle this edge case.
  if ('b:0;' === $value) {
    return;
  }
  $unserialized = @unserialize($value, [
    'allowed_classes' => FALSE,
  ]);
  if ($unserialized === FALSE) {
    $this->context
      ->addViolation($constraint->message);
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.