Skip to content

Commit dd58dee

Browse files
committed
Update docs
1 parent 3fb8ce5 commit dd58dee

File tree

10 files changed

+207
-128
lines changed

10 files changed

+207
-128
lines changed

docs/dom.md

+114-120
Large diffs are not rendered by default.

docs/xsd.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use VeeWee\Xml\Xsd\Schema\Manipulator;
1010
use function VeeWee\Xml\Dom\Locator\Xsd\locate_all_xsd_schemas;
1111

1212
$doc = Document::fromXmlFile('some.xml');
13-
$schemas = locate_all_xsd_schemas($doc->toUnsafeDocument())
13+
$schemas = $doc->map(locate_all_xsd_schemas(...))
1414
->manipulate(Manipulator\base_path('/var/www'))
1515
->manipulate(Manipulator\overwrite_with_local_files([
1616
'http://www.w3.org/2001/XMLSchema' => '/local/XMLSchema.xsd'

psalm.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
errorLevel="1"
44
resolveFromConfigFile="true"
55
strictBinaryOperands="true"
6-
phpVersion="8.1"
6+
phpVersion="8.3"
77
allowStringToStandInForClass="true"
88
rememberPropertyAssignmentsAfterCall="false"
99
skipChecksOnUnresolvableIncludes="false"

src/Xml/Dom/Configurator/loader.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace VeeWee\Xml\Dom\Configurator;
6+
7+
use Closure;
8+
use VeeWee\Xml\Dom\Document;
9+
10+
/**
11+
* @param callable(): \Dom\XMLDocument $loader
12+
*
13+
* @return Closure(\Dom\XMLDocument): \Dom\XMLDocument
14+
*/
15+
function loader(callable $loader): Closure
16+
{
17+
return static function () use ($loader): \Dom\XMLDocument {
18+
return Document::fromLoader($loader)->toUnsafeDocument();
19+
};
20+
}

src/Xml/Dom/Loader/xml_file_loader.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
* @param int $options - bitmask of LIBXML_* constants https://www.php.net/manual/en/libxml.constants.php
1414
* @return Closure(): XMLDocument
1515
*/
16-
function xml_file_loader(string $file, int $options = 0): Closure
16+
function xml_file_loader(string $file, int $options = 0, ?string $override_encoding = null): Closure
1717
{
18-
return static fn () => disallow_issues(static function () use ($file, $options): XMLDocument {
18+
return static fn () => disallow_issues(static function () use ($file, $options, $override_encoding): XMLDocument {
1919
Assert::fileExists($file);
2020

21-
return XMLDocument::createFromFile($file, $options);
21+
return XMLDocument::createFromFile($file, $options, $override_encoding);
2222
});
2323
}

src/Xml/Dom/Loader/xml_string_loader.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
* @param int $options - bitmask of LIBXML_* constants https://www.php.net/manual/en/libxml.constants.php
1414
* @return Closure(): XMLDocument
1515
*/
16-
function xml_string_loader(string $xml, int $options = 0): Closure
16+
function xml_string_loader(string $xml, int $options = 0, ?string $override_encoding = null): Closure
1717
{
18-
return static fn () => disallow_issues(static function () use ($xml, $options): XMLDocument {
19-
return XMLDocument::createFromString($xml, $options);
18+
return static fn () => disallow_issues(static function () use ($xml, $options, $override_encoding): XMLDocument {
19+
return XMLDocument::createFromString($xml, $options, $override_encoding);
2020
});
2121
}

src/bootstrap.php

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
'Xml\Dom\Configurator\canonicalize' => __DIR__.'/Xml/Dom/Configurator/canonicalize.php',
2525
'Xml\Dom\Configurator\comparable' => __DIR__.'/Xml/Dom/Configurator/comparable.php',
2626
'Xml\Dom\Configurator\document_uri' => __DIR__.'/Xml/Dom/Configurator/document_uri.php',
27+
'Xml\Dom\Configurator\loader' => __DIR__.'/Xml/Dom/Configurator/loader.php',
2728
'Xml\Dom\Configurator\normalize' => __DIR__.'/Xml/Dom/Configurator/normalize.php',
2829
'Xml\Dom\Configurator\optimize_namespaces' => __DIR__.'/Xml/Dom/Configurator/optimize_namespaces.php',
2930
'Xml\Dom\Configurator\pretty_print' => __DIR__.'/Xml/Dom/Configurator/pretty_print.php',
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace VeeWee\Tests\Xml\Dom\Configurator;
6+
7+
use Exception;
8+
use PHPUnit\Framework\TestCase;
9+
use VeeWee\Xml\Dom\Document;
10+
use function VeeWee\Xml\Dom\Configurator\loader;
11+
12+
final class LoaderTest extends TestCase
13+
{
14+
public function test_it_can_load_xml(): void
15+
{
16+
$doc = \Dom\XMLDocument::createEmpty();
17+
$xml = '<hello />';
18+
19+
$loader = loader(static function () use ($xml): \Dom\XMLDocument {
20+
return Document::fromXmlString($xml)->toUnsafeDocument();
21+
});
22+
23+
$result = $loader($doc);
24+
static::assertNotSame($doc, $result);
25+
static::assertXmlStringEqualsXmlString($xml, $result->saveXML());
26+
}
27+
28+
29+
public function test_it_can_mark_xml_loading_as_failed(): void
30+
{
31+
$doc = \Dom\XMLDocument::createEmpty();
32+
$exception = new Exception('Could not load the XML document');
33+
$loader = loader(static function () use ($exception): never {
34+
throw $exception;
35+
});
36+
37+
$this->expectExceptionObject($exception);
38+
$loader($doc);
39+
}
40+
}

tests/Xml/Dom/Loader/XmlFileLoaderTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use VeeWee\Tests\Xml\Helper\FillFileTrait;
1010
use VeeWee\Xml\Exception\RuntimeException;
1111
use function VeeWee\Xml\Dom\Loader\xml_file_loader;
12+
use function VeeWee\Xml\Dom\Loader\xml_string_loader;
1213

1314
final class XmlFileLoaderTest extends TestCase
1415
{
@@ -60,4 +61,17 @@ public function test_it_throws_exception_on_invalid_file(): void
6061

6162
$loader();
6263
}
64+
65+
public function test_it_can_override_charset(): void
66+
{
67+
$xml = '<?xml version="1.0" encoding="UTF-8"?><hello>héllo</hello>';
68+
[$file, $handle] = $this->fillFile($xml);
69+
$loader = xml_file_loader($file, override_encoding: 'Windows-1252');
70+
71+
$doc = $loader();
72+
fclose($handle);
73+
74+
static::assertSame('héllo', $doc->documentElement->textContent);
75+
static::assertSame('Windows-1252', $doc->xmlEncoding);
76+
}
6377
}

tests/Xml/Dom/Loader/XmlStringLoaderTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,14 @@ public function test_it_can_load_with_options(): void
3939

4040
static::assertSame('<hello>HELLO</hello>', $doc->saveXML($doc->documentElement));
4141
}
42+
43+
public function test_it_can_override_charset(): void
44+
{
45+
$xml = '<?xml version="1.0" encoding="UTF-8"?><hello>héllo</hello>';
46+
$loader = xml_string_loader($xml, override_encoding: 'Windows-1252');
47+
$doc = $loader();
48+
49+
static::assertSame('héllo', $doc->documentElement->textContent);
50+
static::assertSame('Windows-1252', $doc->xmlEncoding);
51+
}
4252
}

0 commit comments

Comments
 (0)