Skip to content

Commit

Permalink
feat: add InvalidCharactersException
Browse files Browse the repository at this point in the history
  • Loading branch information
clemlatz committed Nov 27, 2022
1 parent 0937137 commit 7dd7b98
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
21 changes: 21 additions & 0 deletions src/Biblys/Isbn/Exception/InvalidCharactersException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the biblys/isbn package.
*
* (c) Clément Latzarus
*
* 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\Exception;

class InvalidCharactersException extends IsbnParsingException
{
public function __construct($invalidCharacters)
{
parent::__construct("Cannot parse string with invalid characters: $invalidCharacters");
}
}
17 changes: 14 additions & 3 deletions src/Biblys/Isbn/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@
namespace Biblys\Isbn;

use Biblys\Isbn\Exception\EmptyInputException;
use Biblys\Isbn\Exception\InvalidCharactersException;
use Biblys\Isbn\Exception\IsbnParsingException;

class Parser
{
// FIXME: Create custom exceptions for each case
const
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',
ERROR_CANNOT_MATCH_RANGE = "Cannot find any ISBN range matching prefix %s";

/**
* @throws EmptyInputException
* @throws InvalidCharactersException
*/
public static function parse(string $input): ParsedIsbn
{
Expand All @@ -37,8 +38,9 @@ public static function parse(string $input): ParsedIsbn
$inputWithoutHyphens = self::_stripHyphens($input);
$inputWithoutChecksum = self::_stripChecksum($inputWithoutHyphens);

if (!is_numeric($inputWithoutChecksum)) {
throw new IsbnParsingException(static::ERROR_INVALID_CHARACTERS);
$invalidCharacters = self::_extractInvalidCharacters($inputWithoutChecksum);
if (!empty($invalidCharacters)) {
throw new InvalidCharactersException($invalidCharacters);
}

$result = self::_extractProductCode($inputWithoutChecksum);
Expand Down Expand Up @@ -184,4 +186,13 @@ private static function _extractPublisherCode($input, $productCode, $countryCode
sprintf(static::ERROR_CANNOT_MATCH_RANGE, $prefix)
);
}

/**
* @param string $inputWithoutChecksum
* @return string
*/
public static function _extractInvalidCharacters(string $inputWithoutChecksum): string
{
return preg_replace('/[0-9]+/', '', $inputWithoutChecksum);
}
}
13 changes: 13 additions & 0 deletions tests/unit/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,17 @@ public function testEmptyInput()
Parser::parse($emptyValue);
}

public function testInvalidCharacters()
{
// given
$stringWithInvalidCharacters = "978-1-2345-ABCD-0";

// then
$this->expectException(InvalidCharactersException::class);
$this->expectExceptionMessage("Cannot parse string with invalid characters: ABCD");

// when
Parser::parse($stringWithInvalidCharacters);
}

}

0 comments on commit 7dd7b98

Please sign in to comment.