Skip to content

Commit

Permalink
refs BooksService works
Browse files Browse the repository at this point in the history
  • Loading branch information
desarrolla2 committed Apr 28, 2012
1 parent ceb1bdf commit dd0e6bf
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 7 deletions.
9 changes: 8 additions & 1 deletion yii-app/protected/models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

class Book
{


public $google_link = null;
public $tittle = null;
public $description = null;
public $author = null;
public $category = null;

}
39 changes: 38 additions & 1 deletion yii-app/protected/models/BooksCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,44 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

class BooksCollection
{
//put your code here

/**
*
* @var array
*/
private $books = array();

/**
* Add book to collection
*
* @param Book $book
*/
public function add(Book $book)
{
array_push($this->books, $book);
}

/**
* Retrieve books Collection as array
*
* @return array
*/
public function toArray()
{
return $this->books;
}

/**
* Retrieve number of items in collection
*
* @return int
*/
public function count()
{
return count($this->books);
}

}
37 changes: 33 additions & 4 deletions yii-app/protected/models/BooksService.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

require_once(dirname(__FILE__) . '/BooksCollection.php');
require_once(dirname(__FILE__) . '/Book.php');
/*
* This file is part of the Instabook package.
*
Expand All @@ -16,14 +17,42 @@
class BooksService
{

private $goole_rest_api_search = null;
private $collection = null;

public function __construct()
{
;
$this->goole_rest_api_search = 'https://www.googleapis.com/books/v1/volumes';
$this->collection = new BooksCollection();
}

public function search()
public function search($query)
{

$result = json_decode(file_get_contents($this->goole_rest_api_search . '?q=' . urlencode($query)));
foreach ($result->items as $item)
{
$book = new Book();
if (isset($item->selfLink)){
$book->google_link = $item->selfLink;
}
if (isset($item->volumeInfo->title)){
$book->tittle = $item->volumeInfo->title;
}
if (isset($item->volumeInfo->description)){
$book->description = $item->volumeInfo->description;
}
if (isset($item->volumeInfo->authors[0])){
$book->author = $item->volumeInfo->authors[0];
}
if (isset($item->industryIdentifiers->categories[0])){
$book->category = $item->industryIdentifiers->categories[0];
}


$this->collection->add($book);
}

return $this->collection;
}

}
7 changes: 6 additions & 1 deletion yii-app/protected/tests/books/service.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<?php
require_once(dirname(__FILE__) . '/../../models/BooksService.php');
$query = "cloud atlas";

require_once(dirname(__FILE__) .'/../../models/Book.php');
$service = new BooksService();
$books = $service->search($query);

die(var_dump($books));

0 comments on commit dd0e6bf

Please sign in to comment.