You are here

class CdnDomainConstraintValidator in CDN 8.3

CDN domain constraint validator.

Hierarchy

Expanded class hierarchy of CdnDomainConstraintValidator

1 file declares its use of CdnDomainConstraintValidator
CdnDomainConstraintValidatorTest.php in tests/src/Unit/Plugin/Validation/Constraint/CdnDomainConstraintValidatorTest.php

File

src/Plugin/Validation/Constraint/CdnDomainConstraintValidator.php, line 14

Namespace

Drupal\cdn\Plugin\Validation\Constraint
View source
class CdnDomainConstraintValidator extends ConstraintValidator {

  /**
   * {@inheritdoc}
   */
  public function validate($domain, Constraint $constraint) {
    if (!$constraint instanceof CdnDomainConstraint) {
      throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\CdnDomain');
    }
    if ($domain === NULL) {
      return;
    }
    if (!static::isValidCdnDomain($domain)) {
      $this->context
        ->buildViolation($constraint->message)
        ->setParameter('%domain', $domain)
        ->setInvalidValue($domain)
        ->addViolation();
    }
  }

  /**
   * Validates the given CDN domain.
   *
   * @param string $domain
   *   A domain as expected by the CDN module: an "authority" in RFC3986.
   *
   * @return bool
   */
  protected static function isValidCdnDomain(string $domain) : bool {

    // Add a scheme so that we have a parseable URL.
    $url = 'https://' . $domain;
    $components = parse_url($url);
    $forbidden_components = [
      'path',
      'query',
      'fragment',
    ];
    return $components === FALSE ? FALSE : empty(array_intersect($forbidden_components, array_keys($components)));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CdnDomainConstraintValidator::isValidCdnDomain protected static function Validates the given CDN domain.
CdnDomainConstraintValidator::validate public function Checks if the passed value is valid.