Merge branch 'master' of github.com-zachborboa:zachborboa/php-curl-class

* 'master' of github.com-zachborboa:zachborboa/php-curl-class:
  Add defaultDecoder for parseResponse
  Add defaultDecoder for parseResponse
This commit is contained in:
Zach Borboa
2016-07-11 01:09:21 -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
@@ -79,6 +79,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;
private static $deferredProperties = array(
'effectiveUrl',
@@ -766,6 +767,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
*
@@ -1080,6 +1100,11 @@ class Curl
if (is_callable($xml_decoder)) {
$response = $xml_decoder($response);
}
} else {
$decoder = $this->defaultDecoder;
if (is_callable($decoder)) {
$response = $decoder($response);
}
}
}