Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Barcode.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
30 #require_once 'Zend/Loader.php';
31 
39 {
40  const INVALID = 'barcodeInvalid';
41  const FAILED = 'barcodeFailed';
42  const INVALID_CHARS = 'barcodeInvalidChars';
43  const INVALID_LENGTH = 'barcodeInvalidLength';
44 
45  protected $_messageTemplates = array(
46  self::FAILED => "'%value%' failed checksum validation",
47  self::INVALID_CHARS => "'%value%' contains invalid characters",
48  self::INVALID_LENGTH => "'%value%' should have a length of %length% characters",
49  self::INVALID => "Invalid type given. String expected",
50  );
51 
57  protected $_messageVariables = array(
58  'length' => '_length'
59  );
60 
66  protected $_length;
67 
73  protected $_adapter;
74 
82  public function __construct($adapter)
83  {
84  if ($adapter instanceof Zend_Config) {
85  $adapter = $adapter->toArray();
86  }
87 
88  $options = null;
89  $checksum = null;
90  if (is_array($adapter)) {
91  if (array_key_exists('options', $adapter)) {
92  $options = $adapter['options'];
93  }
94 
95  if (array_key_exists('checksum', $adapter)) {
96  $checksum = $adapter['checksum'];
97  }
98 
99  if (array_key_exists('adapter', $adapter)) {
100  $adapter = $adapter['adapter'];
101  } else {
102  #require_once 'Zend/Validate/Exception.php';
103  throw new Zend_Validate_Exception("Missing option 'adapter'");
104  }
105  }
106 
107  $this->setAdapter($adapter, $options);
108  if ($checksum !== null) {
109  $this->setChecksum($checksum);
110  }
111  }
112 
118  public function getAdapter()
119  {
120  return $this->_adapter;
121  }
122 
131  public function setAdapter($adapter, $options = null)
132  {
133  $adapter = ucfirst(strtolower($adapter));
134  #require_once 'Zend/Loader.php';
135  if (Zend_Loader::isReadable('Zend/Validate/Barcode/' . $adapter. '.php')) {
136  $adapter = 'Zend_Validate_Barcode_' . $adapter;
137  }
138 
139  if (!class_exists($adapter)) {
141  }
142 
143  $this->_adapter = new $adapter($options);
144  if (!$this->_adapter instanceof Zend_Validate_Barcode_AdapterInterface) {
145  #require_once 'Zend/Validate/Exception.php';
146  throw new Zend_Validate_Exception(
147  "Adapter " . $adapter . " does not implement Zend_Validate_Barcode_AdapterInterface"
148  );
149  }
150 
151  return $this;
152  }
153 
159  public function getChecksum()
160  {
161  return $this->getAdapter()->getCheck();
162  }
163 
170  public function setChecksum($checksum)
171  {
172  $this->getAdapter()->setCheck($checksum);
173  return $this;
174  }
175 
184  public function isValid($value)
185  {
186  if (!is_string($value)) {
187  $this->_error(self::INVALID);
188  return false;
189  }
190 
191  $this->_setValue($value);
192  $adapter = $this->getAdapter();
193  $this->_length = $adapter->getLength();
194  $result = $adapter->checkLength($value);
195  if (!$result) {
196  if (is_array($this->_length)) {
197  $temp = $this->_length;
198  $this->_length = "";
199  foreach($temp as $length) {
200  $this->_length .= "/";
201  $this->_length .= $length;
202  }
203 
204  $this->_length = substr($this->_length, 1);
205  }
206 
207  $this->_error(self::INVALID_LENGTH);
208  return false;
209  }
210 
211  $result = $adapter->checkChars($value);
212  if (!$result) {
213  $this->_error(self::INVALID_CHARS);
214  return false;
215  }
216 
217  if ($this->getChecksum()) {
218  $result = $adapter->checksum($value);
219  if (!$result) {
220  $this->_error(self::FAILED);
221  return false;
222  }
223  }
224 
225  return true;
226  }
227 }
__construct($adapter)
Definition: Barcode.php:82
setAdapter($adapter, $options=null)
Definition: Barcode.php:131
static loadClass($class, $dirs=null)
Definition: Loader.php:52
$adapter
Definition: webapi_user.php:16
_error($messageKey, $value=null)
Definition: Abstract.php:284
static isReadable($filename)
Definition: Loader.php:162
setChecksum($checksum)
Definition: Barcode.php:170
$value
Definition: gender.phtml:16