Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 2 #15

Merged
merged 43 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
05bf22b
Merge pull request #6 from dikastes/dev
dikastes Feb 6, 2024
72ce8e5
Add description of translation feature
dikastes Feb 20, 2024
31f6baf
Merge pull request #11 from dikastes/dev
dikastes Mar 12, 2024
52a8946
Update README.md
dikastes Mar 27, 2024
d6b6558
Implemented conversion from MEI to JSON
Nathmel123 Mar 27, 2024
a8c04e0
Merge branch 'issue-2' of github.com:dikastes/liszt_common into issue-2
Nathmel123 Mar 27, 2024
29d394d
Removed testout.json
Nathmel123 Mar 27, 2024
f1415dc
Unit test now loads testfile
Nathmel123 Mar 27, 2024
b25c3c3
Changed functional test, so it works
Nathmel123 Mar 27, 2024
7a9f36d
Removed varDump
Nathmel123 Mar 27, 2024
b08ef3a
Reformating and style improvements
Nathmel123 Mar 28, 2024
654da84
Some more style changes
Nathmel123 Mar 28, 2024
0f3ee3b
Replaced foreach with Illuminate Collection
Nathmel123 Apr 18, 2024
5dde997
Implemented basic Collection usage for children
Nathmel123 Apr 25, 2024
ab792fe
Replaced collection with forEach loop
Nathmel123 May 2, 2024
20c440f
Added functions to set up single config aspects
Nathmel123 May 15, 2024
4c70549
Removed useless var
Nathmel123 May 15, 2024
973bf6f
fix
Nathmel123 May 15, 2024
a20abfd
convert function is now protected
Nathmel123 May 16, 2024
0cdf0b2
Parser now filters and writes splitnodes
Nathmel123 May 30, 2024
77bce5d
Update README.md
dikastes Jun 19, 2024
7040720
Merge pull request #16 from dikastes/dikastes-patch-1
dikastes Jun 19, 2024
c3707b0
Implemented extern parse child function
Nathmel123 Jun 29, 2024
6d1ff39
Merge branch 'main' into issue-2
Nathmel123 Jun 29, 2024
bceb841
Update README.md
Nathmel123 Jun 29, 2024
af48e83
Update XmlDocument.php
Nathmel123 Jun 30, 2024
4148ba4
Created fluid interface
Nathmel123 Jul 11, 2024
e418739
Merge branch 'issue-2' of github.com:dikastes/liszt_common into issue-2
Nathmel123 Jul 11, 2024
80f83ee
Some minor formatting improvements
Nathmel123 Jul 11, 2024
a4cdcdb
Style changes (line up with conventions)
Nathmel123 Jul 11, 2024
2f61412
Some improvements
Nathmel123 Jul 11, 2024
800ccb1
Reverted composer.json
Nathmel123 Jul 11, 2024
de22b87
Added more testes
Nathmel123 Aug 14, 2024
450623d
Removed xml header from literal string
Nathmel123 Aug 15, 2024
4b00ce2
More tests, fix for XmlDocument
Nathmel123 Sep 3, 2024
9940748
fix
Nathmel123 Sep 3, 2024
29a85d3
Removed testfiles
Nathmel123 Sep 4, 2024
e329986
Revert "fix"
Nathmel123 Sep 4, 2024
d21727f
Revert "Removed testfiles"
Nathmel123 Sep 4, 2024
cb7266e
Revert "Revert "fix""
Nathmel123 Sep 4, 2024
703ad57
Some changes in format
Nathmel123 Sep 5, 2024
226d320
Solved problem with package updates
Nathmel123 Sep 5, 2024
443e317
Merge branch 'dev' into issue-2
Nathmel123 Sep 5, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ public/
typo3temp/
vendor/
composer.lock
Tests/Testfiles/*.json
157 changes: 150 additions & 7 deletions Classes/Common/XmlDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,173 @@

namespace Slub\LisztCommon\Common;

use SimpleXMLElement;
Nathmel123 marked this conversation as resolved.
Show resolved Hide resolved

class XmlDocument
{

protected string $xmlString;

// Set up configuration vars
protected bool $includeLiteralString;
protected bool $includeXmlId;
protected array $splitSymbols;

// Private helper vars
protected array $convertedArray;

public function __construct(string $xmlString)
{
$this->xmlString = $xmlString;

// Deal with xml-reserved symbols
$this->xmlString = str_replace('&', '&', $this->xmlString);

// Default config
$this->includeLiteralString = false;
$this->includeXmlId = true;
$this->splitSymbols = array();
}

public static function from (string $xmlString): XmlDocument
// Set up config, if needed

public function setConfig(array $config)
{
$this->includeLiteralString = $config['literalString'];
$this->includeXmlId = $config['xmlId'];
$this->splitSymbols = $config['splitSymbols'];
return $this;
}

// Functions to set single config aspects

public function setXmlId(bool $xmlId)
{
$this->includeXmlId = $xmlId;
return $this;
}

public function setLiteralString(bool $literal)
{
$this->includeLiteralString = $literal;
return $this;
}

public function setSplitSymbols(array $splitSymbols)
{
$this->splitSymbols = $splitSymbols;
return $this;
}

public static function from(string $xmlString): XmlDocument
{
return new XmlDocument($xmlString);
}

public function toArray(): array
{
// add function here
return [];
// Check if array is already converted
Nathmel123 marked this conversation as resolved.
Show resolved Hide resolved
if (isset($this->convertedArray)) {
return $this->convertedArray;
}

$this->convertedArray = [];
$xml = simplexml_load_string($this->xmlString);
$this->convertedArray[$this->getXmlId($xml)] = $this->convert($xml);
return $this->convertedArray;
}

public function toJson()
{
$result = [];
$xmlArray = $this->toArray();

/*
* Convert every key value pair to valid json,
* then decode it, so it stays valid when the whole
* array is encoded
*/
foreach ($xmlArray as $id => $value) {
Nathmel123 marked this conversation as resolved.
Show resolved Hide resolved
$result[$id] = json_encode($value);
$result[$id] = json_decode($result[$id], true);
}
return trim(json_encode($result));
}

protected function convert(SimpleXMLElement $node): array
{
$result = [];

// Parse attributes
$attrs = collect($node->attributes())->filter(function ($attrValue) {
return !empty(trim(strval($attrValue)));
})->mapWithKeys(function ($attrValue, $attrName) {
return [$attrName => trim((string) $attrValue)];
})->toArray();

// Merge parsed attributes with result array
if (!empty($attrs)) {
$result = array_merge_recursive($result, ['@attributes' => $attrs]);
}
Nathmel123 marked this conversation as resolved.
Show resolved Hide resolved

// Parse value
$nodeValue = trim((string) $node);
if (!empty($nodeValue)) {
$result['@value'] = $nodeValue;
}

// Include xml:id attribute
if ($this->includeXmlId) {
$xmlId = $node->attributes('xml', true)->id;
$trimmedXmlId = trim(strval($xmlId));
if (!empty($trimmedXmlId)) {
$result['@xml:id'] = $trimmedXmlId;
}
}

Nathmel123 marked this conversation as resolved.
Show resolved Hide resolved
// Check if node is a mixed-content element (if literalString is set to true)

if ($this->includeLiteralString) {
if ($node->getName() == 'p' && $node->count() > 0 && !empty($node)) {
// Add literal string, to store the node order
$literal = str_replace(array("\n", "\r"), '', trim($node->asXML()));
$literal = str_replace('<?xml version="1.0"?>', '', $literal);
$result['@literal'] = $literal;
}
}

$toParse = collect($node->children())->filter(function ($subject) use ($node, &$result) {
foreach ($this->splitSymbols as $symbol) {
if ($subject->getName() == $symbol) {
$result['@link'] = $this->getXmlId($subject);
$this->convertedArray[$this->getXmlId($subject)] = $this->convert($subject);
return false;
}
}
return true;
});

$toParse->each(function ($subject) use (&$result) {
$result = $this->parseChild($subject, $result);
});

return $result;
}

private function parseChild(SimpleXMLElement $child, array $result)
{
$childName = $child->getName();
$childData = $this->convert($child);
// Always parse child nodes as array
if (!isset($result[$childName])) {
$result[$childName] = [];
}
Nathmel123 marked this conversation as resolved.
Show resolved Hide resolved
$result[$childName][] = $childData;

return $result;
}

public function toJson(): string
private function getXmlId(SimpleXMLElement $xml)
{
// add function here
return '';
return strval($xml->attributes('xml', true)->id);
}
}
41 changes: 30 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,38 @@ This comprises the elasticsearch connection and translation of file formats.

You can obtain a Controller with easy access to elasticsearch by inheriting from ClientEnabledController.

use Slub\LisztCommon\Controller\ClientEnabledController;
```php
use Slub\LisztCommon\Controller\ClientEnabledController;

class ActionController extends ClientEnabledController
{
class ActionController extends ClientEnabledController
{

public function ExampleAction()
{
$this->initializeClient();
$params = ...
$entity = $this->elasticClient->search($params);
...
public function ExampleAction()
{
$this->initializeClient();
$params = ...
$entity = $this->elasticClient->search($params);
...

}
}

}
}
```

## Translation between XML and JSON

You can read in an XML document and translate it to a PHP array or JSON.

```php
use Slub\LisztCommon\Common\XmlDocument;
...

$xmlDocument = XmlDocument::from($xmlString);
$array = $xmlDocument->toArray();
$json = $xmlDocument->toJson();
```

# Maintainer

If you have any questions or encounter any problems, please do not hesitate to contact me.
- [Matthias Richter](https://github.com/dikastes)
2 changes: 1 addition & 1 deletion Tests/Functional/Common/Fixtures/minimal.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"minimal_root":{"@xml:id":"minimal_root","subroot":[{"subsubroot":[{"@value":"entry"}]}]}}
10 changes: 9 additions & 1 deletion Tests/Functional/Common/Fixtures/minimal.xml
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@

<?xml version="1.0" encoding="UTF-8"?>
<root xml:id="minimal_root">
<subroot>
<subsubroot>
entry
</subsubroot>
</subroot>

</root>
Loading