Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Xml/Reader/DocumentToLookupArrayReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use DOMNode;
use Soap\Encoding\Xml\Node\Element;
use Soap\Encoding\Xml\Node\ElementList;
use VeeWee\Xml\Xmlns\Xmlns;
use function VeeWee\Xml\Dom\Predicate\is_element;

/**
* @psalm-type LookupArray = array<string, string|Element|ElementList>
* @psalm-type LookupArrayValue = string|Element|ElementList
* @psalm-type LookupArray = array<string, LookupArrayValue>
*/
final class DocumentToLookupArrayReader
{
Expand All @@ -20,7 +22,7 @@ final class DocumentToLookupArrayReader
public function __invoke(Element $xml): array
{
$root = $xml->element();
/** @var array<string, string|Element|ElementList> $nodes */
/** @var LookupArray $nodes */
$nodes = [];

// Read all child elements.
Expand All @@ -38,7 +40,7 @@ public function __invoke(Element $xml): array
$currentElement = Element::fromDOMElement($element);

// Incrementally build up lists.
/** @var string|Element|ElementList $value */
/** @var LookupArrayValue $value */
$value = match(true) {
$previousValue instanceof ElementList => $previousValue->append($currentElement),
$previousValue instanceof Element => new ElementList($previousValue, $currentElement),
Expand All @@ -50,8 +52,8 @@ public function __invoke(Element $xml): array

// It might be possible that the child is a regular textNode.
// In that case, we use '_' as the key and the value of the textNode as value.
if (!$nodes && $content = trim($root->textContent)) {
$nodes['_'] = $content;
if (!$nodes && $root->getAttributeNS(Xmlns::xsi()->value(), 'nil') !== 'true') {
$nodes['_'] = $root->textContent;
}

// All attributes also need to be added as key => value pairs.
Expand Down
49 changes: 49 additions & 0 deletions tests/PhpCompatibility/Schema062Variant1Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);

namespace Soap\Encoding\Test\PhpCompatibility;

use PHPUnit\Framework\Attributes\CoversClass;
use Soap\Encoding\Decoder;
use Soap\Encoding\Driver;
use Soap\Encoding\Encoder;

#[CoversClass(Driver::class)]
#[CoversClass(Encoder::class)]
#[CoversClass(Decoder::class)]
final class Schema062Variant1Test extends AbstractCompatibilityTests
{
protected string $schema = <<<EOXML
<complexType name="testType">
<simpleContent>
<restriction base="string">
<attribute name="int" type="int"/>
</restriction>
</simpleContent>
</complexType>
EOXML;
protected string $type = 'type="testType"';

protected function calculateParam(): mixed
{
return (object)[
'_' => '',
'int' => 123
];
}

protected function expectXml(): string
{
return <<<XML
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://test-uri/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<tns:test>
<testParam int="123" xsi:type="tns:testType"/>
</tns:test>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
XML;
}
}
49 changes: 49 additions & 0 deletions tests/PhpCompatibility/Schema062Variant2Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);

namespace Soap\Encoding\Test\PhpCompatibility;

use PHPUnit\Framework\Attributes\CoversClass;
use Soap\Encoding\Decoder;
use Soap\Encoding\Driver;
use Soap\Encoding\Encoder;

#[CoversClass(Driver::class)]
#[CoversClass(Encoder::class)]
#[CoversClass(Decoder::class)]
final class Schema062Variant2Test extends AbstractCompatibilityTests
{
protected string $schema = <<<EOXML
<complexType name="testType">
<simpleContent>
<restriction base="string">
<attribute name="int" type="int"/>
</restriction>
</simpleContent>
</complexType>
EOXML;
protected string $type = 'type="testType"';

protected function calculateParam(): mixed
{
return (object)[
'_' => null,
'int' => 123
];
}

protected function expectXml(): string
{
return <<<XML
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://test-uri/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<tns:test>
<testParam xsi:nil="true" int="123" xsi:type="tns:testType"/>
</tns:test>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
XML;
}
}