Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Regex.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
34 {
35  const INVALID = 'regexInvalid';
36  const NOT_MATCH = 'regexNotMatch';
37  const ERROROUS = 'regexErrorous';
38 
42  protected $_messageTemplates = array(
43  self::INVALID => "Invalid type given. String, integer or float expected",
44  self::NOT_MATCH => "'%value%' does not match against pattern '%pattern%'",
45  self::ERROROUS => "There was an internal error while using the pattern '%pattern%'",
46  );
47 
51  protected $_messageVariables = array(
52  'pattern' => '_pattern'
53  );
54 
60  protected $_pattern;
61 
68  public function __construct($pattern)
69  {
70  if ($pattern instanceof Zend_Config) {
71  $pattern = $pattern->toArray();
72  }
73 
74  if (is_array($pattern)) {
75  if (array_key_exists('pattern', $pattern)) {
76  $pattern = $pattern['pattern'];
77  } else {
78  #require_once 'Zend/Validate/Exception.php';
79  throw new Zend_Validate_Exception("Missing option 'pattern'");
80  }
81  }
82 
83  $this->setPattern($pattern);
84  }
85 
91  public function getPattern()
92  {
93  return $this->_pattern;
94  }
95 
103  public function setPattern($pattern)
104  {
105  $this->_pattern = (string) $pattern;
106  $status = @preg_match($this->_pattern, "Test");
107 
108  if (false === $status) {
109  #require_once 'Zend/Validate/Exception.php';
110  throw new Zend_Validate_Exception("Internal error while using the pattern '$this->_pattern'");
111  }
112 
113  return $this;
114  }
115 
124  public function isValid($value)
125  {
126  if (!is_string($value) && !is_int($value) && !is_float($value)) {
127  $this->_error(self::INVALID);
128  return false;
129  }
130 
131  $this->_setValue($value);
132 
133  $status = @preg_match($this->_pattern, $value);
134  if (false === $status) {
135  $this->_error(self::ERROROUS);
136  return false;
137  }
138 
139  if (!$status) {
140  $this->_error(self::NOT_MATCH);
141  return false;
142  }
143 
144  return true;
145  }
146 }
$pattern
Definition: website.php:22
setPattern($pattern)
Definition: Regex.php:103
_error($messageKey, $value=null)
Definition: Abstract.php:284
isValid($value)
Definition: Regex.php:124
$value
Definition: gender.phtml:16
$status
Definition: order_status.php:8
__construct($pattern)
Definition: Regex.php:68