Skip to content

Commit e13a438

Browse files
committed
Added ISBN validation before hitting providers
1 parent 8860dc4 commit e13a438

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
],
1111
"require": {
1212
"kriswallsmith/buzz": "~0.12",
13-
"symfony/intl": "~2.5.4"
13+
"symfony/intl": "~2.5.4",
14+
"symfony/validator": "~2.5.4"
1415
},
1516
"autoload": {
1617
"psr-4": { "Sprain\\BookFinder\\": "lib/BookFinder" }

lib/BookFinder/BookFinder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Sprain\BookFinder;
44
use Sprain\BookFinder\Response\BookFinderResponse;
5+
use Symfony\Component\Validator\Constraints\Isbn;
6+
use Symfony\Component\Validator\Validation;
57

68
/**
79
* BookFinder
@@ -31,6 +33,16 @@ public function searchByIsbn($isbn)
3133
$isbn = trim($isbn);
3234
$isbn = str_replace('-', '', $isbn);
3335

36+
// Validate isbn
37+
$validator = Validation::createValidator();
38+
$violations = $validator->validateValue($isbn, new Isbn());
39+
40+
if (count($violations) > 0) {
41+
42+
return false;
43+
}
44+
45+
// Use providers to search for book
3446
foreach($this->providers as $provider){
3547
$result = $provider['provider']->searchByIsbn($isbn)->getResult();
3648

0 commit comments

Comments
 (0)