class TweetVisibleConstraintValidator in Media entity Twitter 8
Validates the TweetVisible constraint.
Hierarchy
- class \Drupal\media_entity_twitter\Plugin\Validation\Constraint\TweetVisibleConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator implements ContainerInjectionInterface uses EmbedCodeValueTrait
Expanded class hierarchy of TweetVisibleConstraintValidator
1 file declares its use of TweetVisibleConstraintValidator
- ConstraintsTest.php in tests/
src/ Unit/ ConstraintsTest.php
File
- src/
Plugin/ Validation/ Constraint/ TweetVisibleConstraintValidator.php, line 16
Namespace
Drupal\media_entity_twitter\Plugin\Validation\ConstraintView source
class TweetVisibleConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
use EmbedCodeValueTrait;
/**
* The HTTP client to fetch the feed data with.
*
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* Constructs a new TweetVisibleConstraintValidator.
*
* @param \GuzzleHttp\Client $http_client
* The http client service.
*/
public function __construct(Client $http_client) {
$this->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);
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EmbedCodeValueTrait:: |
protected | function | Extracts the raw embed code from input which may or may not be wrapped. | |
TweetVisibleConstraintValidator:: |
protected | property | The HTTP client to fetch the feed data with. | |
TweetVisibleConstraintValidator:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
TweetVisibleConstraintValidator:: |
public | function | Checks if the passed value is valid. | |
TweetVisibleConstraintValidator:: |
public | function | Constructs a new TweetVisibleConstraintValidator. |