Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
EAN
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
3 / 3
8
100.00% covered (success)
100.00%
1 / 1
 normalizeEAN
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getEAN13CheckDigit
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 isValidEAN13
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * EAN validation and checksumming 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
30namespace VuFindCode;
31
32use function intval;
33use function strlen;
34
35/**
36 * EAN Class
37 *
38 * This class provides EAN validation and checksumming 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 EAN
47{
48    /**
49     * Return the first sequence of at least 12 digits followed by an optional X.
50     * These characters may be separated by any number of '.', '-', '_' and
51     * whitespace characters; the separation characters are removed.
52     * A lower x is converted to an upper X.
53     *
54     * @param string $raw EAN to clean up.
55     *
56     * @return string     Normalized EAN.
57     */
58    public static function normalizeEAN($raw)
59    {
60        if (!preg_match('/(?:\d(?:[\s_.-])*){12,}[xX]?/', $raw, $match)) {
61            return '';
62        }
63        return preg_replace('/[^0-9X]/', '', strtoupper($match[0]));
64    }
65
66    /**
67     * Given the first 12 digits of an EAN, generate the check digit.
68     *
69     * @param string $ean The first 12 digits of an EAN.
70     *
71     * @return string     The check digit.
72     */
73    public static function getEAN13CheckDigit($ean)
74    {
75        $sum = 0;
76        $weight = 1;
77        for ($x = 0; $x < strlen($ean); $x++) {
78            $sum += intval(substr($ean, $x, 1)) * $weight;
79            $weight = $weight == 1 ? 3 : 1;
80        }
81        $retval = 10 - ($sum % 10);
82        return $retval == 10 ? 0 : $retval;
83    }
84
85    /**
86     * Is the provided EAN valid?
87     *
88     * @param string $ean The EAN to test.
89     *
90     * @return bool
91     */
92    public static function isValidEAN13($ean)
93    {
94        $ean = static::normalizeEAN($ean);
95        return (strlen($ean) != 13)
96            ? false
97            : str_ends_with($ean, self::getEAN13CheckDigit(substr($ean, 0, 12)));
98    }
99}