diff --git a/2014-03-20/src/Book.php b/2014-03-20/src/Book.php index 63e2e76..7ba7ab5 100644 --- a/2014-03-20/src/Book.php +++ b/2014-03-20/src/Book.php @@ -1,4 +1,34 @@ setTimestamp($data['publishDate']); + + $data['publishDate'] = $dateTime; + } + + $this->_data = $data; + } + + public function __call($name, array $args) + { + $name = str_replace('get', '', $name); + + if (strlen($name) < 1) { + return null; + } + + $field = strtolower($name[0]) . substr($name, 1); + + if (!isset($this->_data[$field])) { + return null; + } + + return $this->_data[$field]; + } } diff --git a/2014-03-20/src/BooksCollection.php b/2014-03-20/src/BooksCollection.php index 1d78826..5773645 100644 --- a/2014-03-20/src/BooksCollection.php +++ b/2014-03-20/src/BooksCollection.php @@ -1,4 +1,124 @@ _param = []; + $this->_data = []; + $this->_sort = ''; + + return $this; + } + + public function where(array $where) + { + $this->_param = $where; + + return $this; + } + + public function sort($field) + { + $this->_sort = $field; + + $this->_data = $this->_callApi($this->_param, $this->_sort); + + return $this; + } + + public function rewind() + { + $this->_position = 0; + } + + public function current() + { + return new Book($this->_data[$this->_position]); + } + + public function key() + { + return $this->_position; + } + + public function next() + { + ++$this->_position; + } + + public function valid() + { + return array_key_exists($this->_position, $this->_data); + } + + public function count() + { + return count($this->_data); + } + + /** + * Returns array of books based on parameter and sort + * + * @see /src/Book.php + * + * @param array $param parameter to pass to book api + * @param string $sort sort value of title, genre, or price + * @return array of Book + */ + private function _callApi(array $param = [], $sort = '') + { + Util::throwIfNotType(['string' => [$sort]], true); + + Util::ensure(true, in_array($sort, ['title', 'genre', 'price'])); + + $apiParam = ['sort' => $sort]; + Arrays::copyIfKeysExist($param, $apiParam, ['genre', 'offset', 'limit']); + + $ret = []; + do { + $request = json_decode(file_get_contents('http://chadicus.herokuapp.com/books?' . Http::buildQueryString($apiParam)), true); + + if (!isset($request['total'])) { + $request['total'] = 0; + continue; + } + + foreach ($request['books'] as $book) { + $ret[] = $this->_getBookInfo($book['url']); + } + } while ($request['total'] != count($ret)); + + return $ret; + } + + private function _getBookInfo($urlPath) + { + Util::throwIfNotType(['string' => [$urlPath]]); + + return json_decode(file_get_contents("http://chadicus.herokuapp.com{$urlPath}"), true); + } }