Download the PHP package borislavsabev/simple-xml-loader without Composer
On this page you can find all versions of the php package borislavsabev/simple-xml-loader. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download borislavsabev/simple-xml-loader
More information about borislavsabev/simple-xml-loader
Files in borislavsabev/simple-xml-loader
Package simple-xml-loader
Short Description A minimalistic SimpleXml loader. Wraps the simplexml_load_XXXX() methods and provides error handling.
License GPL-3.0
Homepage https://github.com/BorislavSabev/SimpleXmlLoader
Informations about the package simple-xml-loader
SimpleXMLLoader
A minimalistic SimpleXml loader. Wraps the simplexml_load_XXXX() methods and provides libxml error handling.
Main goals:
- provide a transparent OOP interface to SimpleXML's
simplexml_load_file()
andsimplexml_load_string()
functions - handle SimpleXML's internal errors
-
throw catchable Exceptions (XmlLoaderException)
This wrapper will:
- try to load your XML resource into a \SimpleXMLElement object and return it
- handle any LibXML errors that occur during loading
This wrapper will not:
- do anything else
Installation
This package is available on Packagist and installable via Composer:
composer require borislavsabev/simple-xml-loader
Basic usage
As this is a wrapper the wrapped library's functionality is not covered in this documentation. Please refer to:
- The SimpleXml manual
-
The SimpleXml functions reference
There are two main functions in the SimpleXMlLoader:
The loader throws XmlLoaderException on any error that occurs. The exception thrown contains the message and code of the last \LibXmlError. The loader calls are meant to be wrapped in try/catch blocks and read any errors yourself:
The XmlLoader instance is meant to be reused thus:
- Each call to
XmlLoader->loadFile()
orXmlLoader->loadString()
will clear any LibXml errors stored - Each call to
XmlLoader->loadFile()
orXmlLoader->loadString()
will replace it's internal XmlLoaderPayload object
Loop usage
Each consecutive call to a loader method resets the state of LibXML and the payload you must extract all data between calls.
Reusing XmlLoaderPayload
You can also reuse the XmlLoaderPayload in your application if you which. Say for example you use this wrapper to parse different XML file but then want to pass the result to different Services in your code that will handle any business logic. You could just get the XmlLoaderPayload and pass it along to a specific service (or whatever really):
NOTE: Generally you should have you own payload objects to pass data around in your Domain. The idea of XmlLoaderPayload is to be internal for XmlLoader thus it cannot contain any logic outside of that task.
Behaviour overwritten
There is only one behaviour change that this library introduces:
_"If you pass an empty string to simplexml_load_string()
it will return false and throw no error"_.
The SimpleXmlLoader wrapper throws an XmlLoaderException
in this case.
The XmlLoaderException and LibXML's libXMLError
XmlLoaderException's codes are specific to this wrapper. LibXML's libXMLError objects are just returned in an array and as received from SimpleXML/LibXML.
Personal Opinion on the SimpleXML PHP Extension
Generally SimpleXML is not a solid PHP extension and, in my mind, it should be used rarely when you need to do something simple fast. Any serious work should be done via DomDocument.
The previous two sentences are the author's personal opinion which as with any opinion should be taken with a grain of salt.
Versioning
This library follows Semver. According to Semver, you will be able to upgrade to any minor or patch version of this library without any breaking changes to the public API. Semver also requires that we clearly define the public API for this library.
All methods, with public
visibility, are part of the public API. All other methods are not part of the public API. Where possible, we'll try to keep protected
methods backwards-compatible in minor/patch versions, but if you're overriding methods then please test your work before upgrading.
Contributing
Please do! PR's are very welcome. The author is far from thinking that this wrapper library is perfect. Any PR must first abide by the [Main goals](#Main goals) set out by this library.
Please note that this project adheres to the Contributor Covenant v1.4. By participating in this project you agree to abide by its terms.
All versions of simple-xml-loader with dependencies
ext-simplexml Version *