AutorÃa | Ultima modificación | Ver Log |
<?php
declare(strict_types=1);
namespace LeadersLinked\Validator;
use Laminas\Validator\AbstractValidator;
use Laminas\Stdlib\ArrayUtils;
use Traversable;
class UriConditional extends AbstractValidator
{
const INVALID = 'uriInvalid';
const NOT_URI = 'notUri';
/**
* @var array
*/
protected $messageTemplates = [
self::INVALID => 'Invalid type given. String expected',
self::NOT_URI => 'The input does not appear to be a valid Uri',
];
/**
*
* @var string
*/
private $token;
/**
*
* @var $mixed
*/
private $condition;
public function __construct($options = null)
{
if (! array_key_exists('token', $options)) {
throw new \InvalidArgumentException("Missing option 'token'");
}
if (! array_key_exists('condition', $options)) {
throw new \InvalidArgumentException("Missing option 'condition'");
}
$this->token = $options['token'];
$this->condition = $options['condition'];
parent::__construct($options);
}
public function isValid($value, $context = null)
{
$token = isset($context[$this->token]) ? $context[$this->token] : '';
if($token == $this->condition) {
$result = filter_var($value, FILTER_VALIDATE_URL);
if(!$result) {
$this->error(self::NOT_URI);
}
return $result;
} else {
return true;
}
}
}