Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
PHP
<?php

class GetBookList {

	private $format;

	public function __construct($format = 'json')
	{
		$this->format = $format;
	}

	public function getBooksByAuthor($authorName, $limit = 10)
	{
		$return = [];

		$curl = curl_init();

		curl_setopt($curl, CURLOPT_URL, "http://api.book-seller-example.com/by-author?q=" . $authorName . '&limit=' . $limit . '&format=' . $this->format);
		$output = curl_exec($curl);
		curl_close($curl);

		if($this->format == 'json') {
			$json = json_decode($output);

			foreach ($json as $result) {
				$return[] = [
					'title'    => $result->book->title,
					'author'   => $result->book->author,
					'isbn'     => $result->book->isbn,
					'quantity' => $result->stock->level,
					'price'    => $result->stock->price,
				];
			}
		}elseif($this->format == 'xml') {
			$xml = new SimpleXMLElement($output);

			foreach ($xml as $result) {
				$return[] = [
					'title'    => $result->book['name'],
					'author'   => $result->book['author_name'],
					'isbn'     => $result->book['isbn_number'],
					'quantity' => $result->book->stock['number'],
					'price'    => $result->book->stock['unit_price'],
				];
			}
		}

		return $return;
	}
}


What I have tried:

PHP
<?php


class GetBookList {

	private $format;

	public function __construct($format = 'json')
	{
		$this->format = $format;
	}

	public function getBooksByAuthor($authorName, $limit = 10)
	{
		$return = [];

		$curl = curl_init();

		curl_setopt($curl, CURLOPT_URL, "http://api.book-seller-example.com/by-author?q=" . $authorName . '&limit=' . $limit . '&format=' . $this->format);
		$output = curl_exec($curl);
		curl_close($curl);

		if($this->format == 'json') {
            $return = $this->jsonFormat($output);
		}elseif($this->format == 'xml') {
            $return = $this->xmlFormat($output);
		}

		return $return;
	}

    public function jsonFormat($output)
    {
        $return = [];
        $json = json_decode($output);

        foreach ($json as $result) {
            $return[] = [
                'title' => $result->book->title,
                'author' => $result->book->author,
                'isbn' => $result->book->isbn,
                'quantity' => $result->stock->level,
                'price' => $result->stock->price,
            ];
        }
        return $return;
    }

    public function xmlFormat($output)
    {
        $return = [];
        $xml = new SimpleXMLElement($output);

        foreach ($xml as $result) {
            $return[] = [
                'title' => $result->book['name'],
                'author' => $result->book['author_name'],
                'isbn' => $result->book['isbn_number'],
                'quantity' => $result->book->stock['number'],
                'price' => $result->book->stock['unit_price'],
            ];
        }
        return $return;
    }
}
Posted
Comments
Richard MacCutchan 11-Oct-18 7:07am    
To what end?
Member 14015905 11-Oct-18 7:56am    
To improve and optimize the code.
Richard MacCutchan 11-Oct-18 7:59am    
Well that is hardly a technical question with a quick answer. You first need to analyse the code with respect to what you want it to do, and what it actually does. Where are the areas that are not working in the best way, and why?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900