Skip to content

Commit bb0397a

Browse files
authored
Add configurable charencoding property with getter/setter methods (#144)
1 parent 1f9b534 commit bb0397a

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/nusoap.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,29 @@ function setError($str)
396396
$this->error_str = $str;
397397
}
398398

399+
/**
400+
* gets the charencoding setting that controls whether special characters are encoded as XML entities
401+
*
402+
* @return boolean
403+
* @access public
404+
*/
405+
function getCharencoding()
406+
{
407+
return $this->charencoding;
408+
}
409+
410+
/**
411+
* sets the charencoding setting
412+
*
413+
* @param boolean $charencoding Whether to encode special characters as XML entities in expandEntities()
414+
* @return void
415+
* @access public
416+
*/
417+
function setCharencoding($charencoding)
418+
{
419+
$this->charencoding = $charencoding;
420+
}
421+
399422
/**
400423
* detect if array is a simple array or a struct (associative array)
401424
*
@@ -7476,6 +7499,8 @@ function __construct($endpoint, $wsdl = false, $proxyhost = false, $proxyport =
74767499
$this->endpoint = $this->wsdl->wsdl;
74777500
$this->wsdlFile = $this->endpoint;
74787501
$this->debug('existing wsdl instance created from ' . $this->endpoint);
7502+
// propagate charencoding to wsdl
7503+
$this->wsdl->setCharencoding($this->charencoding);
74797504
$this->checkWSDL();
74807505
} else {
74817506
$this->wsdlFile = $this->endpoint;
@@ -7770,6 +7795,8 @@ function loadWSDL()
77707795
$this->debug('instantiating wsdl class with doc: ' . $this->wsdlFile);
77717796
$this->wsdl = new wsdl('', $this->proxyhost, $this->proxyport, $this->proxyusername, $this->proxypassword, $this->timeout, $this->response_timeout, $this->curl_options, $this->use_curl);
77727797
$this->wsdl->setCredentials($this->username, $this->password, $this->authtype, $this->certRequest);
7798+
// propagate charencoding to wsdl
7799+
$this->wsdl->setCharencoding($this->charencoding);
77737800
$this->wsdl->fetchWSDL($this->wsdlFile);
77747801
$this->checkWSDL();
77757802
}

tests/Cases/NusoapBaseTest.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,36 @@ Toolkit::test(static function (): void {
106106
Assert::same('hello & world', $base->expandEntities('hello & world'));
107107
});
108108

109+
// Test charencoding getter/setter
110+
Toolkit::test(static function (): void {
111+
$base = new nusoap_base();
112+
113+
// Default should be true
114+
Assert::true($base->getCharencoding());
115+
116+
// Test setting to false
117+
$base->setCharencoding(false);
118+
Assert::false($base->getCharencoding());
119+
120+
// Test setting back to true
121+
$base->setCharencoding(true);
122+
Assert::true($base->getCharencoding());
123+
});
124+
125+
// Test expandEntities with charencoding disabled
126+
Toolkit::test(static function (): void {
127+
$base = new nusoap_base();
128+
$base->setCharencoding(false);
129+
130+
// When charencoding is false, characters are not converted to XML entities (they remain as literal characters)
131+
Assert::same('&', $base->expandEntities('&'));
132+
Assert::same('<', $base->expandEntities('<'));
133+
Assert::same('>', $base->expandEntities('>'));
134+
Assert::same("'", $base->expandEntities("'"));
135+
Assert::same('"', $base->expandEntities('"'));
136+
Assert::same('hello & world', $base->expandEntities('hello & world'));
137+
});
138+
109139
// Test isArraySimpleOrStruct
110140
Toolkit::test(static function (): void {
111141
$base = new nusoap_base();

0 commit comments

Comments
 (0)