Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Identical.php
Go to the documentation of this file.
1 <?php
23 #require_once 'Zend/Validate/Abstract.php';
24 
32 {
37  const NOT_SAME = 'notSame';
38  const MISSING_TOKEN = 'missingToken';
39 
44  protected $_messageTemplates = array(
45  self::NOT_SAME => "The two given tokens do not match",
46  self::MISSING_TOKEN => 'No token was provided to match against',
47  );
48 
52  protected $_messageVariables = array(
53  'token' => '_tokenString'
54  );
55 
60  protected $_tokenString;
61  protected $_token;
62  protected $_strict = true;
63 
69  public function __construct($token = null)
70  {
71  if ($token instanceof Zend_Config) {
72  $token = $token->toArray();
73  }
74 
75  if (is_array($token) && array_key_exists('token', $token)) {
76  if (array_key_exists('strict', $token)) {
77  $this->setStrict($token['strict']);
78  }
79 
80  $this->setToken($token['token']);
81  } else if (null !== $token) {
82  $this->setToken($token);
83  }
84  }
85 
91  public function getToken()
92  {
93  return $this->_token;
94  }
95 
102  public function setToken($token)
103  {
104  $this->_tokenString = $token;
105  $this->_token = $token;
106  return $this;
107  }
108 
114  public function getStrict()
115  {
116  return $this->_strict;
117  }
118 
125  public function setStrict($strict)
126  {
127  $this->_strict = (boolean) $strict;
128  return $this;
129  }
130 
141  public function isValid($value, $context = null)
142  {
143  $this->_setValue($value);
144 
145  if (($context !== null) && isset($context) && array_key_exists($this->getToken(), $context)) {
146  $token = $context[$this->getToken()];
147  } else {
148  $token = $this->getToken();
149  }
150 
151  if ($token === null) {
152  $this->_error(self::MISSING_TOKEN);
153  return false;
154  }
155 
156  $strict = $this->getStrict();
157  if (($strict && ($value !== $token)) || (!$strict && ($value != $token))) {
158  $this->_error(self::NOT_SAME);
159  return false;
160  }
161 
162  return true;
163  }
164 }
_error($messageKey, $value=null)
Definition: Abstract.php:284
__construct($token=null)
Definition: Identical.php:69
$value
Definition: gender.phtml:16
isValid($value, $context=null)
Definition: Identical.php:141