Skip to content

Commit dca0a39

Browse files
committed
fix: enhance XML attribute handling by converting values to strings for compatibility with Oracle resource types.
1 parent 8723e32 commit dca0a39

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

tests/TestCase.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,16 @@ protected function buildFlatXMLDataSet(array $dataSet): string
129129

130130
foreach ($dataSet as $item) {
131131
$treeElement = $xml->addChild($item['type']);
132-
$treeElement?->addAttribute('id', (string) $item['id']);
132+
$treeElement?->addAttribute('id', $this->convertToString($item['id']));
133133

134134
if ($item['type'] === 'multiple_tree') {
135-
$treeElement?->addAttribute('tree', (string) $item['tree']);
135+
$treeElement?->addAttribute('tree', $this->convertToString($item['tree']));
136136
}
137137

138-
$treeElement?->addAttribute('lft', (string) $item['lft']);
139-
$treeElement?->addAttribute('rgt', (string) $item['rgt']);
140-
$treeElement?->addAttribute('depth', (string) $item['depth']);
141-
$treeElement?->addAttribute('name', $item['name']);
138+
$treeElement?->addAttribute('lft', $this->convertToString($item['lft']));
139+
$treeElement?->addAttribute('rgt', $this->convertToString($item['rgt']));
140+
$treeElement?->addAttribute('depth', $this->convertToString($item['depth']));
141+
$treeElement?->addAttribute('name', $this->convertToString($item['name']));
142142
}
143143

144144
$dom = dom_import_simplexml($xml)->ownerDocument;
@@ -166,6 +166,36 @@ protected function buildFlatXMLDataSet(array $dataSet): string
166166
);
167167
}
168168

169+
/**
170+
* Converts a value to string, handling Oracle resource types correctly.
171+
*
172+
* Oracle database may return numeric values as resource types when using `asArray()` with {@see ActiveRecord}.
173+
*
174+
* This method properly converts those resources to strings for use with {@see SimpleXMLElement::addAttribute()}.
175+
*
176+
* @param int|string|resource|null $value The value to convert to string
177+
*
178+
* @return string The converted string value
179+
*/
180+
protected function convertToString($value): string
181+
{
182+
if (is_resource($value)) {
183+
$content = stream_get_contents($value);
184+
185+
if (is_string($content)) {
186+
return trim($content);
187+
}
188+
189+
return '';
190+
}
191+
192+
if ($value === null) {
193+
return '';
194+
}
195+
196+
return (string) $value;
197+
}
198+
169199
protected function createDatabase(): void
170200
{
171201
$command = $this->getDb()->createCommand();

0 commit comments

Comments
 (0)