Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit fd2a1c8

Browse files
committed
Merge branch 'feature/47' into develop
Close #47
2 parents bf3fa57 + 2e6d2f3 commit fd2a1c8

File tree

4 files changed

+135
-31
lines changed

4 files changed

+135
-31
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ All notable changes to this project will be documented in this file, in reverse
88

99
- [#18](https://github.com/zendframework/zend-validator/pull/18) adds a `GpsPoint`
1010
validator for validating GPS coordinates.
11+
- [#47](https://github.com/zendframework/zend-validator/pull/47) adds two new
12+
classes, `Zend\Validator\Isbn\Isbn10` and `Isbn13`; these classes are the
13+
result of an extract class refactoring, and contain the logic specific to
14+
calcualting the checksum for each ISBN style. `Zend\Validator\Isbn` now
15+
instantiates the appropriate one and invokes it.
1116

1217
### Deprecated
1318

src/Isbn.php

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
/**
33
* Zend Framework (http://framework.zend.com/)
44
*
5-
* @link http://github.com/zendframework/zf2 for the canonical source repository
6-
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @link http://github.com/zendframework/zend-validator for the canonical source repository
6+
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
77
* @license http://framework.zend.com/license/new-bsd New BSD License
88
*/
99

@@ -101,45 +101,21 @@ public function isValid($value)
101101

102102
switch ($this->detectFormat()) {
103103
case self::ISBN10:
104-
// sum
105-
$isbn10 = str_replace($this->getSeparator(), '', $value);
106-
$sum = 0;
107-
for ($i = 0; $i < 9; $i++) {
108-
$sum += (10 - $i) * $isbn10{$i};
109-
}
110-
111-
// checksum
112-
$checksum = 11 - ($sum % 11);
113-
if ($checksum == 11) {
114-
$checksum = '0';
115-
} elseif ($checksum == 10) {
116-
$checksum = 'X';
117-
}
104+
$isbn = new Isbn\Isbn10();
118105
break;
119106

120107
case self::ISBN13:
121-
// sum
122-
$isbn13 = str_replace($this->getSeparator(), '', $value);
123-
$sum = 0;
124-
for ($i = 0; $i < 12; $i++) {
125-
if ($i % 2 == 0) {
126-
$sum += $isbn13{$i};
127-
} else {
128-
$sum += 3 * $isbn13{$i};
129-
}
130-
}
131-
// checksum
132-
$checksum = 10 - ($sum % 10);
133-
if ($checksum == 10) {
134-
$checksum = '0';
135-
}
108+
$isbn = new Isbn\Isbn13();
136109
break;
137110

138111
default:
139112
$this->error(self::NO_ISBN);
140113
return false;
141114
}
142115

116+
$value = str_replace($this->getSeparator(), '', $value);
117+
$checksum = $isbn->getChecksum($value);
118+
143119
// validate
144120
if (substr($this->getValue(), -1) != $checksum) {
145121
$this->error(self::NO_ISBN);

src/Isbn/Isbn10.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zend-validator for the canonical source repository
6+
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace Zend\Validator\Isbn;
11+
12+
class Isbn10
13+
{
14+
/**
15+
* @param int|string $value
16+
* @return int|string
17+
*/
18+
public function getChecksum($value)
19+
{
20+
$sum = $this->sum($value);
21+
return $this->checksum($sum);
22+
}
23+
24+
/**
25+
* Calculate the value sum.
26+
*
27+
* @param int|string $value
28+
* @return int
29+
*/
30+
private function sum($value)
31+
{
32+
$sum = 0;
33+
34+
for ($i = 0; $i < 9; $i++) {
35+
$sum += (10 - $i) * $value{$i};
36+
}
37+
38+
return $sum;
39+
}
40+
41+
/**
42+
* Calculate the checksum for the value's sum.
43+
*
44+
* @param int $sum
45+
* @return int|string
46+
*/
47+
private function checksum($sum)
48+
{
49+
$checksum = 11 - ($sum % 11);
50+
51+
if ($checksum == 11) {
52+
return '0';
53+
}
54+
55+
if ($checksum == 10) {
56+
return 'X';
57+
}
58+
59+
return $checksum;
60+
}
61+
}

src/Isbn/Isbn13.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zend-validator for the canonical source repository
6+
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace Zend\Validator\Isbn;
11+
12+
class Isbn13
13+
{
14+
/**
15+
* @param int|string $value
16+
* @return int|string
17+
*/
18+
public function getChecksum($value)
19+
{
20+
$sum = $this->sum($value);
21+
return $this->checksum($sum);
22+
}
23+
24+
/**
25+
* Calculate the value sum.
26+
*
27+
* @param int|string $value
28+
* @return int
29+
*/
30+
private function sum($value)
31+
{
32+
$sum = 0;
33+
34+
for ($i = 0; $i < 12; $i++) {
35+
if ($i % 2 == 0) {
36+
$sum += $value{$i};
37+
continue;
38+
}
39+
40+
$sum += 3 * $value{$i};
41+
}
42+
43+
return $sum;
44+
}
45+
46+
/**
47+
* Calculate the checksum for the value's sum.
48+
*
49+
* @param int $sum
50+
* @return int|string
51+
*/
52+
private function checksum($sum)
53+
{
54+
$checksum = 10 - ($sum % 10);
55+
56+
if ($checksum == 10) {
57+
return '0';
58+
}
59+
60+
return $checksum;
61+
}
62+
}

0 commit comments

Comments
 (0)