initial commit

This commit is contained in:
2021-12-20 12:50:53 +01:00
commit 839913d51a
51 changed files with 13051 additions and 0 deletions

View File

@ -0,0 +1,115 @@
<?php
/*
* This file is part of the biblys/isbn package.
*
* (c) Clément Bourgoin
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
namespace Biblys\Isbn;
class Formatter
{
public static function formatAsIsbn10(string $input): string
{
$parsedInput = Parser::parse($input);
$countryCode = $parsedInput["countryCode"];
$publisherCode = $parsedInput["publisherCode"];
$publicationCode = $parsedInput["publicationCode"];
$checksum = self::_calculateChecksumForIsbn10Format($countryCode, $publisherCode, $publicationCode);
return "$countryCode-$publisherCode-$publicationCode-$checksum";
}
public static function formatAsIsbn13(string $input): string
{
$parsedInput = Parser::parse($input);
$productCode = $parsedInput['productCode'];
$countryCode = $parsedInput["countryCode"];
$publisherCode = $parsedInput["publisherCode"];
$publicationCode = $parsedInput["publicationCode"];
$checksum = self::_calculateChecksumForIsbn13Format($productCode, $countryCode, $publisherCode, $publicationCode);
return "$productCode-$countryCode-$publisherCode-$publicationCode-$checksum";
}
public static function formatAsEan13(string $input): string
{
$parsedInput = Parser::parse($input);
$productCode = $parsedInput['productCode'];
$countryCode = $parsedInput["countryCode"];
$publisherCode = $parsedInput["publisherCode"];
$publicationCode = $parsedInput["publicationCode"];
$checksum = self::_calculateChecksumForIsbn13Format($productCode, $countryCode, $publisherCode, $publicationCode);
return $productCode . $countryCode . $publisherCode . $publicationCode . $checksum;
}
public static function formatAsGtin14(string $input, int $prefix): string
{
$parsedInput = Parser::parse($input);
$productCode = $parsedInput['productCode'];
$countryCode = $parsedInput['countryCode'];
$publisherCode = $parsedInput['publisherCode'];
$publicationCode = $parsedInput['publicationCode'];
$productCodeWithPrefix = $prefix . $productCode;
$checksum = self::_calculateChecksumForIsbn13Format($productCodeWithPrefix, $countryCode, $publisherCode, $publicationCode);
return $prefix . $productCode . $countryCode . $publisherCode . $publicationCode . $checksum;
}
private static function _calculateChecksumForIsbn10Format(
string $countryCode,
string $publisherCode,
string $publicationCode
): string {
$code = $countryCode . $publisherCode . $publicationCode;
$chars = str_split($code);
$checksum = (11 - (
($chars[0] * 10) +
($chars[1] * 9) +
($chars[2] * 8) +
($chars[3] * 7) +
($chars[4] * 6) +
($chars[5] * 5) +
($chars[6] * 4) +
($chars[7] * 3) +
($chars[8] * 2)) % 11) % 11;
if ($checksum == 10) {
$checksum = 'X';
}
return $checksum;
}
private static function _calculateChecksumForIsbn13Format(
string $productCode,
string $countryCode,
string $publisherCode,
string $publicationCode
): string {
$checksum = null;
$code = $productCode . $countryCode . $publisherCode . $publicationCode;
$chars = array_reverse(str_split($code));
foreach ($chars as $index => $char) {
if ($index & 1) {
$checksum += $char;
} else {
$checksum += $char * 3;
}
}
$checksum = (10 - ($checksum % 10)) % 10;
return $checksum;
}
}

View File

@ -0,0 +1,345 @@
<?php
/*
* This file is part of the biblys/isbn package.
*
* (c) Clément Bourgoin
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
namespace Biblys\Isbn;
class Isbn
{
/**
* Converts input into an ISBN-10
*
* ISBN-10 are 10 characters long and includes hyphens.
*
* // Returns 3-464-60352-0
* $isbn10 = Isbn::convertToIsbn10("9783464603529");
*
* @param string $input A string to convert
*
* @return string
*/
static public function convertToIsbn10(string $input): string
{
return Formatter::formatAsIsbn10($input);
}
/**
* Converts input into an ISBN-13
*
* ISBN-13 are 13 characters long and includes hyphens.
*
* // Returns 978-2-207-25804-0
* $isbn10 = Isbn::convertToIsbn13("9782207258040");
*
* @param string $input A string to convert
*
* @return string
*/
static public function convertToIsbn13(string $input): string
{
return Formatter::formatAsIsbn13($input);
}
/**
* Converts input into an EAN-13
*
* EAN-13 are 13 characters long and does not include hyphens.
*
* // Returns 9782207258040
* $isbn10 = Isbn::convertToEan13("978-2-207-25804-0");
*
* @param string $input A string to convert
*
* @return string
*/
static public function convertToEan13(string $input): string
{
return Formatter::formatAsEan13($input);
}
/**
* Converts input into a GTIN-14
*
* GTIN-14 are 14 characters long and does not include hyphens.
*
* // Returns 19783464603526
* $isbn10 = Isbn::convertToGtin14("9783464603529", 1);
*
* @param string $input A string to convert
* @param int $prefix A int to preprend (defaults to 1)
*
* @return string
*/
static public function convertToGtin14(string $input, int $prefix = 1): string
{
return Formatter::formatAsGtin14($input, $prefix);
}
/**
* Validates input as a correctly formed ISBN-10
*
* // Throws because second hyphen is misplaced
* Isbn::validateAsIsbn10("3-46460-352-0");
*
* @param string $input A string to validate
*
* @throws IsbnValidationException
*/
static public function validateAsIsbn10(string $input): void
{
$expected = Formatter::formatAsIsbn10($input);
if ($input !== $expected) {
throw new IsbnValidationException(
"$input is not a valid ISBN-10. Expected $expected."
);
}
}
/**
* Validates input as a correctly formed ISBN-13
*
* // Throws because second hyphen is misplaced
* Isbn::validateAsIsbn13("978-220-7-25804-0");
*
* @param string $input A string to validate
*
* @throws IsbnValidationException
*/
static public function validateAsIsbn13(string $input): void
{
$expected = Formatter::formatAsIsbn13($input);
if ($input !== $expected) {
throw new IsbnValidationException(
"$input is not a valid ISBN-13. Expected $expected."
);
}
}
/**
* Validates input as a correctly formed EAN-13
*
* // Throws because checksum character is invalid
* Isbn::validateAsEan13("9782207258045");
*
* @param string $input A string to validate
*
* @throws IsbnValidationException
*/
static public function validateAsEan13(string $input): void
{
$expected = Formatter::formatAsEan13($input);
if ($input !== $expected) {
throw new IsbnValidationException(
"$input is not a valid EAN-13. Expected $expected."
);
}
}
/**
* Checks that an input can be parsed (and thus, formatted) by the library
*
* // Returns false because string contains invalid characters
* Isbn::validateAsEan13("9782SPI258040");
*
* @param string $input A string to check for parsability
*
* @return boolean true if the input can be parsed
*/
static public function isParsable(string $input): bool
{
try {
Parser::parse($input);
return true;
} catch (IsbnParsingException $exception) {
return false;
}
}
/* Legacy non static properties and methods (backward compatibility) */
// FIXME: deprecate and remove on next major version
private $_input;
private $_gs1productCode;
private $_countryCode;
private $_publisherCode;
private $_publicationCode;
private $_isbnAgencyCode;
private $_checksumCharacter;
private $_gtin14Prefix;
public function __construct($code = null)
{
$this->_input = $code;
try {
$parsedCode = Parser::parse($code);
$this->_gs1productCode = $parsedCode["productCode"];
$this->_countryCode = $parsedCode["countryCode"];
$this->_isbnAgencyCode = $parsedCode["agencyCode"];
$this->_publisherCode = $parsedCode["publisherCode"];
$this->_publicationCode = $parsedCode["publicationCode"];
} catch (IsbnParsingException $exception) {
// FIXME in next major version (breaking change)
// For backward compatibility reason, instanciating should not throw
}
}
/**
* Checks if ISBN is valid
*
* @deprecated
*
* @return boolean true if the ISBN is valid
*/
public function isValid()
{
trigger_error(
"Isbn->isValid is deprecated and will be removed in the future. Use Isbn::validateAs… methods instead. Learn more: https://git.io/JtAEx",
E_USER_DEPRECATED
);
try {
Parser::parse($this->_input);
return true;
} catch (\Exception $exception) {
return false;
}
}
/**
* Returns a list of errors if ISBN is invalid
*
* @deprecated
*
* @return string the error list
*/
public function getErrors()
{
trigger_error(
"Isbn->getErrors is deprecated and will be removed in the future. Use Isbn::validateAs… methods instead. Learn more: https://git.io/JtAEx",
E_USER_DEPRECATED
);
try {
Parser::parse($this->_input);
} catch (\Exception $exception) {
return '[' . $this->_input . '] ' . $exception->getMessage();
}
}
/**
* Throws an exception if ISBN is invalid
*
* @deprecated
*/
public function validate()
{
trigger_error(
"Isbn->validate is deprecated and will be removed in the future. Use Isbn::validateAs… methods instead. Learn more: https://git.io/JtAEx",
E_USER_DEPRECATED
);
Parser::parse($this->_input);
return true;
}
/**
* Formats an ISBN according to specified format
*
* @deprecated
*
* @param string $format (ISBN-10, ISBN-13, EAN-13, GTIN-14), default EAN-13
* @param string $prefix The prefix to use when formatting, default 1
*
* @return string the formatted ISBN
*/
public function format($format = 'EAN-13', $prefix = 1)
{
try {
switch ($format) {
case 'ISBN-10':
trigger_error(
"Isbn->format is deprecated and will be removed in the future. Use the Isbn::convertToIsbn10 method instead. Learn more: https://git.io/JtAEx",
E_USER_DEPRECATED
);
return Formatter::formatAsIsbn10($this->_input);
case 'ISBN-13':
case 'ISBN':
trigger_error(
"Isbn->format is deprecated and will be removed in the future. Use the Isbn::convertToIsbn13 method instead. Learn more: https://git.io/JtAEx",
E_USER_DEPRECATED
);
return Formatter::formatAsIsbn13($this->_input);
case 'GTIN-14':
trigger_error(
"Isbn->format is deprecated and will be removed in the future. Use the Isbn::convertToGtin14 method instead. Learn more: https://git.io/JtAEx",
E_USER_DEPRECATED
);
return Formatter::formatAsGtin14($this->_input, $prefix);
case 'EAN-13':
case 'EAN':
default:
trigger_error(
"Isbn->format is deprecated and will be removed in the future. Use the Isbn::convertToEan13 method instead. Learn more: https://git.io/JtAEx",
E_USER_DEPRECATED
);
return Formatter::formatAsEan13($this->_input);
}
} catch (IsbnParsingException $exception) {
// FIXME: remove message customization
// (kept for backward compatibility)
throw new IsbnParsingException(
"Cannot format invalid ISBN: [$this->_input] " . $exception->getMessage()
);
}
}
public function getProduct()
{
return $this->_gs1productCode;
}
public function getCountry()
{
return $this->_countryCode;
}
public function getPublisher()
{
return $this->_publisherCode;
}
public function getPublication()
{
return $this->_publicationCode;
}
public function getChecksum()
{
return $this->_checksumCharacter;
}
public function getAgency()
{
return $this->_isbnAgencyCode;
}
public function getGtin14Prefix()
{
return $this->_gtin14Prefix;
}
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the biblys/isbn package.
*
* (c) Clément Bourgoin
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
namespace Biblys\Isbn;
class IsbnParsingException extends \Exception
{
}

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of the biblys/isbn package.
*
* (c) Clément Bourgoin
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
namespace Biblys\Isbn;
class IsbnValidationException extends \Exception
{
}

View File

@ -0,0 +1,179 @@
<?php
/*
* This file is part of the biblys/isbn package.
*
* (c) Clément Bourgoin
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
namespace Biblys\Isbn;
class Parser
{
// FIXME: Create custom exceptions for each case
const ERROR_EMPTY = 'No code provided',
ERROR_INVALID_CHARACTERS = 'Invalid characters in the code',
ERROR_INVALID_LENGTH = 'Code is too short or too long',
ERROR_INVALID_PRODUCT_CODE = 'Product code should be 978 or 979',
ERROR_INVALID_COUNTRY_CODE = 'Country code is unknown';
public static function parse($input)
{
if (empty($input)) {
throw new IsbnParsingException(static::ERROR_EMPTY);
}
$inputWithoutHyphens = self::_stripHyphens($input);
$inputWithoutChecksum = self::_stripChecksum($inputWithoutHyphens);
if (!is_numeric($inputWithoutChecksum)) {
throw new IsbnParsingException(static::ERROR_INVALID_CHARACTERS);
}
$result = self::_extractProductCode($inputWithoutChecksum);
$inputWithoutProductCode = $result[0];
$productCode = $result[1];
$result = self::_extractCountryCode($inputWithoutProductCode, $productCode);
$inputWithoutCountryCode = $result[0];
$countryCode = $result[1];
$result = self::_extractPublisherCode($inputWithoutCountryCode, $productCode, $countryCode);
$agencyCode = $result[0];
$publisherCode = $result[1];
$publicationCode = $result[2];
return [
"productCode" => $productCode,
"countryCode" => $countryCode,
"agencyCode" => $agencyCode,
"publisherCode" => $publisherCode,
"publicationCode" => $publicationCode,
];
}
private static function _stripHyphens($input)
{
$replacements = array('-', '_', ' ');
$input = str_replace($replacements, '', $input);
return $input;
}
private static function _stripChecksum($input)
{
$length = strlen($input);
if ($length == 13 || $length == 10) {
$input = substr_replace($input, "", -1);
return $input;
}
if ($length == 12 || $length == 9) {
return $input;
}
throw new IsbnParsingException(static::ERROR_INVALID_LENGTH);
}
private static function _extractProductCode($input)
{
if (strlen($input) == 9) {
return [$input, 978];
}
$first3 = substr($input, 0, 3);
if ($first3 == 978 || $first3 == 979) {
$input = substr($input, 3);
return [$input, $first3];
}
throw new IsbnParsingException(static::ERROR_INVALID_PRODUCT_CODE);
}
private static function _extractCountryCode($input, $productCode)
{
// Get the seven first digits
$first7 = substr($input, 0, 7);
// Select the right set of rules according to the product code
$ranges = new Ranges();
$prefixes = $ranges->getPrefixes();
foreach ($prefixes as $p) {
if ($p['Prefix'] == $productCode) {
$rules = $p['Rules']['Rule'];
break;
}
}
// Select the right rule
foreach ($rules as $r) {
$ra = explode('-', $r['Range']);
if ($first7 >= $ra[0] && $first7 <= $ra[1]) {
$length = $r['Length'];
break;
}
}
// Country code is invalid
if (!isset($length) || $length === "0") {
throw new IsbnParsingException(static::ERROR_INVALID_COUNTRY_CODE);
};
$countryCode = substr($input, 0, $length);
$input = substr($input, $length);
return [$input, $countryCode];
}
/**
* Remove and save Publisher Code and Publication Code
*/
private static function _extractPublisherCode($input, $productCode, $countryCode)
{
// Get the seven first digits or less
$first7 = substr($input, 0, 7);
$inputLength = strlen($first7);
// Select the right set of rules according to the agency (product + country code)
$ranges = new Ranges();
$groups = $ranges->getGroups();
foreach ($groups as $g) {
if ($g['Prefix'] <> $productCode . '-' . $countryCode) {
continue;
}
$rules = $g['Rules']['Rule'];
$agency = $g['Agency'];
// Select the right rule
foreach ($rules as $rule) {
// Get min and max value in range
// and trim values to match code length
$range = explode('-', $rule['Range']);
$min = substr($range[0], 0, $inputLength);
$max = substr($range[1], 0, $inputLength);
// If first 7 digits is smaller than min
// or greater than max, continue to next rule
if ($first7 < $min || $first7 > $max) {
continue;
}
$length = $rule['Length'];
$publisherCode = substr($input, 0, $length);
$publicationCode = substr($input, $length);
return [$agency, $publisherCode, $publicationCode];
}
break;
}
}
}

View File

@ -0,0 +1,35 @@
<?php
/*
* This file is part of the biblys/isbn package.
*
* (c) Clément Bourgoin
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
namespace Biblys\Isbn;
class Ranges
{
private $prefixes, $groups;
public function __construct()
{
include('ranges-array.php');
$this->prefixes = $prefixes;
$this->groups = $groups;
}
public function getPrefixes()
{
return $this->prefixes;
}
public function getGroups()
{
return $this->groups;
}
}

File diff suppressed because it is too large Load Diff