1 | <?php |
---|
2 | /** |
---|
3 | * Zend Framework |
---|
4 | * |
---|
5 | * LICENSE |
---|
6 | * |
---|
7 | * This source file is subject to the new BSD license that is bundled |
---|
8 | * with this package in the file LICENSE.txt. |
---|
9 | * It is also available through the world-wide-web at this URL: |
---|
10 | * http://framework.zend.com/license/new-bsd |
---|
11 | * If you did not receive a copy of the license and are unable to |
---|
12 | * obtain it through the world-wide-web, please send an email |
---|
13 | * to license@zend.com so we can send you a copy immediately. |
---|
14 | * |
---|
15 | * @category Zend |
---|
16 | * @package Zend_Validate |
---|
17 | * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
---|
18 | * @license http://framework.zend.com/license/new-bsd New BSD License |
---|
19 | * @version $Id: Isbn.php 22668 2010-07-25 14:50:46Z thomas $ |
---|
20 | */ |
---|
21 | |
---|
22 | /** |
---|
23 | * @see Zend_Validate_Abstract |
---|
24 | */ |
---|
25 | require_once 'Zend/Validate/Abstract.php'; |
---|
26 | |
---|
27 | /** |
---|
28 | * @category Zend |
---|
29 | * @package Zend_Validate |
---|
30 | * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
---|
31 | * @license http://framework.zend.com/license/new-bsd New BSD License |
---|
32 | */ |
---|
33 | class Zend_Validate_Isbn extends Zend_Validate_Abstract |
---|
34 | { |
---|
35 | const AUTO = 'auto'; |
---|
36 | const ISBN10 = '10'; |
---|
37 | const ISBN13 = '13'; |
---|
38 | const INVALID = 'isbnInvalid'; |
---|
39 | const NO_ISBN = 'isbnNoIsbn'; |
---|
40 | |
---|
41 | /** |
---|
42 | * Validation failure message template definitions. |
---|
43 | * |
---|
44 | * @var array |
---|
45 | */ |
---|
46 | protected $_messageTemplates = array( |
---|
47 | self::INVALID => "Invalid type given. String or integer expected", |
---|
48 | self::NO_ISBN => "'%value%' is no valid ISBN number", |
---|
49 | ); |
---|
50 | |
---|
51 | /** |
---|
52 | * Allowed type. |
---|
53 | * |
---|
54 | * @var string |
---|
55 | */ |
---|
56 | protected $_type = self::AUTO; |
---|
57 | |
---|
58 | /** |
---|
59 | * Separator character. |
---|
60 | * |
---|
61 | * @var string |
---|
62 | */ |
---|
63 | protected $_separator = ''; |
---|
64 | |
---|
65 | /** |
---|
66 | * Set up options. |
---|
67 | * |
---|
68 | * @param Zend_Config|array $options |
---|
69 | * @throws Zend_Validate_Exception When $options is not valid |
---|
70 | * @return void |
---|
71 | */ |
---|
72 | public function __construct($options = array()) |
---|
73 | { |
---|
74 | // prepare options |
---|
75 | if ($options instanceof Zend_Config) { |
---|
76 | $options = $options->toArray(); |
---|
77 | } |
---|
78 | if (!is_array($options)) { |
---|
79 | /** |
---|
80 | * @see Zend_Validate_Exception |
---|
81 | */ |
---|
82 | require_once 'Zend/Validate/Exception.php'; |
---|
83 | throw new Zend_Validate_Exception('Invalid options provided.'); |
---|
84 | } |
---|
85 | |
---|
86 | // set type |
---|
87 | if (array_key_exists('type', $options)) { |
---|
88 | $this->setType($options['type']); |
---|
89 | } |
---|
90 | |
---|
91 | // set separator |
---|
92 | if (array_key_exists('separator', $options)) { |
---|
93 | $this->setSeparator($options['separator']); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Detect input format. |
---|
99 | * |
---|
100 | * @return string |
---|
101 | */ |
---|
102 | protected function _detectFormat() |
---|
103 | { |
---|
104 | // prepare separator and pattern list |
---|
105 | $sep = quotemeta($this->_separator); |
---|
106 | $patterns = array(); |
---|
107 | $lengths = array(); |
---|
108 | |
---|
109 | // check for ISBN-10 |
---|
110 | if ($this->_type == self::ISBN10 || $this->_type == self::AUTO) { |
---|
111 | if (empty($sep)) { |
---|
112 | $pattern = '/^[0-9]{9}[0-9X]{1}$/'; |
---|
113 | $length = 10; |
---|
114 | } else { |
---|
115 | $pattern = "/^[0-9]{1,7}[{$sep}]{1}[0-9]{1,7}[{$sep}]{1}[0-9]{1,7}[{$sep}]{1}[0-9X]{1}$/"; |
---|
116 | $length = 13; |
---|
117 | } |
---|
118 | |
---|
119 | $patterns[$pattern] = self::ISBN10; |
---|
120 | $lengths[$pattern] = $length; |
---|
121 | } |
---|
122 | |
---|
123 | // check for ISBN-13 |
---|
124 | if ($this->_type == self::ISBN13 || $this->_type == self::AUTO) { |
---|
125 | if (empty($sep)) { |
---|
126 | $pattern = '/^[0-9]{13}$/'; |
---|
127 | $length = 13; |
---|
128 | } else { |
---|
129 | $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}$/"; |
---|
130 | $length = 17; |
---|
131 | } |
---|
132 | |
---|
133 | $patterns[$pattern] = self::ISBN13; |
---|
134 | $lengths[$pattern] = $length; |
---|
135 | } |
---|
136 | |
---|
137 | // check pattern list |
---|
138 | foreach ($patterns as $pattern => $type) { |
---|
139 | if ((strlen($this->_value) == $lengths[$pattern]) && preg_match($pattern, $this->_value)) { |
---|
140 | return $type; |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | return null; |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * Defined by Zend_Validate_Interface. |
---|
149 | * |
---|
150 | * Returns true if and only if $value is a valid ISBN. |
---|
151 | * |
---|
152 | * @param string $value |
---|
153 | * @return boolean |
---|
154 | */ |
---|
155 | public function isValid($value) |
---|
156 | { |
---|
157 | if (!is_string($value) && !is_int($value)) { |
---|
158 | $this->_error(self::INVALID); |
---|
159 | return false; |
---|
160 | } |
---|
161 | |
---|
162 | $value = (string) $value; |
---|
163 | $this->_setValue($value); |
---|
164 | |
---|
165 | switch ($this->_detectFormat()) { |
---|
166 | case self::ISBN10: |
---|
167 | // sum |
---|
168 | $isbn10 = str_replace($this->_separator, '', $value); |
---|
169 | $sum = 0; |
---|
170 | for ($i = 0; $i < 9; $i++) { |
---|
171 | $sum += (10 - $i) * $isbn10{$i}; |
---|
172 | } |
---|
173 | |
---|
174 | // checksum |
---|
175 | $checksum = 11 - ($sum % 11); |
---|
176 | if ($checksum == 11) { |
---|
177 | $checksum = '0'; |
---|
178 | } elseif ($checksum == 10) { |
---|
179 | $checksum = 'X'; |
---|
180 | } |
---|
181 | break; |
---|
182 | |
---|
183 | case self::ISBN13: |
---|
184 | // sum |
---|
185 | $isbn13 = str_replace($this->_separator, '', $value); |
---|
186 | $sum = 0; |
---|
187 | for ($i = 0; $i < 12; $i++) { |
---|
188 | if ($i % 2 == 0) { |
---|
189 | $sum += $isbn13{$i}; |
---|
190 | } else { |
---|
191 | $sum += 3 * $isbn13{$i}; |
---|
192 | } |
---|
193 | } |
---|
194 | // checksum |
---|
195 | $checksum = 10 - ($sum % 10); |
---|
196 | if ($checksum == 10) { |
---|
197 | $checksum = '0'; |
---|
198 | } |
---|
199 | break; |
---|
200 | |
---|
201 | default: |
---|
202 | $this->_error(self::NO_ISBN); |
---|
203 | return false; |
---|
204 | } |
---|
205 | |
---|
206 | // validate |
---|
207 | if (substr($this->_value, -1) != $checksum) { |
---|
208 | $this->_error(self::NO_ISBN); |
---|
209 | return false; |
---|
210 | } |
---|
211 | return true; |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | * Set separator characters. |
---|
216 | * |
---|
217 | * It is allowed only empty string, hyphen and space. |
---|
218 | * |
---|
219 | * @param string $separator |
---|
220 | * @throws Zend_Validate_Exception When $separator is not valid |
---|
221 | * @return Zend_Validate_Isbn Provides a fluent interface |
---|
222 | */ |
---|
223 | public function setSeparator($separator) |
---|
224 | { |
---|
225 | // check separator |
---|
226 | if (!in_array($separator, array('-', ' ', ''))) { |
---|
227 | /** |
---|
228 | * @see Zend_Validate_Exception |
---|
229 | */ |
---|
230 | require_once 'Zend/Validate/Exception.php'; |
---|
231 | throw new Zend_Validate_Exception('Invalid ISBN separator.'); |
---|
232 | } |
---|
233 | |
---|
234 | $this->_separator = $separator; |
---|
235 | return $this; |
---|
236 | } |
---|
237 | |
---|
238 | /** |
---|
239 | * Get separator characters. |
---|
240 | * |
---|
241 | * @return string |
---|
242 | */ |
---|
243 | public function getSeparator() |
---|
244 | { |
---|
245 | return $this->_separator; |
---|
246 | } |
---|
247 | |
---|
248 | /** |
---|
249 | * Set allowed ISBN type. |
---|
250 | * |
---|
251 | * @param string $type |
---|
252 | * @throws Zend_Validate_Exception When $type is not valid |
---|
253 | * @return Zend_Validate_Isbn Provides a fluent interface |
---|
254 | */ |
---|
255 | public function setType($type) |
---|
256 | { |
---|
257 | // check type |
---|
258 | if (!in_array($type, array(self::AUTO, self::ISBN10, self::ISBN13))) { |
---|
259 | /** |
---|
260 | * @see Zend_Validate_Exception |
---|
261 | */ |
---|
262 | require_once 'Zend/Validate/Exception.php'; |
---|
263 | throw new Zend_Validate_Exception('Invalid ISBN type'); |
---|
264 | } |
---|
265 | |
---|
266 | $this->_type = $type; |
---|
267 | return $this; |
---|
268 | } |
---|
269 | |
---|
270 | /** |
---|
271 | * Get allowed ISBN type. |
---|
272 | * |
---|
273 | * @return string |
---|
274 | */ |
---|
275 | public function getType() |
---|
276 | { |
---|
277 | return $this->_type; |
---|
278 | } |
---|
279 | } |
---|