From 1cf7bc169adbd6a16663bda9ca04f5d9e1239756 Mon Sep 17 00:00:00 2001 From: Rambo Date: Thu, 19 May 2016 19:14:18 +0800 Subject: [PATCH 1/2] Add defaultDecoder for parseResponse --- .gitignore | 2 ++ src/Curl/Curl.php | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/.gitignore b/.gitignore index d1502b0..f054ad8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ vendor/ composer.lock + +.idea/ diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index f4f8911..122186b 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -80,6 +80,7 @@ class Curl private $jsonPattern = '/^(?:application|text)\/(?:[a-z]+(?:[\.-][0-9a-z]+){0,}[\+\.]|x-)?json(?:-[a-z]+)?/i'; private $xmlDecoder = null; private $xmlPattern = '~^(?:text/|application/(?:atom\+|rss\+)?)xml~i'; + private $defaultDecoder = null; /** * Construct @@ -99,6 +100,7 @@ class Curl $this->setDefaultUserAgent(); $this->setDefaultJsonDecoder(); $this->setDefaultXmlDecoder(); + $this->setDefaultDecoder(); $this->setDefaultTimeout(); $this->setOpt(CURLINFO_HEADER_OUT, true); $this->setOpt(CURLOPT_HEADERFUNCTION, array($this, 'headerCallback')); @@ -752,6 +754,25 @@ class Curl }; } + /** + * Set Default Decoder + * + * @access public + * @param $decoder string|callable + */ + public function setDefaultDecoder($decoder = 'json') + { + if (is_callable($decoder)) { + $this->defaultDecoder = $decoder; + } else { + if ($decoder === 'json') { + $this->defaultDecoder = $this->jsonDecoder; + } elseif ($decoder === 'xml') { + $this->defaultDecoder = $this->xmlDecoder; + } + } + } + /** * Set Default Timeout * @@ -1036,6 +1057,11 @@ class Curl if (is_callable($xml_decoder)) { $response = $xml_decoder($response); } + } else { + $decoder = $this->defaultDecoder; + if (is_callable($decoder)) { + $response = $decoder($response); + } } } From ca6e9ba81210595928868bbea45919a620541b77 Mon Sep 17 00:00:00 2001 From: Rambo Date: Thu, 19 May 2016 19:42:01 +0800 Subject: [PATCH 2/2] Add defaultDecoder for parseResponse --- src/Curl/Curl.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 122186b..940af11 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -100,7 +100,6 @@ class Curl $this->setDefaultUserAgent(); $this->setDefaultJsonDecoder(); $this->setDefaultXmlDecoder(); - $this->setDefaultDecoder(); $this->setDefaultTimeout(); $this->setOpt(CURLINFO_HEADER_OUT, true); $this->setOpt(CURLOPT_HEADERFUNCTION, array($this, 'headerCallback'));