class ApiDocFileLinkConstraintValidator in Apigee API Catalog 8.2
Same name and namespace in other branches
- 8 src/Plugin/Validation/Constraint/ApiDocFileLinkConstraintValidator.php \Drupal\apigee_api_catalog\Plugin\Validation\Constraint\ApiDocFileLinkConstraintValidator
Class ApiDocFileLinkConstraintValidator.
Hierarchy
- class \Drupal\apigee_api_catalog\Plugin\Validation\Constraint\ApiDocFileLinkConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator implements ContainerInjectionInterface
Expanded class hierarchy of ApiDocFileLinkConstraintValidator
File
- src/
Plugin/ Validation/ Constraint/ ApiDocFileLinkConstraintValidator.php, line 34
Namespace
Drupal\apigee_api_catalog\Plugin\Validation\ConstraintView source
class ApiDocFileLinkConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
/**
* The HTTP client to fetch the files with.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* ApiDocFileLinkConstraintValidator constructor.
*
* @param \GuzzleHttp\ClientInterface $http_client
* A Guzzle client object.
*/
public function __construct(ClientInterface $http_client) {
$this->httpClient = $http_client;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('http_client'));
}
/**
* {@inheritdoc}
*/
public function validate($items, Constraint $constraint) {
/** @var \Drupal\Core\Field\FieldItemListInterface $items */
if (!isset($items)) {
return;
}
foreach ($items as $item) {
if ($item
->isEmpty()) {
continue;
}
// Try to resolve the given URI to a URL. It may fail if it's scheme-less.
try {
$url = Url::fromUri($item
->getValue()['uri'], [
'absolute' => TRUE,
])
->toString();
} catch (\InvalidArgumentException $e) {
$this->context
->addViolation($constraint->urlParseError, [
'@error' => $e
->getMessage(),
]);
return;
}
try {
$options = [
'allow_redirects' => [
'strict' => TRUE,
],
];
// Perform only a HEAD method to save bandwidth.
/* @var $response \Psr\Http\Message\ResponseInterface */
$response = $this->httpClient
->head($url, $options);
} catch (RequestException $request_exception) {
$this->context
->addViolation($constraint->notValid, [
'%value' => $url,
]);
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ApiDocFileLinkConstraintValidator:: |
protected | property | The HTTP client to fetch the files with. | |
ApiDocFileLinkConstraintValidator:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
ApiDocFileLinkConstraintValidator:: |
public | function | Checks if the passed value is valid. | |
ApiDocFileLinkConstraintValidator:: |
public | function | ApiDocFileLinkConstraintValidator constructor. |