116 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
		
		
			
		
	
	
			116 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| 
								 | 
							
								<?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;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |