Skip to content

Commit

Permalink
Implement isParsable static method
Browse files Browse the repository at this point in the history
  • Loading branch information
clemlatz committed Mar 5, 2021
1 parent 75c4efd commit 501bf56
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,15 @@ All validating methods:
- `Isbn::validateAsIsbn10`
- `Isbn::validateAsIbsn13`
- `Isbn::validateAsEan13`
- `Isbn::isParsable`

[Learn more about validating ISBNs](https://github.com/biblys/isbn/wiki/Validating-ISBNs-using-the-new-public-API)

## Development

### Using Gitpod

You can start a dev environnement by clicking
You can start a dev environnement by clicking
[![Gitpod ready-to-code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/biblys/isbn)
and start hacking in your browser right away!

Expand Down
20 changes: 20 additions & 0 deletions src/Biblys/Isbn/Isbn.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,26 @@ static public function validateAsEan13(string $input): void
}
}

/**
* 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

Expand Down
37 changes: 37 additions & 0 deletions tests/isParsableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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.
*/

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once __DIR__ . '/../vendor/autoload.php';

use Biblys\Isbn\Isbn;
use PHPUnit\Framework\TestCase;

class testIsParsable extends TestCase
{
public function testParsableIsbn()
{
$isParsable = Isbn::isParsable("9782207258040");

$this->assertTrue($isParsable);
}

public function testUnparsableIsbn()
{
$isParsable = Isbn::isParsable("9782SPI258040");

$this->assertFalse($isParsable);
}
}

0 comments on commit 501bf56

Please sign in to comment.