Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Isbn.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
34 {
35  const AUTO = 'auto';
36  const ISBN10 = '10';
37  const ISBN13 = '13';
38  const INVALID = 'isbnInvalid';
39  const NO_ISBN = 'isbnNoIsbn';
40 
46  protected $_messageTemplates = array(
47  self::INVALID => "Invalid type given. String or integer expected",
48  self::NO_ISBN => "'%value%' is not a valid ISBN number",
49  );
50 
56  protected $_type = self::AUTO;
57 
63  protected $_separator = '';
64 
71  public function __construct($options = array())
72  {
73  // prepare options
74  if ($options instanceof Zend_Config) {
75  $options = $options->toArray();
76  }
77  if (!is_array($options)) {
81  #require_once 'Zend/Validate/Exception.php';
82  throw new Zend_Validate_Exception('Invalid options provided.');
83  }
84 
85  // set type
86  if (array_key_exists('type', $options)) {
87  $this->setType($options['type']);
88  }
89 
90  // set separator
91  if (array_key_exists('separator', $options)) {
92  $this->setSeparator($options['separator']);
93  }
94  }
95 
101  protected function _detectFormat()
102  {
103  // prepare separator and pattern list
104  $sep = quotemeta($this->_separator);
105  $patterns = array();
106  $lengths = array();
107 
108  // check for ISBN-10
109  if ($this->_type == self::ISBN10 || $this->_type == self::AUTO) {
110  if (empty($sep)) {
111  $pattern = '/^[0-9]{9}[0-9X]{1}$/';
112  $length = 10;
113  } else {
114  $pattern = "/^[0-9]{1,7}[{$sep}]{1}[0-9]{1,7}[{$sep}]{1}[0-9]{1,7}[{$sep}]{1}[0-9X]{1}$/";
115  $length = 13;
116  }
117 
118  $patterns[$pattern] = self::ISBN10;
119  $lengths[$pattern] = $length;
120  }
121 
122  // check for ISBN-13
123  if ($this->_type == self::ISBN13 || $this->_type == self::AUTO) {
124  if (empty($sep)) {
125  $pattern = '/^[0-9]{13}$/';
126  $length = 13;
127  } else {
128  $pattern = "/^[0-9]{1,9}[{$sep}]{1}[0-9]{1,5}[{$sep}]{1}[0-9]{1,9}[{$sep}]{1}[0-9]{1,9}[{$sep}]{1}[0-9]{1}$/";
129  $length = 17;
130  }
131 
132  $patterns[$pattern] = self::ISBN13;
133  $lengths[$pattern] = $length;
134  }
135 
136  // check pattern list
137  foreach ($patterns as $pattern => $type) {
138  if ((strlen($this->_value) == $lengths[$pattern]) && preg_match($pattern, $this->_value)) {
139  return $type;
140  }
141  }
142 
143  return null;
144  }
145 
154  public function isValid($value)
155  {
156  if (!is_string($value) && !is_int($value)) {
157  $this->_error(self::INVALID);
158  return false;
159  }
160 
161  $value = (string) $value;
162  $this->_setValue($value);
163 
164  switch ($this->_detectFormat()) {
165  case self::ISBN10:
166  // sum
167  $isbn10 = str_replace($this->_separator, '', $value);
168  $sum = 0;
169  for ($i = 0; $i < 9; $i++) {
170  $sum += (10 - $i) * $isbn10{$i};
171  }
172 
173  // checksum
174  $checksum = 11 - ($sum % 11);
175  if ($checksum == 11) {
176  $checksum = '0';
177  } elseif ($checksum == 10) {
178  $checksum = 'X';
179  }
180  break;
181 
182  case self::ISBN13:
183  // sum
184  $isbn13 = str_replace($this->_separator, '', $value);
185  $sum = 0;
186  for ($i = 0; $i < 12; $i++) {
187  if ($i % 2 == 0) {
188  $sum += $isbn13{$i};
189  } else {
190  $sum += 3 * $isbn13{$i};
191  }
192  }
193  // checksum
194  $checksum = 10 - ($sum % 10);
195  if ($checksum == 10) {
196  $checksum = '0';
197  }
198  break;
199 
200  default:
201  $this->_error(self::NO_ISBN);
202  return false;
203  }
204 
205  // validate
206  if (substr($this->_value, -1) != $checksum) {
207  $this->_error(self::NO_ISBN);
208  return false;
209  }
210  return true;
211  }
212 
222  public function setSeparator($separator)
223  {
224  // check separator
225  if (!in_array($separator, array('-', ' ', ''))) {
229  #require_once 'Zend/Validate/Exception.php';
230  throw new Zend_Validate_Exception('Invalid ISBN separator.');
231  }
232 
233  $this->_separator = $separator;
234  return $this;
235  }
236 
242  public function getSeparator()
243  {
244  return $this->_separator;
245  }
246 
254  public function setType($type)
255  {
256  // check type
257  if (!in_array($type, array(self::AUTO, self::ISBN10, self::ISBN13))) {
261  #require_once 'Zend/Validate/Exception.php';
262  throw new Zend_Validate_Exception('Invalid ISBN type');
263  }
264 
265  $this->_type = $type;
266  return $this;
267  }
268 
274  public function getType()
275  {
276  return $this->_type;
277  }
278 }
const NO_ISBN
Definition: Isbn.php:39
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$pattern
Definition: website.php:22
const ISBN13
Definition: Isbn.php:37
const INVALID
Definition: Isbn.php:38
_error($messageKey, $value=null)
Definition: Abstract.php:284
$type
Definition: item.phtml:13
isValid($value)
Definition: Isbn.php:154
$value
Definition: gender.phtml:16
const ISBN10
Definition: Isbn.php:36
setSeparator($separator)
Definition: Isbn.php:222
setType($type)
Definition: Isbn.php:254
$i
Definition: gallery.phtml:31
__construct($options=array())
Definition: Isbn.php:71