Skip to content

Instantly share code, notes, and snippets.

@eftakhairul
Created August 27, 2015 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eftakhairul/634f27ea84f8db63613a to your computer and use it in GitHub Desktop.
Save eftakhairul/634f27ea84f8db63613a to your computer and use it in GitHub Desktop.
Google Book Library based on ISBN
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Description of ISBA Library
*
* @package Library
* @author Jamael Tanveer Nayon <imjtnayon@gmail.com>
* @author Eftakhairul Islam <eftakhairul@gmail.com> http://eftakhairul.com
* @website Changebd.net
*/
class Isbn
{
/**
* @var string
*/
private $_isbn;
/**
* @var string
*/
protected $_isbnUrl;
/**
* @var array
*/
protected $_bookDetails;
/**
* Init value
*/
function __construct()
{
$this->_isbnUrl = 'https://www.googleapis.com/books/v1/volumes?key=AIzaSyDKepjfaVBRcgsnPALw5s2UNyfOk-1FHUU&country=bd&q=isbn:';
$this->_bookDetails = array();
}
/**
* @param null $isbn
* @return Isbn|null
*/
function fetchBookInfo($isbn = null)
{
if (!empty($isbn)) $this->_isbn = $isbn;
if (empty($this->_isbn)) return $this;
$booklist = json_decode( file_get_contents('https://www.googleapis.com/books/v1/volumes?key=AIzaSyDKepjfaVBRcgsnPALw5s2UNyfOk-1FHUU&country=bd&q=isbn:' . $isbn) );
if ($booklist->totalItems > 0) {
$book = json_decode(file_get_contents($booklist->items[0]->selfLink.'?key=AIzaSyDKepjfaVBRcgsnPALw5s2UNyfOk-1FHUU&country=bd'));
$bookInfo = $book->volumeInfo;
//Title
$titles = $bookInfo->title;
//Author list
$authors = 'Author is not available';
if (isset($bookInfo->authors)) {
$authors = '';
$authorList = $bookInfo->authors;
for ($i=0; $i < count($authorList); $i++) $authors.= $authorList[$i].', ';
$authors = rtrim($authors, ', ');
}
//Description
$descriptions = 'Description is not available';
if (isset($bookInfo->description)) $descriptions = $bookInfo->description;
//Category list
$categories = array();
$categoryList = $bookInfo->categories;
for ($i=0; $i < count($categoryList); $i++) array_push($categories, $categoryList[$i]);
//Image
$image = $bookInfo->imageLinks->thumbnail;
//ISBN
if (isset($bookInfo->industryIdentifiers[1])) {
$isbn = $bookInfo->industryIdentifiers[1]->identifier;
} else {
$isbn = $bookInfo->industryIdentifiers[0]->identifier;
}
$this->_bookDetails = array('title' => $titles,
'author' => $authors,
'description' => $descriptions,
'categories' => $categories,
'image' => $image,
'isbn' => $isbn,
);
}
return $this;
}
/**
* Get all value;
*
* @return array
*/
public function getAll()
{
return $this->_bookDetails;
}
/**
* Set the isbn
*
* @param $isbn
* @return Isbn|null
*/
public function setIsbn($isbn)
{
$this->_isbn = $isbn;
return $this;
}
/**
* get the ISBN
*
* @return mixed
*/
public function getIsbn()
{
return $this->_isbn;
}
/**
* Set the url
*
* @param $isbnUrl
* @return Isbn|null
*/
public function setIsbnUrl($isbnUrl)
{
$this->_isbnUrl = $isbnUrl;
}
/**
* Get the url
*
* @return string
*/
public function getIsbnUrl()
{
return $this->_isbnUrl;
}
/*
* Given an ISBN, returns true if it is valid
* false if not.
*/
function validateISBN($isbn)
{
$isbn = trim($isbn);
$isbn = str_replace("-", "", $isbn);
if (strlen($isbn) == 10) {
$sum = $isbn[0]*10 + $isbn[1]*9 + $isbn[2]*8 + $isbn[3]*7 + $isbn[4]*6 + $isbn[5]*5 + $isbn[6]*4 + $isbn[7]*3 + $isbn[8]*2;
$check_digit = $isbn[9];
$test_check = 11 - ($sum % 11);
if ($test_check == 10) $test_check = "X";
if ($test_check == $check_digit) {
return true;
} else {
return false;
}
}
else if (strlen($isbn) == 13) {
$sum = $isbn[0]*1 + $isbn[1]*3 + $isbn[2]*1 + $isbn[3]*3 + $isbn[4]*1 + $isbn[5]*3 + $isbn[6]*1 + $isbn[7]*3 + $isbn[8]*1 + $isbn[9]*3 + $isbn[10]*1 + $isbn[11]*3;
$check_digit = $isbn[12];
$test_check = 10 - ($sum % 10);
if ($test_check == 10) $test_check = 0;
if ($test_check == $check_digit) {
return true;
} else {
return false;
}
}
else return false;
}
/**
* Remove all unnecessary char form ISBN
*
* @param $isbnNum
* @return string
*/
private function filterISBN($isbnNum)
{
$isbnNum = trim($isbnNum);
preg_match_all('!\d+!', $isbnNum, $matches);
return implode("", $matches[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment