Skip to content

Commit b3de9d6

Browse files
committed
Update docs
1 parent 3fb8ce5 commit b3de9d6

File tree

12 files changed

+215
-138
lines changed

12 files changed

+215
-138
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'

src/Xml/Dom/Assert/assert_cdata.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
use function Psl\Type\instance_of;
1010

1111
/**
12-
* @psalm-assert \Dom\CDATASection $node
12+
* @psalm-assert \Dom\CdataSection $node
1313
* @throws AssertException
1414
*/
15-
function assert_cdata(mixed $node): \Dom\CDATASection
15+
function assert_cdata(mixed $node): \Dom\CdataSection
1616
{
17-
return instance_of(\Dom\CDATASection::class)->assert($node);
17+
return instance_of(\Dom\CdataSection::class)->assert($node);
1818
}

src/Xml/Dom/Builder/cdata.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@
55
namespace VeeWee\Xml\Dom\Builder;
66

77
use Closure;
8-
use \Dom\CDATASection;
8+
use \Dom\CdataSection;
99
use \Dom\Node;
1010
use function VeeWee\Xml\Dom\Assert\assert_cdata;
1111
use function VeeWee\Xml\Dom\Locator\Node\detect_document;
1212
use function VeeWee\Xml\Internal\configure;
1313

1414
/**
15-
* @param list<callable(\Dom\CDATASection): \Dom\CDATASection> $configurators
15+
* @param list<callable(\Dom\CdataSection): \Dom\CdataSection> $configurators
1616
*
17-
* @return Closure(\Dom\Node): \Dom\CDATASection
17+
* @return Closure(\Dom\Node): \Dom\CdataSection
1818
*/
1919
function cdata(string $data, ...$configurators): Closure
2020
{
21-
return static function (\Dom\Node $node) use ($data, $configurators): \Dom\CDATASection {
21+
return static function (\Dom\Node $node) use ($data, $configurators): \Dom\CdataSection {
2222
$document = detect_document($node);
2323

2424
return assert_cdata(
25-
configure(...$configurators)($document->createCDATASection($data))
25+
configure(...$configurators)($document->createCdataSection($data))
2626
);
2727
};
2828
}

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 ): void $loader
12+
*
13+
* @return Closure(\DOM\XMLDocument ): \DOM\XMLDocument
14+
*/
15+
function loader(callable $loader): Closure
16+
{
17+
return static function (\DOM\XMLDocument $document) 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/Xml/Dom/Predicate/is_cdata.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
namespace VeeWee\Xml\Dom\Predicate;
66

7-
use \Dom\CDATASection;
7+
use \Dom\CdataSection;
88
use \Dom\Node;
99

1010
/**
11-
* @psalm-assert-if-true \Dom\CDATASection $node
11+
* @psalm-assert-if-true \Dom\CdataSection $node
1212
*/
1313
function is_cdata(\Dom\Node $node): bool
1414
{
15-
return $node instanceof \Dom\CDATASection;
15+
return $node instanceof \Dom\CdataSection;
1616
}

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',
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 function VeeWee\Xml\Dom\Configurator\loader;
10+
11+
final class LoaderTest extends TestCase
12+
{
13+
public function test_it_can_load_xml(): void
14+
{
15+
$doc = new \Dom\XMLDocument();
16+
$xml = '<hello />';
17+
18+
$loader = loader(static function (\Dom\XMLDocument $doc) use ($xml): void {
19+
$doc->loadXML($xml);
20+
});
21+
22+
$result = $loader($doc);
23+
static::assertNotSame($doc, $result);
24+
static::assertXmlStringEqualsXmlString($xml, $result->saveXML());
25+
}
26+
27+
28+
public function test_it_can_mark_xml_loading_as_failed(): void
29+
{
30+
$doc = new \Dom\XMLDocument();
31+
$exception = new Exception('Could not load the XML document');
32+
$loader = loader(static function (\Dom\XMLDocument $doc) use ($exception): void {
33+
throw $exception;
34+
});
35+
36+
$this->expectExceptionObject($exception);
37+
$loader($doc);
38+
}
39+
}

tests/Xml/Dom/Loader/XmlFileLoaderTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,17 @@ public function test_it_throws_exception_on_invalid_file(): void
6060

6161
$loader();
6262
}
63+
64+
public function test_it_can_override_charset(): void
65+
{
66+
$xml = '<hello>héllo</hello>';
67+
[$file, $handle] = $this->fillFile($xml);
68+
$loader = xml_file_loader($file, override_encoding: 'ASCII');
69+
70+
$doc = $loader();
71+
fclose($handle);
72+
73+
static::assertSame('<hello>héllo</hello>', $doc->saveXML($doc->documentElement));
74+
static::assertSame('ASCII', $doc->encoding);
75+
}
6376
}

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 = '<hello>héllo</hello>';
46+
$loader = xml_string_loader($xml, override_encoding: 'ASCII');
47+
$doc = $loader();
48+
49+
static::assertSame('<hello>héllo</hello>', $doc->saveXML($doc->documentElement));
50+
static::assertSame('ASCII', $doc->encoding);
51+
}
4252
}

0 commit comments

Comments
 (0)