httpClient = $http_client; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static($container->get('http_client')); } /** * {@inheritdoc} */ 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); } } } }