Skip to content

Commit

Permalink
[REFACTOR] Rename Loader Classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWeinert committed Oct 22, 2023
1 parent c5980e4 commit 46d55b0
Show file tree
Hide file tree
Showing 76 changed files with 893 additions and 818 deletions.
6 changes: 3 additions & 3 deletions examples/Json/Formats/JsonDOM.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
/*
* FluentDOM
*
* @link https://thomas.weinert.info/FluentDOM/
* @copyright Copyright 2009-2018 FluentDOM Contributors
* @copyright Copyright 2009-2023 FluentDOM Contributors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*
*/
Expand Down Expand Up @@ -40,7 +40,7 @@
'text/json',
[
// optional, map key to tag name
\FluentDOM\Loader\Json\JsonDOM::ON_MAP_KEY => function($key, $isArrayElement) {
\FluentDOM\Loader\Json\JsonDOMLoader::ON_MAP_KEY => function($key, $isArrayElement) {
$map = [
'phoneNumbers' => 'phoneNumber'
];
Expand Down
13 changes: 11 additions & 2 deletions examples/Performance/index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<?php
/*
* FluentDOM
*
* @link https://thomas.weinert.info/FluentDOM/
* @copyright Copyright 2009-2023 FluentDOM Contributors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*
*/

/**
* The example compares the FluentDOM\Query Api with the extended DOM classes
*/
Expand All @@ -14,7 +23,7 @@ function benchmark(callable $callback, $callCount) {
}

// FluentDOM\Query
$fd = FluentDOM('test.html', 'text/html', [FluentDOM\Loader\Options::IS_FILE => TRUE]);
$fd = FluentDOM('test.html', 'text/html', [FluentDOM\Loader\LoaderOptions::IS_FILE => TRUE]);
echo benchmark(
function() use ($fd) {
$fd->find('//div[@class="test"]')->text();
Expand All @@ -23,7 +32,7 @@ function() use ($fd) {
), "\n";

// extended DOM (FluentDOM >= 5.2), this is faster
$fd = FluentDOM::load('test.html', 'text/html', [FluentDOM\Loader\Options::IS_FILE => TRUE]);
$fd = FluentDOM::load('test.html', 'text/html', [FluentDOM\Loader\LoaderOptions::IS_FILE => TRUE]);
echo benchmark(
function() use ($fd) {
$fd('string(//div[@class="test"])');
Expand Down
11 changes: 10 additions & 1 deletion examples/SimpleXML Migration/1_load.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<?php
/*
* FluentDOM
*
* @link https://thomas.weinert.info/FluentDOM/
* @copyright Copyright 2009-2023 FluentDOM Contributors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*
*/

require __DIR__.'/../../vendor/autoload.php';

$file = 'data://text/xml;base64,'.base64_encode('<foo/>');
Expand All @@ -17,7 +26,7 @@
echo $document->saveXML();

/* FluentDOM load xml from file */
$document = FluentDOM::load($file, 'xml', [FluentDOM\Loader\Options::IS_FILE => TRUE]);
$document = FluentDOM::load($file, 'xml', [FluentDOM\Loader\LoaderOptions::IS_FILE => TRUE]);
echo $document->saveXML();

/* FluentDOM load html from string */
Expand Down
11 changes: 10 additions & 1 deletion examples/SimpleXML Migration/3_property_of_non_object.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<?php
/*
* FluentDOM
*
* @link https://thomas.weinert.info/FluentDOM/
* @copyright Copyright 2009-2023 FluentDOM Contributors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*
*/

require __DIR__.'/../../vendor/autoload.php';

/*
Expand All @@ -23,4 +32,4 @@
*/
$document = FluentDOM::load('<foo/>');
var_dump($document('count(/root/some/other/element) > 0'));
// bool(false)
// bool(false)
11 changes: 10 additions & 1 deletion examples/SimpleXML Migration/4_traversing.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<?php
/*
* FluentDOM
*
* @link https://thomas.weinert.info/FluentDOM/
* @copyright Copyright 2009-2023 FluentDOM Contributors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*
*/

require __DIR__.'/../../vendor/autoload.php';

$xml = <<<'XML'
Expand Down Expand Up @@ -42,4 +51,4 @@
$document = FluentDOM::load($xml);
foreach ($document('/rss/channel/item/title') as $title) {
echo $title, "\n";
}
}
11 changes: 10 additions & 1 deletion examples/SimpleXML Migration/5_attributes.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<?php
/*
* FluentDOM
*
* @link https://thomas.weinert.info/FluentDOM/
* @copyright Copyright 2009-2023 FluentDOM Contributors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*
*/

require __DIR__.'/../../vendor/autoload.php';

$xml = <<<'XML'
Expand All @@ -24,4 +33,4 @@
* ... but if it is an integer it will access the child nodes.
*/
$element = FluentDOM::load($xml)->documentElement[1];
echo $element->localName, "\n";
echo $element->localName, "\n";
11 changes: 10 additions & 1 deletion examples/SimpleXML Migration/5_children.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<?php
/*
* FluentDOM
*
* @link https://thomas.weinert.info/FluentDOM/
* @copyright Copyright 2009-2023 FluentDOM Contributors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*
*/

require __DIR__.'/../../vendor/autoload.php';

$xml = <<<'XML'
Expand Down Expand Up @@ -35,4 +44,4 @@
* Since PHP 5.6.3, DOMNodelist allows to use array syntax.
*/
$document = FluentDOM::load($xml);
var_dump((string)$document('/rss/channel/item/title')[0]);
var_dump((string)$document('/rss/channel/item/title')[0]);
11 changes: 10 additions & 1 deletion examples/SimpleXML Migration/6_namespaces.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<?php
/*
* FluentDOM
*
* @link https://thomas.weinert.info/FluentDOM/
* @copyright Copyright 2009-2023 FluentDOM Contributors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*
*/

require __DIR__.'/../../vendor/autoload.php';

$xml = <<<'XML'
Expand Down Expand Up @@ -47,4 +56,4 @@
$document->registerNamespace('a', 'http://www.w3.org/2005/Atom');
foreach ($document('/a:feed/a:entry') as $entry) {
echo $entry('string(a:title)'), "\n";
}
}
11 changes: 10 additions & 1 deletion examples/Tasks/modifyLinksInHTML.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
<?php
/*
* FluentDOM
*
* @link https://thomas.weinert.info/FluentDOM/
* @copyright Copyright 2009-2023 FluentDOM Contributors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*
*/

require __DIR__.'/../../vendor/autoload.php';

// define url
$url = 'http://www.heise.de/';
// load data from an url
$html = file_get_contents($url);

$document = FluentDOM::load($html, 'html', [FluentDOM\Loader\Options::ALLOW_FILE => TRUE]);
$document = FluentDOM::load($html, 'html', [FluentDOM\Loader\LoaderOptions::ALLOW_FILE => TRUE]);
foreach ($document('//a[@href]') as $a) {
// check for relative url
if (!preg_match('(^[a-zA-Z]+://)', $a['href'])) {
Expand Down
10 changes: 5 additions & 5 deletions src/FluentDOM.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use FluentDOM\Exceptions\InvalidSource\Variable as InvalidVariableSource;
use FluentDOM\Exceptions\NoSerializer as NoSerializerException;
use FluentDOM\Loadable;
use FluentDOM\Loader\Lazy as LazyLoader;
use FluentDOM\Loader\LazyLoaders;
use FluentDOM\Serializer\Factory\Group as SerializerFactoryGroup;
use FluentDOM\Serializer\SerializerFactory as SerializerFactory;
use FluentDOM\Xpath\Transformer as XpathTransformer;
Expand Down Expand Up @@ -107,7 +107,7 @@ public static function QueryCss(

/**
* Set a loader used in FluentDOM::load(), FALSE will reset the loader.
* If no loader is provided an FluentDOM\Loader\Standard() will be created.
* If no loader is provided an FluentDOM\Loader\StandardLoaders() will be created.
*
* @throws InvalidArgument
*/
Expand All @@ -128,7 +128,7 @@ public static function registerLoader(
): FluentDOM\Loaders {
$loaders = self::getDefaultLoaders();
if (count($contentTypes) > 0) {
$lazyLoader = new LazyLoader();
$lazyLoader = new LazyLoaders();
foreach ($contentTypes as $contentType) {
$lazyLoader->add($contentType, $loader);
}
Expand All @@ -143,13 +143,13 @@ public static function registerLoader(
}

/**
* Standard loader + any registered loader.
* StandardLoaders loader + any registered loader.
*
* @codeCoverageIgnore
*/
public static function getDefaultLoaders(): FluentDOM\Loaders {
if (!(self::$_defaultLoaders instanceof FluentDOM\Loaders)) {
self::$_defaultLoaders = new FluentDOM\Loaders(new FluentDOM\Loader\Standard());
self::$_defaultLoaders = new FluentDOM\Loaders(new FluentDOM\Loader\StandardLoaders());
}
return self::$_defaultLoaders;
}
Expand Down
4 changes: 2 additions & 2 deletions src/FluentDOM/Loadable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace FluentDOM {

use FluentDOM\DOM\DocumentFragment;
use FluentDOM\Loader\Result;
use FluentDOM\Loader\LoaderResult;

/**
* FluentDOM\Loadable describes an interface for loader objects that can be used to load
Expand All @@ -31,7 +31,7 @@ public function supports(string $contentType):bool;
* Load the data source and return the new DOM document. Return NULL if
* the data source could not be loaded.
*/
public function load($source, string $contentType, iterable $options = []): ?Result;
public function load($source, string $contentType, iterable $options = []): ?LoaderResult;

/**
* Load the data source and return the new DOM document. Return NULL if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
/**
* Load a DOM document from a xml string
*/
class Html implements Loadable {
class HtmlLoader implements Loadable {

use Supports\Libxml;
use LoaderSupports\LibxmlSupports;

public const IS_FRAGMENT = 'is_fragment';
public const CONTENT_TYPES = ['html', 'text/html', 'html-fragment', 'text/html-fragment'];
Expand All @@ -34,7 +34,7 @@ class Html implements Loadable {
* @throws \Throwable
* @see Loadable::load
*/
public function load(mixed $source, string $contentType, iterable $options = []): ?Result {
public function load(mixed $source, string $contentType, iterable $options = []): ?LoaderResult {
if ($this->supports($contentType)) {
return (new Libxml\Errors())->capture(
function() use ($source, $contentType, $options) {
Expand All @@ -47,16 +47,16 @@ function() use ($source, $contentType, $options) {
} else {
$settings->isAllowed($sourceType = $settings->getSourceType($source));
switch ($sourceType) {
case Options::IS_FILE :
$document->loadHTMLFile($source, $settings[Options::LIBXML_OPTIONS]);
case LoaderOptions::IS_FILE :
$document->loadHTMLFile($source, $settings[LoaderOptions::LIBXML_OPTIONS]);
break;
case Options::IS_STRING :
case LoaderOptions::IS_STRING :
default :
$document->loadHTML(
$this->ensureEncodingPI(
$source, $settings[Options::ENCODING], (bool)$settings[Options::FORCE_ENCODING]
$source, $settings[LoaderOptions::ENCODING], (bool)$settings[LoaderOptions::FORCE_ENCODING]
),
$settings[Options::LIBXML_OPTIONS]
$settings[LoaderOptions::LIBXML_OPTIONS]
);
}
}
Expand All @@ -66,7 +66,7 @@ function() use ($source, $contentType, $options) {
) {
$pi->remove();
}
return new Result($document, 'text/html', $selection);
return new LoaderResult($document, 'text/html', $selection);
}
);
}
Expand Down Expand Up @@ -132,10 +132,10 @@ function() use ($source, $options) {
$document->loadHTML(
$this->ensureEncodingPI(
'<html-fragment>'.$source.'</html-fragment>',
$options[Options::ENCODING],
(bool)$options[Options::FORCE_ENCODING]
$options[LoaderOptions::ENCODING],
(bool)$options[LoaderOptions::FORCE_ENCODING]
),
$options[Options::LIBXML_OPTIONS]
$options[LoaderOptions::LIBXML_OPTIONS]
);
$nodes = $document->evaluate('//html-fragment[1]/node()');
foreach ($nodes as $node) {
Expand All @@ -161,10 +161,10 @@ private function loadFragmentIntoDOM(\DOMDocument $document, string $source, $se
$htmlDom->loadHTML(
$this->ensureEncodingPI(
'<html-fragment>'.$source.'</html-fragment>',
$settings[Options::ENCODING],
(bool)$settings[Options::FORCE_ENCODING]
$settings[LoaderOptions::ENCODING],
(bool)$settings[LoaderOptions::FORCE_ENCODING]
),
$settings[Options::LIBXML_OPTIONS]
$settings[LoaderOptions::LIBXML_OPTIONS]
);
$nodes = $htmlDom->evaluate('//html-fragment[1]/node()');
foreach ($nodes as $node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
use FluentDOM\DOM\DocumentFragment;
use FluentDOM\DOM\Element;
use FluentDOM\Loadable;
use FluentDOM\Loader\Json\JsonDOM;
use FluentDOM\Loader\Json\JsonDOMLoader;
use FluentDOM\Utility\Constraints;
use FluentDOM\Utility\QualifiedName;

/**
* Load IBMs JSONx format.
* Load IBMs JSONxLoader format.
*/
class JSONx implements Loadable {
class JSONxLoader implements Loadable {

use Supports\Libxml;
use LoaderSupports\LibxmlSupports;
public const CONTENT_TYPES = ['jsonx', 'application/xml+jsonx'];

/** @noinspection HttpUrlsUsage */
private const XMLNS_JSONX = 'http://www.ibm.com/xmlns/prod/2009/jsonx';
private const XMLNS_JSONDOM = JsonDOM::XMLNS;
private const XMLNS_JSONDOM = JsonDOMLoader::XMLNS;
private const DEFAULT_QNAME = '_';

/**
Expand All @@ -38,15 +38,15 @@ class JSONx implements Loadable {
*/
public function load(
mixed $source, string $contentType, iterable $options = []
): ?Result {
): ?LoaderResult {
if (NULL !== $source && $this->supports($contentType)) {
$document = $this->loadXmlDocument($source, $options);
$target = new Document();
$target->registerNamespace('json', self::XMLNS_JSONDOM);
if (isset($document->documentElement)) {
$this->transferNode($document->documentElement, $target);
}
return new Result($target, $contentType);
return new LoaderResult($target, $contentType);
}
return NULL;
}
Expand All @@ -56,7 +56,7 @@ public function load(
*
* @param mixed $source
* @param string $contentType
* @param array|\Traversable|Options $options
* @param array|\Traversable|LoaderOptions $options
* @return DocumentFragment|NULL
* @throws \Throwable
*/
Expand Down
Loading

0 comments on commit 46d55b0

Please sign in to comment.