Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
15394 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Validator;
6
 
7
use Laminas\Validator\AbstractValidator;
8
use Laminas\Stdlib\ArrayUtils;
9
use Traversable;
10
use Laminas\Validator\Exception\InvalidArgumentException;
11
 
12
 
13
class DifferentThanOtherField extends AbstractValidator
14
{
15
    /**
16
     * Error codes
17
     *
18
     * @const string
19
     */
20
    public const SAME       = 'notSame';
21
    public const MISSING_TOKEN = 'missingToken';
22
 
23
    /**
24
     * Error messages
25
     *
26
     * @var array
27
     */
28
    protected $messageTemplates = [
29
        self::SAME       => 'The two given tokens  match',
30
        self::MISSING_TOKEN => 'No token was provided to match against',
31
    ];
32
 
33
    /** @var array */
34
    protected $messageVariables = [
35
        'token' => 'tokenString',
36
    ];
37
 
38
    /**
39
     * Original token against which to validate
40
     *
41
     * @var null|string
42
     */
43
    protected $tokenString;
44
 
45
    /** @var null|string */
46
    protected $token;
47
 
48
    /** @var bool */
49
    protected $strict = true;
50
 
51
    /** @var bool */
52
    protected $literal = false;
53
 
54
    /**
55
     * Sets validator options
56
     *
57
     * @param  mixed $token
58
     */
59
    public function __construct($token = null)
60
    {
61
        if ($token instanceof Traversable) {
62
            $token = ArrayUtils::iteratorToArray($token);
63
        }
64
 
65
        if (is_array($token) && array_key_exists('token', $token)) {
66
            if (array_key_exists('strict', $token)) {
67
                $this->setStrict($token['strict']);
68
            }
69
 
70
            if (array_key_exists('literal', $token)) {
71
                $this->setLiteral($token['literal']);
72
            }
73
 
74
            $this->setToken($token['token']);
75
        } elseif (null !== $token) {
76
            $this->setToken($token);
77
        }
78
 
79
        parent::__construct(is_array($token) ? $token : null);
80
    }
81
 
82
    /**
83
     * Retrieve token
84
     *
85
     * @return mixed
86
     */
87
    public function getToken()
88
    {
89
        return $this->token;
90
    }
91
 
92
    /**
93
     * Set token against which to compare
94
     *
95
     * @param  mixed $token
96
     * @return $this
97
     */
98
    public function setToken($token)
99
    {
100
        $this->tokenString = is_array($token) ? var_export($token, true) : (string) $token;
101
        $this->token       = $token;
102
        return $this;
103
    }
104
 
105
    /**
106
     * Returns the strict parameter
107
     *
108
     * @return bool
109
     */
110
    public function getStrict()
111
    {
112
        return $this->strict;
113
    }
114
 
115
    /**
116
     * Sets the strict parameter
117
     *
118
     * @param  bool $strict
119
     * @return $this
120
     */
121
    public function setStrict($strict)
122
    {
123
        $this->strict = (bool) $strict;
124
        return $this;
125
    }
126
 
127
    /**
128
     * Returns the literal parameter
129
     *
130
     * @return bool
131
     */
132
    public function getLiteral()
133
    {
134
        return $this->literal;
135
    }
136
 
137
    /**
138
     * Sets the literal parameter
139
     *
140
     * @param  bool $literal
141
     * @return $this
142
     */
143
    public function setLiteral($literal)
144
    {
145
        $this->literal = (bool) $literal;
146
        return $this;
147
    }
148
 
149
    /**
150
     * Returns true if and only if a token has been set and the provided value
151
     * matches that token.
152
     *
153
     * @param  mixed $value
154
     * @param  null|array|\ArrayAccess $context
155
     * @throws InvalidArgumentException:: If context is not array or ArrayObject.
156
     * @return bool
157
     */
158
    public function isValid($value, $context = null)
159
    {
160
        $this->setValue($value);
161
 
162
        $token = $this->getToken();
163
 
164
        if (! $this->getLiteral() && $context !== null) {
165
            if (! is_array($context) && ! $context instanceof \ArrayAccess) {
166
                throw new InvalidArgumentException(sprintf(
167
                    'Context passed to %s must be array, ArrayObject or null; received "%s"',
168
                    __METHOD__,
169
                    is_object($context) ? get_class($context) : gettype($context)
170
                    ));
171
            }
172
 
173
            if (is_array($token)) {
174
                while (is_array($token)) {
175
                    $key = key($token);
176
                    if (! isset($context[$key])) {
177
                        break;
178
                    }
179
                    $context = $context[$key];
180
                    $token   = $token[$key];
181
                }
182
            }
183
 
184
            // if $token is an array it means the above loop didn't went all the way down to the leaf,
185
            // so the $token structure doesn't match the $context structure
186
            if (
187
                is_array($token)
188
                || (! is_int($token) && ! is_string($token))
189
                || ! isset($context[$token])
190
                ) {
191
                    $token = $this->getToken();
192
                } else {
193
                    $token = $context[$token];
194
                }
195
        }
196
 
197
        if ($token === null) {
198
            $this->error(self::MISSING_TOKEN);
199
            return false;
200
        }
201
 
202
        $strict = $this->getStrict();
203
        if (
204
            ($strict && ($value === $token))
205
            || (! $strict && ($value == $token))
206
            ) {
207
                $this->error(self::SAME );
208
                return false;
209
            }
210
 
211
            return true;
212
    }
213
}