Merge branch 'master' of git://github.com/zuoRambo/php-curl-class into zuoRambo-master

* 'master' of git://github.com/zuoRambo/php-curl-class:
  Add defaultDecoder for parseResponse
  Add defaultDecoder for parseResponse
This commit is contained in:
Zach Borboa
2016-07-10 22:49:23 -07:00
2 changed files with 27 additions and 0 deletions
+2
View File
@@ -1,2 +1,4 @@
vendor/
composer.lock
.idea/
+25
View File
@@ -81,6 +81,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
@@ -754,6 +755,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
*
@@ -1041,6 +1061,11 @@ class Curl
if (is_callable($xml_decoder)) {
$response = $xml_decoder($response);
}
} else {
$decoder = $this->defaultDecoder;
if (is_callable($decoder)) {
$response = $decoder($response);
}
}
}