public function validateField(Structure $document, $fieldName, array $params)
{
$value = $document->get($fieldName);
// check only if set
if (!$value) {
return;
}
// check if url valid
$isValidUrl = (bool) filter_var($value, FILTER_VALIDATE_URL);
if (!$isValidUrl) {
if (!isset($params['message'])) {
$params['message'] = 'Value of field "' . $fieldName . '" is not valid url in model ' . get_called_class();
}
$document->addError($fieldName, $this->getName(), $params['message']);
return;
}
// ping not required - so url is valid
if (empty($params['ping'])) {
return;
}
// ping required
$dnsRecordExists = dns_get_record(parse_url($value, PHP_URL_HOST));
// network not allowed
if ($dnsRecordExists === false) {
throw new \RuntimeException('Error getting DNS record to validated url');
}
// empty array - host not found
if (is_array($dnsRecordExists) && empty($dnsRecordExists)) {
if (!isset($params['message'])) {
$params['message'] = 'Value of field "' . $fieldName . '" is valid url but host is unreachable in model ' . get_called_class();
}
$document->addError($fieldName, $this->getName(), $params['message']);
return;
}
if ($this->isUrlAccessible($value)) {
return;
}
if (!isset($params['message'])) {
$params['message'] = 'Value of field "' . $fieldName . '" is valid url but page not found ' . get_called_class();
}
$document->addError($fieldName, $this->getName(), $params['message']);
}