public function TweetVisibleConstraintValidator::validate in Media entity Twitter 8
File
- src/
Plugin/ Validation/ Constraint/ TweetVisibleConstraintValidator.php, line 47
Class
- TweetVisibleConstraintValidator
- Validates the TweetVisible constraint.
Namespace
Drupal\media_entity_twitter\Plugin\Validation\ConstraintCode
public function validate($value, Constraint $constraint) {
$value = $this
->getEmbedCode($value);
if (!isset($value)) {
return;
}
$matches = [];
foreach (Twitter::$validationRegexp as $pattern => $key) {
if (preg_match($pattern, $value, $item_matches)) {
$matches[] = $item_matches;
}
}
if (empty($matches[0][0])) {
// If there are no matches the URL is not correct, so stop validation.
return;
}
// Fetch content from the given url.
$response = $this->httpClient
->get($matches[0][0], [
'allow_redirects' => FALSE,
]);
if ($response
->getStatusCode() == 302 && ($location = $response
->getHeader('location'))) {
$effective_url_parts = parse_url($location[0]);
if (!empty($effective_url_parts) && isset($effective_url_parts['query']) && $effective_url_parts['query'] == 'protected_redirect=true') {
$this->context
->addViolation($constraint->message);
}
}
}