Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Validator;
6
 
7
use Laminas\Validator\AbstractValidator;
8
 
9
use Laminas\Stdlib\ArrayUtils;
10
use Traversable;
11
 
12
 
13
class UriConditional extends AbstractValidator
14
{
15
    const INVALID = 'uriInvalid';
16
    const NOT_URI = 'notUri';
17
 
18
    /**
19
     * @var array
20
     */
21
    protected $messageTemplates = [
22
        self::INVALID => 'Invalid type given. String expected',
23
        self::NOT_URI => 'The input does not appear to be a valid Uri',
24
    ];
25
 
26
    /**
27
     *
28
     * @var string
29
     */
30
    private $token;
31
 
32
    /**
33
     *
34
     * @var $mixed
35
     */
36
    private $condition;
37
 
38
 
39
    public function __construct($options = null)
40
    {
41
 
42
        if (! array_key_exists('token', $options)) {
43
            throw new \InvalidArgumentException("Missing option 'token'");
44
        }
45
 
46
        if (! array_key_exists('condition', $options)) {
47
            throw new \InvalidArgumentException("Missing option 'condition'");
48
        }
49
 
50
 
51
        $this->token        =  $options['token'];
52
        $this->condition    =  $options['condition'];
53
 
54
        parent::__construct($options);
55
    }
56
 
57
    public function isValid($value, $context = null)
58
    {
59
        $token = isset($context[$this->token]) ? $context[$this->token] : '';
60
        if($token == $this->condition) {
61
            $result = filter_var($value, FILTER_VALIDATE_URL);
62
            if(!$result) {
63
                $this->error(self::NOT_URI);
64
            }
65
 
66
            return $result;
67
        } else {
68
            return true;
69
        }
70
    }
71
}