Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Zend_Validate_Isbn Class Reference
Inheritance diagram for Zend_Validate_Isbn:
Zend_Validate_Abstract Zend_Validate_Interface

Public Member Functions

 __construct ($options=array())
 
 isValid ($value)
 
 setSeparator ($separator)
 
 getSeparator ()
 
 setType ($type)
 
 getType ()
 
- Public Member Functions inherited from Zend_Validate_Abstract
 getMessages ()
 
 getMessageVariables ()
 
 getMessageTemplates ()
 
 setMessage ($messageString, $messageKey=null)
 
 setMessages (array $messages)
 
 __get ($property)
 
 getErrors ()
 
 setObscureValue ($flag)
 
 getObscureValue ()
 
 setTranslator ($translator=null)
 
 getTranslator ()
 
 hasTranslator ()
 
 setDisableTranslator ($flag)
 
 translatorIsDisabled ()
 

Data Fields

const AUTO = 'auto'
 
const ISBN10 = '10'
 
const ISBN13 = '13'
 
const INVALID = 'isbnInvalid'
 
const NO_ISBN = 'isbnNoIsbn'
 

Protected Member Functions

 _detectFormat ()
 
- Protected Member Functions inherited from Zend_Validate_Abstract
 _createMessage ($messageKey, $value)
 
 _implodeRecursive (array $pieces)
 
 _error ($messageKey, $value=null)
 
 _setValue ($value)
 

Protected Attributes

 $_messageTemplates
 
 $_type = self::AUTO
 
 $_separator = ''
 
- Protected Attributes inherited from Zend_Validate_Abstract
 $_value
 
 $_messageVariables = array()
 
 $_messageTemplates = array()
 
 $_messages = array()
 
 $_obscureValue = false
 
 $_errors = array()
 
 $_translator
 
 $_translatorDisabled = false
 

Additional Inherited Members

- Static Public Member Functions inherited from Zend_Validate_Abstract
static setDefaultTranslator ($translator=null)
 
static getDefaultTranslator ()
 
static hasDefaultTranslator ()
 
static getMessageLength ()
 
static setMessageLength ($length=-1)
 
- Static Protected Attributes inherited from Zend_Validate_Abstract
static $_defaultTranslator
 
static $_messageLength = -1
 

Detailed Description

Definition at line 33 of file Isbn.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $options = array())

Set up options.

Parameters
Zend_Config | array$options
Exceptions
Zend_Validate_ExceptionWhen $options is not valid
See also
Zend_Validate_Exception

Definition at line 71 of file Isbn.php.

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  }
setSeparator($separator)
Definition: Isbn.php:222
setType($type)
Definition: Isbn.php:254

Member Function Documentation

◆ _detectFormat()

_detectFormat ( )
protected

Detect input format.

Returns
string

Definition at line 101 of file Isbn.php.

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  }
$pattern
Definition: website.php:22
const ISBN13
Definition: Isbn.php:37
$type
Definition: item.phtml:13
const ISBN10
Definition: Isbn.php:36

◆ getSeparator()

getSeparator ( )

Get separator characters.

Returns
string

Definition at line 242 of file Isbn.php.

243  {
244  return $this->_separator;
245  }

◆ getType()

getType ( )

Get allowed ISBN type.

Returns
string

Definition at line 274 of file Isbn.php.

275  {
276  return $this->_type;
277  }

◆ isValid()

isValid (   $value)

Defined by Zend_Validate_Interface.

Returns true if and only if $value is a valid ISBN.

Parameters
string$value
Returns
boolean

Implements Zend_Validate_Interface.

Definition at line 154 of file Isbn.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
const ISBN13
Definition: Isbn.php:37
_error($messageKey, $value=null)
Definition: Abstract.php:284
$value
Definition: gender.phtml:16
const ISBN10
Definition: Isbn.php:36
$i
Definition: gallery.phtml:31

◆ setSeparator()

setSeparator (   $separator)

Set separator characters.

It is allowed only empty string, hyphen and space.

Parameters
string$separator
Exceptions
Zend_Validate_ExceptionWhen $separator is not valid
Returns
Zend_Validate_Isbn Provides a fluent interface
See also
Zend_Validate_Exception

Definition at line 222 of file Isbn.php.

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  }

◆ setType()

setType (   $type)

Set allowed ISBN type.

Parameters
string$type
Exceptions
Zend_Validate_ExceptionWhen $type is not valid
Returns
Zend_Validate_Isbn Provides a fluent interface
See also
Zend_Validate_Exception

Definition at line 254 of file Isbn.php.

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  }
$type
Definition: item.phtml:13

Field Documentation

◆ $_messageTemplates

$_messageTemplates
protected
Initial value:
= array(
self::INVALID => "Invalid type given. String or integer expected",
self::NO_ISBN => "'%value%' is not a valid ISBN number",
)

Definition at line 46 of file Isbn.php.

◆ $_separator

$_separator = ''
protected

Definition at line 63 of file Isbn.php.

◆ $_type

$_type = self::AUTO
protected

Definition at line 56 of file Isbn.php.

◆ AUTO

const AUTO = 'auto'

Definition at line 35 of file Isbn.php.

◆ INVALID

const INVALID = 'isbnInvalid'

Definition at line 38 of file Isbn.php.

◆ ISBN10

const ISBN10 = '10'

Definition at line 36 of file Isbn.php.

◆ ISBN13

const ISBN13 = '13'

Definition at line 37 of file Isbn.php.

◆ NO_ISBN

const NO_ISBN = 'isbnNoIsbn'

Definition at line 39 of file Isbn.php.


The documentation for this class was generated from the following file: