Skip to content
Merged

1.0 #18

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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"ext-libxml": "*",
"ext-simplexml": "*",
"pcrov/jsonreader": "^1.0",
"dmt-software/xml-parser": "^0.3"
"dmt-software/xml-parser": "^0.4"
},
"autoload": {
"psr-4": {
Expand Down
11 changes: 10 additions & 1 deletion src/Handlers/FilePointers/XmlPathFilePointer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,23 @@ public function seek($reader, int $skip): void
}

$paths = preg_split('~/~', $this->path, -1, PREG_SPLIT_NO_EMPTY);
$depth = 0;
$stack = [];

try {
while ($node = $reader->parse()) {
if ($node instanceof Text) {
continue;
}
$stack[$node->depth() - 1] = $node->localName;

if ($depth >= $node->depth()) {
array_pop($stack);
}

$depth = $node->depth() - 1;
if ($depth <= count($paths)) {
$stack[$depth] = $node->localName;
}
if ($paths == $stack) {
break;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Handlers/HandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ private function getDefaultInitializeHandlerCallbacks(): array
return new CsvReaderHandler($fileHandler, ...$sanitizers);
},
XmlReaderHandler::class => function (string $file, array $config, array $sanitizers): HandlerInterface {
$pointer = new XmlPathFilePointer($config['path'] ?? '');
$encoding = $config['encoding'] ?? 'UTF-8';
settype($encoding, 'array');

$pointer = new XmlPathFilePointer($config['path'] ?? '');
$fileHandler = new Parser(
new Tokenizer(
new FileParser($file),
$config['encoding'] ?? null,
current($encoding),
$config['flags'] ?? 0
)
);
Expand Down
7 changes: 5 additions & 2 deletions src/ReaderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ public function buildToObjectReader(string $file, array $options, SerializerInte
* path : the path (from root) of the objects or elements to read
* - xml -> a xpath like structure of local element names (root/child/element)
* - json -> a dotted path to the objects (root.elements)
* flags : a bitmask of (xml or json) options
* flags : a bitmask of (xml or json) options, possible options:
* - Reader::JSON_FLOATS_AS_STRINGS
* - Reader::XML_DROP_NAMESPACES
* - Reader::XML_USE_CDATA
*
* trim : array with chars and direction
* encoding : the encoding of the file (when not utf-8)
Expand All @@ -162,7 +165,7 @@ public function createHandler(string $file, array $options): HandlerInterface
$sanitizers = $this->getSanitizersFromOptions($options, ['encoding']);
break;
case XmlReaderHandler::class:
$sanitizers = $this->getSanitizersFromOptions($options, ['encoding', 'trim']);
$sanitizers = $this->getSanitizersFromOptions($options, ['trim']);
break;
default:
$sanitizers = $this->getSanitizersFromOptions($options);
Expand Down
7 changes: 7 additions & 0 deletions src/ReaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

use Closure;
use DMT\Import\Reader\Exceptions\ReaderReadException;
use DMT\XmlParser\Parser;
use DMT\XmlParser\Tokenizer;
use Iterator;
use pcrov\JsonReader\JsonReader;

interface ReaderInterface
{
public const JSON_FLOATS_AS_STRINGS = JsonReader::FLOATS_AS_STRINGS;
public const XML_DROP_NAMESPACES = Tokenizer::XML_DROP_NAMESPACES;
public const XML_USE_CDATA = Tokenizer::XML_USE_CDATA;

/**
* Read through a file.
*
Expand Down