XmlPath
extends ProcessorEntity
in package
Uses
ConvertToXmlTrait
Class XmlPath
Processor class to fetch/set values in XML or HTML string.
Table of Contents
- $id : mixed
- Processor ID.
- $db : ADOConnection|null
- DB connections.
- $details : array<string|int, mixed>
- {@inheritDoc}
- $logger : MonologWrapper
- Logger object.
- $meta : array<string|int, mixed>
- Metadata for the processor.
- $request : Request
- All the request details.
- __construct() : mixed
- details() : array<string|int, mixed>
- Return details for processor.
- fromArrayToXml() : string|null
- Convert array to XML string.
- fromBooleanToXml() : string
- Convert boolean to XML string.
- fromFileToXml() : string
- Convert file to XML string.
- fromFloatToXml() : string
- Convert float to XML string.
- fromHtmlToXml() : string
- Convert an HTML string to XML string.
- fromImageToXml() : string
- Convert an image string to XML string.
- fromIntegerToXml() : string
- Convert integer to XML string.
- fromJsonToXml() : string
- Convert JSON string to XML string.
- fromTextToXml() : string
- Convert text to XML string.
- fromUndefinedToXml() : string
- Convert undefined to XML.
- fromXmlToXml() : string
- Convert XML string to XML string.
- process() : DataContainer
- {@inheritDoc}
- val() : mixed|DataContainer
- Process a variable into a final result for the processor.
- addXpath() : string
- Add a value to an XML string.
- array2xml() : string
- Recursive method to convert an array into XML format.
- generateParams() : array<string|int, mixed>
- Generate the params array for the sql search.
- getBaseXmlWrapper() : SimpleXMLElement
- Get the base SimpleXMLElement wrapper for XML for converting non XML inputs to XML output.
- getXpath() : string
- Fetch an array of items found in an XML string with an expression.
- isDataContainer() : bool
- Validate if a set of data is wrapped in a DataContainer object.
- removeXpath() : string
- Add a value to an XML string.
- setXpath() : string
- Set a value in an XML string.
- simplexmlImportXml() : bool
- Append a SimpleXMLElement structure to an existing SimpleXMLElement.
- validateAllowedTypes() : void
- Validate an input for allowed variable types
- validateAllowedValues() : void
- Validate an input for allowed values.
Properties
$id
Processor ID.
public
mixed
$id
= ''
Processor ID.
$db
DB connections.
protected
ADOConnection|null
$db
$details
{@inheritDoc}
protected
array<string|int, mixed>
$details
= ['name' => 'XML Path', 'machineName' => 'xml_path', 'description' => <<<DESCRIPTION
Get, update, add or remove values in an XML or HTML string.
Examples:
Source XML:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
Get operation:
processor: xml_path
id: get_example
data: "<xml><string /></xml>"
operation: get
expression: "//bookstore/book[1]/price"
Result:
["<price>30.00</price>"]
Set operation:
processor: xml_path
id: set_example
data: "<xml><string></string></xml>"
operation: set
expression: "//bookstore/book[1]/price"
value: 100000000.95
Result:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>100000000.95</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
Add operation:
processor: json_path
id: add_example
data: "<xml><string></string></xml>"
operation: add
expression: "$.store.book"
value: "<book category="DRAMA">
<title lang="en">Killing Heidi</title>
<author>Jane Austen</author>
<year>2035</year>
<price>0.95</price>
</book>"
Result:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>100000000.95</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
<book category="DRAMA">
<title lang="en">Killing Heidi</title>
<author>Jane Austen</author>
<year>2035</year>
<price>0.95</price>
</book>
</bookstore>
Remove operation:
processor: json_path
id: remove_example
data: "<xml><string></string></xml>"
operation: remove
expression: "$.store.book[0]"
Result:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<bookstore>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
DESCRIPTION
, 'menu' => 'Data operation', 'input' => ['data' => ['description' => 'The input XML string.', 'cardinality' => [1, 1], 'literalAllowed' => true, 'limitProcessors' => [], 'limitTypes' => ['xml', 'html'], 'limitValues' => [], 'default' => null], 'expression' => ['description' => 'The JSON path expression.', 'cardinality' => [1, 1], 'literalAllowed' => true, 'limitProcessors' => [], 'limitTypes' => ['text'], 'limitValues' => [], 'default' => '$.*'], 'operation' => ['description' => 'The operation ("get", "set", "add" or "remove").', 'cardinality' => [1, 1], 'literalAllowed' => true, 'limitProcessors' => [], 'limitTypes' => ['text'], 'limitValues' => ['get', 'set', 'add', 'remove'], 'default' => 'get'], 'value' => [
// phpcs:ignore
'description' => 'The XML node string to insert or value to update on a node (used for "set" or "add" operations).',
'cardinality' => [0, 1],
'literalAllowed' => true,
'limitProcessors' => [],
'limitTypes' => [],
'limitValues' => [],
'default' => null,
]]]
Details of the processor.
$logger
Logger object.
protected
MonologWrapper
$logger
$meta
Metadata for the processor.
protected
array<string|int, mixed>
$meta
$request
All the request details.
protected
Request
$request
Request.
Methods
__construct()
public
__construct(array<string|int, mixed> &$meta, Request &$request[, MonologWrapper|null $logger = null ]) : mixed
Parameters
- $meta : array<string|int, mixed>
-
Metadata for the processor.
- $request : Request
-
The full request object.
- $logger : MonologWrapper|null = null
-
The logger.
Return values
mixed —details()
Return details for processor.
public
details() : array<string|int, mixed>
Return values
array<string|int, mixed> —fromArrayToXml()
Convert array to XML string.
public
fromArrayToXml(array<string|int, mixed>|null $array) : string|null
Parameters
- $array : array<string|int, mixed>|null
Tags
Return values
string|null —fromBooleanToXml()
Convert boolean to XML string.
public
fromBooleanToXml(bool|null $boolean) : string
Parameters
- $boolean : bool|null
Return values
string —fromFileToXml()
Convert file to XML string.
public
fromFileToXml( $file) : string
Parameters
Return values
string —fromFloatToXml()
Convert float to XML string.
public
fromFloatToXml(float|null $float) : string
Parameters
- $float : float|null
Return values
string —fromHtmlToXml()
Convert an HTML string to XML string.
public
fromHtmlToXml(string|null $html) : string
Parameters
- $html : string|null
Return values
string —fromImageToXml()
Convert an image string to XML string.
public
fromImageToXml( $image) : string
Parameters
Return values
string —fromIntegerToXml()
Convert integer to XML string.
public
fromIntegerToXml(int|float|null $integer) : string
Parameters
- $integer : int|float|null
Return values
string —fromJsonToXml()
Convert JSON string to XML string.
public
fromJsonToXml(string|null $json) : string
Parameters
- $json : string|null
Return values
string —fromTextToXml()
Convert text to XML string.
public
fromTextToXml(string|null $text) : string
Parameters
- $text : string|null
Return values
string —fromUndefinedToXml()
Convert undefined to XML.
public
fromUndefinedToXml( $data) : string
Parameters
Return values
string —fromXmlToXml()
Convert XML string to XML string.
public
fromXmlToXml(string|null $xml) : string
Parameters
- $xml : string|null
Return values
string —process()
{@inheritDoc}
public
process() : DataContainer
Tags
Return values
DataContainer —Result of the processor.
val()
Process a variable into a final result for the processor.
public
val(string $key[, bool|null $rawData = false ]) : mixed|DataContainer
This method can be used to process a value in its meta to return a final result that it can use. If the object is a processor, then it will process that down to a final return value, or if the obj is a simple value, then it will return that. Anything else will return an error object.
Setting $realValue to true will force the value to be the actual value, rather than a potential dataContainer.
Parameters
- $key : string
-
The key for the input variable in the meta.
- $rawData : bool|null = false
-
Return the raw data or a DataContainer.
Tags
Return values
mixed|DataContainer —addXpath()
Add a value to an XML string.
protected
addXpath(string $data, string $expression, string $value) : string
Parameters
- $data : string
-
XML string.
- $expression : string
-
XPath expression for nodes to add to.
- $value : string
-
Value to add to the nodes found by XPath.
Tags
Return values
string —array2xml()
Recursive method to convert an array into XML format.
protected
array2xml(array<string|int, mixed> $array, SimpleXMLElement $xml) : string
Parameters
- $array : array<string|int, mixed>
-
Input array.
- $xml : SimpleXMLElement
-
A SimpleXMLElement element.
Return values
string —A populated SimpleXMLElement.
generateParams()
Generate the params array for the sql search.
protected
generateParams(string|null $keyword, array<string|int, mixed>|null $keywordCols, string|null $orderBy, string|null $direction) : array<string|int, mixed>
Parameters
- $keyword : string|null
-
Search keyword.
- $keywordCols : array<string|int, mixed>|null
-
Columns to search for the keyword.
- $orderBy : string|null
-
Order by column.
- $direction : string|null
-
Order direction.
Return values
array<string|int, mixed> —getBaseXmlWrapper()
Get the base SimpleXMLElement wrapper for XML for converting non XML inputs to XML output.
protected
getBaseXmlWrapper([string $baseTag = 'apiOpenStudioWrapper' ]) : SimpleXMLElement
Parameters
- $baseTag : string = 'apiOpenStudioWrapper'
Tags
Return values
SimpleXMLElement —getXpath()
Fetch an array of items found in an XML string with an expression.
protected
getXpath(string $data, string $expression) : string
Parameters
- $data : string
-
XML string.
- $expression : string
-
XPath expression.
Tags
Return values
string —isDataContainer()
Validate if a set of data is wrapped in a DataContainer object.
protected
isDataContainer(mixed $data) : bool
Parameters
- $data : mixed
-
DataContainer or raw data.
Return values
bool —removeXpath()
Add a value to an XML string.
protected
removeXpath(string $data, string $expression) : string
Parameters
- $data : string
-
XML string.
- $expression : string
-
XPath expression for nodes to add to.
Tags
Return values
string —setXpath()
Set a value in an XML string.
protected
setXpath(string $data, string $expression, mixed $value) : string
Parameters
- $data : string
-
XML string.
- $expression : string
-
XPath expression for nodes to set.
- $value : mixed
-
Value to set on the nodes found by XPath.
Tags
Return values
string —simplexmlImportXml()
Append a SimpleXMLElement structure to an existing SimpleXMLElement.
protected
simplexmlImportXml(SimpleXMLElement $parent, string|SimpleXMLElement $xml[, bool $before = false ]) : bool
Parameters
- $parent : SimpleXMLElement
- $xml : string|SimpleXMLElement
- $before : bool = false
Tags
Return values
bool —validateAllowedTypes()
Validate an input for allowed variable types
private
validateAllowedTypes(string $type, array<string|int, mixed> $limitTypes, int $min, string $key) : void
Parameters
- $type : string
-
Input value type.
- $limitTypes : array<string|int, mixed>
-
List of limit on variable types.
- $min : int
-
Minimum number of values.
- $key : string
-
The key of the input being validated.
Tags
Return values
void —validateAllowedValues()
Validate an input for allowed values.
private
validateAllowedValues(mixed $val, array<string|int, mixed> $limitValues, int $min, string $key) : void
Parameters
- $val : mixed
-
Input value.
- $limitValues : array<string|int, mixed>
-
List of allowed values.
- $min : int
-
Minimum number of values.
- $key : string
-
The key of the input being validated.