Skip to content
Merged
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
44 changes: 44 additions & 0 deletions Documentation/reference/tasks/_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
TaskName
========

_Describe main goal an use cases of the task_

Task reference
--------------

* **Service**: `ClassName`

Accepted inputs
---------------

_Description of allowed types_

Possible outputs
----------------

_Description of possible types_

Options
-------

| Code | Type | Required | Default | Description |
| ---- | ---- | :------: | ------- | ----------- |
| `code` | `type` | **X** _or nothing_ | `default value` _if available_ | _description_ |

Examples
--------

_YAML samples and explanations_

* Example 1
- details
- details

```yaml
# Task configuration level
code:
service: '@service_ref'
options:
a: 1
b: 2
```
39 changes: 39 additions & 0 deletions Documentation/reference/tasks/xml_reader_task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
XmlReaderTask
=============

Open and read an XML file.
Requires `php-xml`.

Task reference
--------------

* **Service**: `CleverAge\ProcessBundle\Task\File\Xml\XmlReaderTask`

Accepted inputs
---------------

No input accepted.

Possible outputs
----------------

A `\DOMDocument` built from the file.

Options
-------

| Code | Type | Required | Default | Description |
| ---- | ---- | :------: | ------- | ----------- |
| `file_path` | `string` | **X** | | Path of the file to read from (relative to symfony root or absolute) |
| `mode` | `string` | | `rb` | File open mode (see [fopen mode parameter](https://secure.php.net/manual/en/function.fopen.php)) |

Examples
--------

```yaml
# Task configuration level
my_xml_reader:
service: '@CleverAge\ProcessBundle\Task\File\Xml\XmlReaderTask'
options:
file_path: '%kernel.project_dir%/var/data/file.xml'
```
4 changes: 2 additions & 2 deletions Documentation/reference/transformers/_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ TransformerName

_Describe main goal an use cases of the transformer_

Task reference
--------------
Transformer reference
---------------------

* **Service**: `ClassName`
* **Transformer code**: `code`
Expand Down
116 changes: 116 additions & 0 deletions Documentation/reference/transformers/xpath_evaluator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
XpathEvaluatorTransformer
=========================

Manipulate a DOMNode to extract some information using xpath.
Requires `php-xml`.

**Important** : due to the [behavior of `\DOMXpath::query`](https://www.php.net/manual/en/domxpath.query.php), if you want
to make a query on a sub element of the full `\DOMDocument` you need to start your query with a `.` to specify the current node.

Task reference
--------------

* **Service**: `CleverAge\ProcessBundle\Transformer\Xml\XpathEvaluatorTransformer`
* **Transformer code**: `xpath_evaluator`

Accepted inputs
---------------

`\DOMNode` only.

Possible outputs
----------------

Depending on the options :
- `string`
- `\DOMNode`
- `null`
- an `array` of one of the type above

Options
-------

| Code | Type | Required | Default | Description |
| ---- | ---- | :------: | ------- | ----------- |
| `query` | `string` or `array` | **X** | | One or multiple Xpath queries. Using an array, you can either have a simple list of subqueries, or override some root-level query options |
| `single_result` | `boolean` | | `true` | Force the result to match a single value |
| `ignore_missing` | `boolean` | | `true` | Only used with `single_result`, avoid errors if the query doesn't match anything |
| `unwrap_value` | `boolean` | | `true` | Return the textual content of the node, only works if the result is a `\DOMText` (you might need to use the `text()` xpath selector) or a `\DOMAttr` |

Subqueries, in their complex form, have the following options :

| Code | Type | Required | Default | Description |
| ---- | ---- | :------: | ------- | ----------- |
| `subquery` | `string` | **X** | | An Xpath query, no additional sublevel is allowed |
| `single_result` | `boolean` | | _Root-level value for `single_result`_ | Force the result to match a single value |
| `ignore_missing` | `boolean` | | _Root-level value for `ignore_missing`_ | Only used with `single_result`, avoid errors if the query doesn't match anything |
| `unwrap_value` | `boolean` | | _Root-level value for `unwrap_value`_ | Return the textual content of the node, only works if the result is a `\DOMText` (you might need to use the `text()` xpath selector) or a `\DOMAttr` |


Examples
--------

All examples assume this XML
```xml
<a>
<b>
<c>ok1</c>
<c>ok2</c>
<c>ok3</c>
</b>
<d>
<e>ok4</e>
<f>ok5</f>
<g>ok6</g>
</d>
</a>
```

* Example 1 : get a single value

```yaml
# Transformer options level
xpath_evaluator:
query: '/a/b/c[0]/text()'
```

* Example 2 : get a multiple values

```yaml
# Transformer options level
xpath_evaluator:
query: '/a/b/c/text()'
single_result: false
```

```yaml
# Transformer options level
xpath_evaluator:
query:
- '/a/d/e/text()'
- '/a/d/f/text()'
- '/a/d/g/text()'
```

* Example 3 : get a `\DOMNode`

```yaml
# Transformer options level
xpath_evaluator:
query: '/a/b'
unwrap_value: false
```

* Example 4 : subquery with partially overridden options

```yaml
# Transformer options level
xpath_evaluator:
query:
all_c_values:
subquery: '/a/b/c/text()'
single_result: false
e_value: '/a/d/e/text()'
f_value:
subquery: '/a/d/f/text()'
```
54 changes: 54 additions & 0 deletions Filesystem/XmlFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types=1);
/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (C) 2017-2019 Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CleverAge\ProcessBundle\Filesystem;

/**
* Read and write XML files
* @author Valentin Clavreul <vclavreul@clever-age.com>
*/
class XmlFile
{
/** @var \SplFileObject */
protected $file;

/**
* XmlFile constructor.
*
* @param string $path
* @param string $mode
*/
public function __construct(string $path, string $mode = 'rb')
{
$this->file = new \SplFileObject($path, $mode);
}

public function read(): \DOMDocument
{
$dom = new \DOMDocument();
$this->file->rewind();
$fileSize = $this->file->getSize();
$fileContent = $this->file->fread($fileSize);

$dom->loadXML($fileContent);

return $dom;
}

public function write(\DOMDocument $dom)
{
$content = $dom->saveXML();
$result = $this->file->fwrite($content);

if ($result === null) {
throw new \RuntimeException("Could not write content to file");
}
}
}
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ Basically, it will greatly ease the configuration of import and exports but can
- Reference
- [Process definition](Documentation/reference/01-process_definition.md)
- [Task definition](Documentation/reference/02-task_definition.md)
- [Generic transformers definition](Documentation/reference/03-generic_transformers_definition.md)
- Basic and debug
- [ConstantOutputTask](Documentation/reference/tasks/constant_output_task.md)
- [ConstantIterableOutputTask](Documentation/reference/tasks/constant_iterable_output_task.md)
Expand All @@ -44,6 +43,8 @@ Basically, it will greatly ease the configuration of import and exports but can
- File/CSV
- [CsvReaderTask](Documentation/reference/tasks/csv_reader_task.md)
- [CsvWriterTask](Documentation/reference/tasks/csv_writer_task.md)
- File/XML
- [XmlReaderTask](Documentation/reference/tasks/xml_reader_task.md)
- Flow manipulation
- [AggregateIterableTask](Documentation/reference/tasks/aggregate_iterable_task.md)
- [InputAggregatorTask](Documentation/reference/tasks/input_aggregator_task.md)
Expand All @@ -52,6 +53,8 @@ Basically, it will greatly ease the configuration of import and exports but can
- [ArrayFilterTransformer](Documentation/reference/transformers/array_filter_transformer.md)
- [MappingTransformer](Documentation/reference/transformers/mapping_transformer.md)
- [RulesTransformer](Documentation/reference/transformers/rules_transformer.md)
- [XpathEvaluatorTransformer](Documentation/reference/transformers/xpath_evaluator.md)
- [Generic transformers definition](Documentation/reference/03-generic_transformers_definition.md)
- Examples
- [Simple ETL]
- [Roadmap and versions](Documentation/100-roadmap.md)
Expand Down
63 changes: 63 additions & 0 deletions Task/File/Xml/XmlReaderTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types=1);
/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (C) 2017-2019 Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CleverAge\ProcessBundle\Task\File\Xml;

use CleverAge\ProcessBundle\Filesystem\XmlFile;
use CleverAge\ProcessBundle\Model\AbstractConfigurableTask;
use CleverAge\ProcessBundle\Model\ProcessState;
use Psr\Log\LoggerInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Read an XML file
*
* @author Valentin Clavreul <vclavreul@clever-age.com>
*/
class XmlReaderTask extends AbstractConfigurableTask
{
/** @var LoggerInterface */
protected $logger;

/**
* XmlReaderTask constructor.
*
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}

/**
* {@inheritDoc}
*/
protected function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired('file_path');
$resolver->setAllowedTypes('file_path', 'string');

$resolver->setDefault('mode', 'rb');
$resolver->setAllowedTypes('mode', 'string');
}

/**
* {@inheritDoc}
*/
public function execute(ProcessState $state)
{
if ($state->getInput() !== null) {
$this->logger->warning('Input has been ignored for XMLReaderTask');
}

$file = new XmlFile($this->getOption($state, 'file_path'), $this->getOption($state, 'mode'));
$state->setOutput($file->read());
}
}
20 changes: 18 additions & 2 deletions Tests/AbstractProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected function assertDataQueue(array $expected, string $processName, bool $c
*/
protected function getContainer(): ContainerInterface
{
if(isset(self::$container)) {
if (isset(self::$container)) {
return self::$container;
}

Expand All @@ -115,6 +115,22 @@ protected function getContainer(): ContainerInterface
* @throws ExceptionInterface
*/
protected function assertTransformation(string $transformerCode, $expected, $value, array $options = [])
{
$result = $this->transform($transformerCode, $value, $options);
self::assertEquals($expected, $result);
}

/**
* Transform some value using referenced transformer with given options
*
* @param string $transformerCode
* @param mixed $value
* @param array $options
*
* @return mixed
* @throws ExceptionInterface
*/
protected function transform(string $transformerCode, $value, array $options = [])
{
$transformer = $this->transformerRegistry->getTransformer($transformerCode);

Expand All @@ -124,6 +140,6 @@ protected function assertTransformation(string $transformerCode, $expected, $val
$options = $resolver->resolve($options);
}

self::assertEquals($expected, $transformer->transform($value, $options));
return $transformer->transform($value, $options);
}
}
Loading