Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
ISBN
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
9 / 9
21
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get10
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 get13
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 isValid
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 normalizeISBN
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getISBN10CheckDigit
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 isValidISBN10
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getISBN13CheckDigit
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isValidISBN13
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * ISBN validation and conversion functionality
5 *
6 * PHP version 7
7 *
8 * Copyright (c) Demian Katz 2010.
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
30namespace VuFindCode;
31
32use function intval;
33use function strlen;
34
35/**
36 * ISBN Class
37 *
38 * This class provides ISBN validation and conversion functionality.
39 *
40 * @category VuFind
41 * @package  Code
42 * @author   Demian Katz <demian.katz@villanova.edu>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org Main Page
45 */
46class ISBN
47{
48    /**
49     * Raw ISBN string
50     *
51     * @var string
52     */
53    protected $raw;
54
55    /**
56     * Validation status of ISBN (null until checked)
57     *
58     * @var bool
59     */
60    protected $valid = null;
61
62    /**
63     * Constructor
64     *
65     * @param string $raw Raw ISBN string to convert/validate.
66     */
67    public function __construct($raw)
68    {
69        // Strip out irrelevant characters:
70        $this->raw = self::normalizeISBN($raw);
71    }
72
73    /**
74     * Get the ISBN in ISBN-10 format:
75     *
76     * @return mixed ISBN, or false if invalid/incompatible.
77     */
78    public function get10()
79    {
80        // Is it valid?
81        if ($this->isValid()) {
82            // Is it already an ISBN-10?  If so, return as-is.
83            if (strlen($this->raw) == 10) {
84                return $this->raw;
85            } elseif (
86                strlen($this->raw) == 13
87                && str_starts_with($this->raw, '978')
88            ) {
89                // Is it a Bookland EAN?  If so, we can convert to ISBN-10.
90                $start = substr($this->raw, 3, 9);
91                return $start . self::getISBN10CheckDigit($start);
92            }
93        }
94
95        // If we made it this far, conversion was not possible:
96        return false;
97    }
98
99    /**
100     * Get the ISBN in ISBN-13 format:
101     *
102     * @return mixed ISBN, or false if invalid/incompatible.
103     */
104    public function get13()
105    {
106        // Is it invalid?
107        if (!$this->isValid()) {
108            return false;
109        }
110        // Is it an ISBN-10?  If so, convert to Bookland EAN:
111        if (strlen($this->raw) == 10) {
112            $start = '978' . substr($this->raw, 0, 9);
113            return $start . self::getISBN13CheckDigit($start);
114        }
115        // If we made it this far, it must already be an ISBN-13; return as-is.
116        return $this->raw;
117    }
118
119    /**
120     * Is the current ISBN valid in some format?  (May be 10 or 13 digit).
121     *
122     * @return bool
123     */
124    public function isValid()
125    {
126        // If we haven't already checked validity, do so now and store the result:
127        if (null === $this->valid) {
128            $this->valid = self::isValidISBN10($this->raw)
129                || self::isValidISBN13($this->raw);
130        }
131        return $this->valid;
132    }
133
134    /**
135     * Return the first sequence of at least 8 digits followed by an optional X.
136     * These ISBN characters may be separated by any number of '.', '-', '_' and
137     * whitespace characters; the separation characters are removed.
138     * A lower x is converted to an upper X.
139     *
140     * @param string $raw ISBN to clean up.
141     *
142     * @return string     Normalized ISBN.
143     */
144    public static function normalizeISBN($raw)
145    {
146        if (!preg_match('/(?:\d(?:[\s_.-])*){8,}[xX]?/', $raw, $match)) {
147            return '';
148        }
149        return preg_replace('/[^0-9X]/', '', strtoupper($match[0]));
150    }
151
152    /**
153     * Given the first 9 digits of an ISBN-10, generate the check digit.
154     *
155     * @param string $isbn The first 9 digits of an ISBN-10.
156     *
157     * @return string      The check digit.
158     */
159    public static function getISBN10CheckDigit($isbn)
160    {
161        $sum = 0;
162        for ($x = 0; $x < strlen($isbn); $x++) {
163            $sum += intval(substr($isbn, $x, 1)) * (1 + $x);
164        }
165        $checkdigit = $sum % 11;
166        return $checkdigit == 10 ? 'X' : $checkdigit;
167    }
168
169    /**
170     * Is the provided ISBN-10 valid?
171     *
172     * @param string $isbn The ISBN-10 to test.
173     *
174     * @return bool
175     */
176    public static function isValidISBN10($isbn)
177    {
178        $isbn = self::normalizeISBN($isbn);
179        return (strlen($isbn) != 10)
180            ? false
181            : str_ends_with($isbn, self::getISBN10CheckDigit(substr($isbn, 0, 9)));
182    }
183
184    /**
185     * Given the first 12 digits of an ISBN-13, generate the check digit.
186     *
187     * @param string $isbn The first 12 digits of an ISBN-13.
188     *
189     * @return string      The check digit.
190     */
191    public static function getISBN13CheckDigit($isbn)
192    {
193        // ISBN-13 is the same as EAN-13:
194        return EAN::getEAN13CheckDigit($isbn);
195    }
196
197    /**
198     * Is the provided ISBN-13 valid?
199     *
200     * @param string $isbn The ISBN-13 to test.
201     *
202     * @return bool
203     */
204    public static function isValidISBN13($isbn)
205    {
206        // ISBN-13 is the same as EAN-13:
207        return EAN::isValidEAN13($isbn);
208    }
209}