Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
26 / 26 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
ISMN | |
100.00% |
26 / 26 |
|
100.00% |
9 / 9 |
18 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
get10 | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
get13 | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
isValid | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
normalizeISMN | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getISMN10CheckDigit | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isValidISMN10 | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getISMN13CheckDigit | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isValidISMN13 | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * ISMN validation and conversion functionality |
5 | * |
6 | * PHP version 7 |
7 | * |
8 | * Copyright (c) Demian Katz 2019. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package Code |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org Main Page |
28 | */ |
29 | |
30 | namespace VuFindCode; |
31 | |
32 | use function strlen; |
33 | |
34 | /** |
35 | * ISMN Class |
36 | * |
37 | * This class provides ISMN validation and conversion functionality. |
38 | * |
39 | * @category VuFind |
40 | * @package Code |
41 | * @author Demian Katz <demian.katz@villanova.edu> |
42 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
43 | * @link https://vufind.org Main Page |
44 | */ |
45 | class ISMN |
46 | { |
47 | /** |
48 | * Raw ISMN string |
49 | * |
50 | * @var string |
51 | */ |
52 | protected $raw; |
53 | |
54 | /** |
55 | * Validation status of ISMN (null until checked) |
56 | * |
57 | * @var bool |
58 | */ |
59 | protected $valid = null; |
60 | |
61 | /** |
62 | * Constructor |
63 | * |
64 | * @param string $raw Raw ISMN string to convert/validate. |
65 | */ |
66 | public function __construct($raw) |
67 | { |
68 | // Strip out irrelevant characters: |
69 | $this->raw = self::normalizeISMN($raw); |
70 | } |
71 | |
72 | /** |
73 | * Get the ISMN in ISMN-10 format: |
74 | * |
75 | * @return mixed ISMN, or false if invalid/incompatible. |
76 | */ |
77 | public function get10() |
78 | { |
79 | // Is it valid? |
80 | if ($this->isValid()) { |
81 | // Is it already an ISMN-10? If so, return as-is. |
82 | if (strlen($this->raw) == 10) { |
83 | return $this->raw; |
84 | } elseif ( |
85 | strlen($this->raw) == 13 |
86 | && str_starts_with($this->raw, '979') |
87 | ) { |
88 | // Is it a music EAN? If so, we can convert to ISMN-10. |
89 | $start = 'M' . substr($this->raw, 4, 8); |
90 | return $start . self::getISMN10CheckDigit($start); |
91 | } |
92 | } |
93 | |
94 | // If we made it this far, conversion was not possible: |
95 | return false; |
96 | } |
97 | |
98 | /** |
99 | * Get the ISMN in ISMN-13 format: |
100 | * |
101 | * @return mixed ISMN, or false if invalid/incompatible. |
102 | */ |
103 | public function get13() |
104 | { |
105 | // Is it invalid? |
106 | if (!$this->isValid()) { |
107 | return false; |
108 | } |
109 | // Is it an ISMN-10? If so, convert to music EAN: |
110 | if (strlen($this->raw) == 10) { |
111 | $start = '9790' . substr($this->raw, 1, 8); |
112 | return $start . self::getISMN13CheckDigit($start); |
113 | } |
114 | // If we made it this far, it must already be an ISMN-13; return as-is. |
115 | return $this->raw; |
116 | } |
117 | |
118 | /** |
119 | * Is the current ISMN valid in some format? (May be 10 or 13 digit). |
120 | * |
121 | * @return bool |
122 | */ |
123 | public function isValid() |
124 | { |
125 | // If we haven't already checked validity, do so now and store the result: |
126 | if (null === $this->valid) { |
127 | $this->valid = self::isValidISMN10($this->raw) |
128 | || self::isValidISMN13($this->raw); |
129 | } |
130 | return $this->valid; |
131 | } |
132 | |
133 | /** |
134 | * Return the first sequence of at least 9 digits preceded by an optional M. |
135 | * These ISMN characters may be separated by any number of '.', '-', '_' and |
136 | * whitespace characters; the separation characters are removed. |
137 | * A lower m is converted to an upper M. |
138 | * |
139 | * @param string $raw ISMN to clean up. |
140 | * |
141 | * @return string Normalized ISMN. |
142 | */ |
143 | public static function normalizeISMN($raw) |
144 | { |
145 | $regex = '/m?(?:(?:[\s_.-])*[0-9](?:[\s_.-])*){9,}/i'; |
146 | return preg_match($regex, $raw, $match) |
147 | ? preg_replace('/[^0-9M]/', '', strtoupper($match[0])) : ''; |
148 | } |
149 | |
150 | /** |
151 | * Given the first 9 digits of an ISMN-10, generate the check digit. |
152 | * |
153 | * @param string $ismn The first 9 digits of an ISMN-10. |
154 | * |
155 | * @return string The check digit. |
156 | */ |
157 | public static function getISMN10CheckDigit($ismn) |
158 | { |
159 | return static::getISMN13CheckDigit(preg_replace('/^m/i', '9790', $ismn)); |
160 | } |
161 | |
162 | /** |
163 | * Is the provided ISMN-10 valid? |
164 | * |
165 | * @param string $ismn The ISMN-10 to test. |
166 | * |
167 | * @return bool |
168 | */ |
169 | public static function isValidISMN10($ismn) |
170 | { |
171 | return static::isValidISMN13(preg_replace('/^m/i', '9790', $ismn)); |
172 | } |
173 | |
174 | /** |
175 | * Given the first 12 digits of an ISMN-13, generate the check digit. |
176 | * |
177 | * @param string $ismn The first 12 digits of an ISMN-13. |
178 | * |
179 | * @return string The check digit. |
180 | */ |
181 | public static function getISMN13CheckDigit($ismn) |
182 | { |
183 | // ISMN-13 is the same as EAN-13: |
184 | return EAN::getEAN13CheckDigit($ismn); |
185 | } |
186 | |
187 | /** |
188 | * Is the provided ISMN-13 valid? |
189 | * |
190 | * @param string $ismn The ISMN-13 to test. |
191 | * |
192 | * @return bool |
193 | */ |
194 | public static function isValidISMN13($ismn) |
195 | { |
196 | // ISMN-13 is the same as EAN-13: |
197 | return EAN::isValidEAN13($ismn); |
198 | } |
199 | } |