Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate ISBN with EAN-2 or EAN-5 add-on. #13

Open
dac514 opened this issue Nov 5, 2015 · 4 comments
Open

Validate ISBN with EAN-2 or EAN-5 add-on. #13

dac514 opened this issue Nov 5, 2015 · 4 comments

Comments

@dac514
Copy link

dac514 commented Nov 5, 2015

ISBN supports a 2 or 5 digit add-on, example: 978-1-873671-00-9 54499

Valid ISBN Number

Can you improve your library to validate this?

Thanks.

Source:

@Fale
Copy link
Owner

Fale commented Nov 21, 2017

Are there machine-parsable set of rules for those?

@dac514
Copy link
Author

dac514 commented Nov 21, 2017

Here they are:

https://www.activebarcode.com/codes/ean5_ean2.html

Valid characters: 01234567890
Length: 2 bzw. 5
Check digit: none
Type#: EAN-5 - #3 - CODEEAN5, EAN-2 - #4 - CODEEAN2

@Fale
Copy link
Owner

Fale commented Nov 21, 2017

It seems like they "add" stuff to EAN codes, and that sometimes are used with ISBN but they are not part of the ISBN standard. What kind of output would you expect?

@dac514
Copy link
Author

dac514 commented Nov 21, 2017

Well, this ticket is from 2015 so we already have this working, heh.

We use your library and our code looks something like this:

	/**
	 * Validate an ISBN string
	 *
	 * @param $isbn_number
	 *
	 * @return bool
	 */
	function validateIsbnNumber( $isbn_number ) {
		// Regex to split a string only by the last whitespace character
		@list( $isbn_number, $addon ) = preg_split( '/\s+(?=\S*+$)/', trim( $isbn_number ) ); // @codingStandardsIgnoreLine
		$is_valid_isbn = ( new \Isbn\Isbn() )->validation->isbn( $isbn_number );
		$is_valid_addon = true;
		if ( $addon ) {
			if ( ! preg_match( '/^([0-9]{2}|[0-9]{5})$/', $addon ) ) {
				$is_valid_addon = false;
			}
		}
		return $is_valid_isbn && $is_valid_addon;
	}

The PHPUnit @dataProvider looks like this:

	/**
	 *  [ $isbnNumber, $expected ]
	 *
	 * @return array
	 */
	public function validateIsbnNumberProvider() {
		return [
			[ '978-1-873671-00-9 54499', true ],
			[ '978-1-873671-00-9 59', true ],
			[ '978-1-873671-00-9', true ],
			[ '9781873671009', true ],
			[ '1-86074-271-8', true ],
			[ '   1-86074-271-8   ', true ],
			[ '   1-86074-271-8    88888', true ],
			[ '   1-86074-271-8    88888   ', true ],
			[ '978-1-873671-00-9 111', false ],
			[ '111-1-873671-00-9', false ],
			[ '1111111111111', false ],
			[ 'abc', false ],
			[ '', false ],
			[ '1-86074-271-2', false ], // We do not support automatically upgrading ISBN-10 to ISBN-13
			[ '978-1-873671-00', false ], // We do not support ISBN without a trailing check digit
		];
	}

Does this help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants