Allow specifying xml decoder options when calling Curl::setDefaultXmlDecoder

This commit is contained in:
Zach Borboa
2018-06-18 23:08:26 -07:00
parent e087a5c117
commit 2bb151e03d
3 changed files with 45 additions and 4 deletions
+12 -1
View File
@@ -55,6 +55,7 @@ class Curl
private $jsonDecoderArgs = array();
private $jsonPattern = '/^(?:application|text)\/(?:[a-z]+(?:[\.-][0-9a-z]+){0,}[\+\.]|x-)?json(?:-[a-z]+)?/i';
private $xmlDecoderArgs = array();
private $xmlPattern = '~^(?:text/|application/(?:atom\+|rss\+|soap\+)?)xml~i';
private $defaultDecoder = null;
@@ -206,6 +207,7 @@ class Curl
$this->jsonDecoder = null;
$this->jsonDecoderArgs = null;
$this->xmlDecoder = null;
$this->xmlDecoderArgs = null;
$this->defaultDecoder = null;
}
@@ -855,10 +857,15 @@ class Curl
* Set Default XML Decoder
*
* @access public
* @param $class_name
* @param $options
* @param $ns
* @param $is_prefix
*/
public function setDefaultXmlDecoder()
{
$this->xmlDecoder = '\Curl\Decoder::decodeXml';
$this->xmlDecoderArgs = func_get_args();
}
/**
@@ -973,8 +980,10 @@ class Curl
{
if ($mixed === false) {
$this->xmlDecoder = false;
$this->xmlDecoderArgs = array();
} elseif (is_callable($mixed)) {
$this->xmlDecoder = $mixed;
$this->xmlDecoderArgs = array();
}
}
@@ -1418,7 +1427,9 @@ class Curl
}
} elseif (preg_match($this->xmlPattern, $response_headers['Content-Type'])) {
if ($this->xmlDecoder) {
$response = call_user_func($this->xmlDecoder, $response);
$args = $this->xmlDecoderArgs;
array_unshift($args, $response);
$response = call_user_func_array($this->xmlDecoder, $args);
}
} else {
if ($this->defaultDecoder) {
+8 -3
View File
@@ -35,11 +35,16 @@ class Decoder
* Decode XML
*
* @access public
* @param $response
* @param $data
* @param $class_name
* @param $options
* @param $ns
* @param $is_prefix
*/
public static function decodeXml($response)
public static function decodeXml()
{
$xml_obj = @simplexml_load_string($response);
$args = func_get_args();
$xml_obj = @call_user_func_array('simplexml_load_string', $args);
if (!($xml_obj === false)) {
$response = $xml_obj;
}
+25
View File
@@ -2574,6 +2574,31 @@ class CurlTest extends \PHPUnit\Framework\TestCase
}
}
public function testXmlDecoderOptions()
{
// Implicit default xml decoder should return object.
$test = new Test();
$test->server('xml_with_cdata_response', 'GET');
$this->assertTrue(is_object($test->curl->response));
$this->assertFalse(strpos($test->curl->response->saveXML(), '<![CDATA[') === false);
// Explicit default xml decoder should return object.
$test = new Test();
$test->curl->setDefaultXmlDecoder();
$test->server('xml_with_cdata_response', 'GET');
$this->assertTrue(is_object($test->curl->response));
$this->assertFalse(strpos($test->curl->response->saveXML(), '<![CDATA[') === false);
// Explicit default xml decoder with options should return value using options as specified.
$class_name = 'SimpleXMLElement';
$options = LIBXML_NOCDATA;
$test = new Test();
$test->curl->setDefaultXmlDecoder($class_name, $options);
$test->server('xml_with_cdata_response', 'GET');
$this->assertTrue(is_object($test->curl->response));
$this->assertTrue(strpos($test->curl->response->saveXML(), '<![CDATA[') === false);
}
public function testXmlContentTypeDetection()
{
$xml_content_types = array(