IsoCodes\Isbn::validate PHP Method

validate() public static method

public static validate ( string $isbn, integer | null $type = null ) : boolean
$isbn string
$type integer | null 10 or 13. Leave empty for both
return boolean
    public static function validate($isbn, $type = null)
    {
        if ($type !== null && !in_array($type, [10, 13], true)) {
            throw new \InvalidArgumentException('ISBN type option must be 10 or 13');
        }
        $isbn = str_replace(' ', '', $isbn);
        $isbn = str_replace('-', '', $isbn);
        // this is a dash
        $isbn = str_replace('‐', '', $isbn);
        // this is an authentic hyphen
        if ($type === 10) {
            return self::validateIsbn10($isbn);
        }
        if ($type === 13) {
            return self::validateIsbn13($isbn);
        }
        return self::validateIsbn10($isbn) || self::validateIsbn13($isbn);
    }

Usage Example

Example #1
0
 /**
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage ISBN type option must be 10 or 13
  */
 public function testInvalidTypeOption()
 {
     Isbn::validate('', 0);
 }