我该如何重构这段代码? [英] How can I refactor this code?

查看:77
本文介绍了我该如何重构这段代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?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


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;
    }
}

推荐答案

format ;

公共函数__construct(
format; public function __construct(


format = ' json'
{
format = 'json') {


this-> format =
this->format =


这篇关于我该如何重构这段代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆