Skip to content

Methods deprecated in 2.3.0

Clément Bourgoin edited this page Jan 10, 2022 · 14 revisions

The following methods were deprecated in update 2.3.0. They will be supported until release 3.0 but using the new API (see README) is recommended.

Isbn->isValid

Introduced in 2.0, the Isbn->isValid method now issues a deprecation warning:

Isbn->isValid is deprecated and will be removed in the future. Use Isbn::validateAs… methods instead.

TLDR: if you want to replicate the exact behavior Isbn->isValid in your implementation, you can replace it with Isbn::isParsable without instantiating the Isbn class.

// Legacy usage
$isbn = new Isbn("9781234567890")
if ($isbn->isValid()) { 
  echo "ISBN is valid!"; 
}

// New usage
if (Isbn::isParsable("9781234567890")) { 
  echo "ISBN is valid!";
}

Learn more about the Isbn::isParsable method.

But you probably don’t need to!

While the legacy Isbn->format method required you to validate a string before attempting to convert it to target format, using one of the new Isbn::convertAs… methods you can directly call the method and catch exceptions.

Learn more about how to upgrade from Isbn->format to Isbn::convertTo….

If you need to strictly validate a string against one of the format supported by the library (ISBN-10, ISBN-13, EAN-13), take a look at the new ISBN::validateAs… methods, but please not that this does not replicate the behavior of isValid.

Isbn->getErrors

Introduced in 2.0, the Isbn->getErrors now issues a deprecation warning:

Isbn->getErrors is deprecated and will be removed in the future. Use Isbn::validateAs… methods instead.

The recommend way to get validation errors is now to use one of the Isbn::validateAs… methods introduced in 2.3.0. They do not require to instantiate an Isbn object and can be called statically. If validation fails, you can catch Exception to get the error message.

Learn more about the new Isbn::validateAs… methods

Note that the Isbn::convertAs… methods will also throw an Exception if input is so badly formed that in cannot be parsed, but otherwise it will still attempt to format the input event is hyphen are misplaced or checksum character is incorrect. The Isbn->isValid legacy method had a similar behavior.

Learn more about how to upgrade from Isbn->format to Isbn::convertTo….

Isbn->validate

Introduced in 2.1.0, the Isbn->validate now throws a deprecation warning:

Isbn->format is deprecated and will be removed in the future. Use the Isbn::validateAs… method instead.

It was deprecated because of its misleading name. it would only throw if parsing the string was impossible, and input would still be validated if, let's say, checksum was incorrect or hyphens were misplaced. Also, it was impossible to specify against which format (ISBN-10, ISBN-13, EAN-13) the input should be validated.

Learn more about the new Isbn::validateAs… methods

Isbn->format

Introduced in 2.0, the Isbn->format method was used to convert a string into some representation of an ISBN (ISBN-10, ISBN-13, EAN-13 or GTIN-14). It would take the target format as first argument and return a formatted string.

// Deprecated usage
try {
    $input = new Isbn("9782843449499");
    $isbn13 = $input->format("ISBN-13");
    echo "ISBN-13: $isbn13"; // will output "ISBN-13: 978-2-84344-949-9"
} catch(Exception $e) {
    echo "An parsing error occurred while attempting to format ISBN";
}

Starting from 2.3.0, Isbn->format will now issue a deprecation warning:

Isbn->format is deprecated and will be removed in the future. Use Isbn::convertTo… methods instead.

The recommend way to do validation is now to use one of the Isbn::convertTo… methods introduced in 2.3.0. They do not require to instantiate an Isbn object and can be called statically. Each format handled by the library has its own convert method:

  • Isbn::convertToIsbn10(string $input): void
  • Isbn::convertToIsbn13(string $input): void
  • Isbn::convertToEan13(string $input): void
  • Isbn::convertToGtin14(string $input): void

As Isbn->format, they return the formatted ISBN as a string.

// New usage
try {
    $isbn13 = Isbn::convertToIsbn13("9782843449499");
    echo "ISBN-13 $isbn13"; // Will output "ISBN-13 : 978-2-84344-949-9"
} catch(Exception $e) {
    echo "An parsing error occurred while attempting to format ISBN";
}

Note that the Isbn::convertAs… methods also will throw an Exception if input is so badly formed that in cannot be parsed, but otherwise they will still attempt to format the input event is hyphen are misplaced or checksum character is incorrect.