Skip to content

Commit

Permalink
Some changes in format
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathmel123 committed Sep 5, 2024
1 parent cb7266e commit 703ad57
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 82 deletions.
13 changes: 10 additions & 3 deletions Classes/Common/XmlDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,17 @@ 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) {
$result[$id] = json_encode($value, JSON_PRETTY_PRINT);
$result[$id] = json_encode($value);
$result[$id] = json_decode($result[$id], true);
}
return trim(implode($result));
return trim(json_encode($result));
}

protected function convert(SimpleXMLElement $node): array
Expand Down Expand Up @@ -124,7 +131,7 @@ protected function convert(SimpleXMLElement $node): array
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);
$literal = str_replace('<?xml version="1.0"?>', '', $literal);
$result['@literal'] = $literal;
}
}
Expand Down
12 changes: 1 addition & 11 deletions Tests/Functional/Common/Fixtures/minimal.json
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
{
"subroot": [
{
"subsubroot": [
{
"@value": "entry"
}
]
}
]
}
{"minimal_root":{"@xml:id":"minimal_root","subroot":[{"subsubroot":[{"@value":"entry"}]}]}}
2 changes: 1 addition & 1 deletion Tests/Functional/Common/Fixtures/minimal.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<root>
<root xml:id="minimal_root">
<subroot>
<subsubroot>
entry
Expand Down
82 changes: 15 additions & 67 deletions Tests/Unit/Common/XmlDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ public function xmlIdIsIncluded()
*/
public function testMixedContentIsIncluded()
{
$mixedContentString = '<p> I am <b> mixed </b> content </p>';
$mixedContentString = '<mei xml:id="mei_head"> <p> I am <b> mixed </b> content </p> </mei>';
$subject = XmlDocument::from($mixedContentString);
$subject->setLiteralString(true);
$expected = '"@literal": "<p> I am <b> mixed <\/b> content <\/p>"';
$expected = '"@literal":"<p> I am <b> mixed <\/b> content <\/p>"';
self::assertStringContainsString($expected, $subject->toJson());
}

Expand All @@ -95,26 +95,25 @@ public function testMixedContentIsNotIncluded()
*/
public function testConvertedAttribute()
{
$subject = XmlDocument::from('<item id="1" name="ExampleItem" />');
$subject = XmlDocument::from('<item id="1" xml:id="item_test" name="ExampleItem" />');

$expected = '{
"@attributes": {
"id": "1",
"name": "ExampleItem"
}
}';
$expected = '{"item_test":{"@attributes":{"id":"1","name":"ExampleItem"},"@xml:id":"item_test"}}';

self::assertSame(json_decode($expected, true), json_decode($subject->toJson(), true));
self::assertSame($expected, $subject->toJson());
}

/**
* @test
*/
public function testPlainText()
{
$subject = XmlDocument::from('<p>I am plain text</p>');
$expected = '{"@value": "I am plain text"}';
self::assertSame(json_decode($subject->toJson(), true), json_decode($expected, true));
$subject = XmlDocument::from('<p xml:id="testid">I am plain text</p>');
$expected = '{"testid": {
"@value": "I am plain text",
"@xml:id": "testid" }
}';

self::assertJsonStringEqualsJsonString($expected, $subject->toJson());
}

/**
Expand All @@ -123,7 +122,7 @@ public function testPlainText()
public function testSplitSymbols()
{
$xmlString = '
<mei xmlns="http://www.music-encoding.org/ns/mei">
<mei xml:id="mei_head" xmlns="http://www.music-encoding.org/ns/mei">
<music>
<body>
<mdiv xml:id="TC-01">
Expand All @@ -142,61 +141,10 @@ public function testSplitSymbols()
';

$jsonString = '{
"@xml:id": "TC-01",
"measure": [
{
"@attributes": {
"n": "1"
},
"staff": [
{
"@attributes": {
"n": "1"
},
"layer": [
{
"@attributes": {
"n": "1"
},
"note": [
{
"@attributes": {
"pname": "e",
"oct": "4",
"dur": "4"
},
"@xml:id": "N2"
}
]
}
]
}
]
}
]
}{
"music": [
{
"body": [
{
"@link": "TC-01"
}
]
}
]
}';
$expected = '{"TC-01":{"@xml:id":"TC-01","measure":[{"@attributes":{"n":"1"},"staff":[{"@attributes":{"n":"1"},"layer":[{"@attributes":{"n":"1"},"note":[{"@attributes":{"pname":"e","oct":"4","dur":"4"},"@xml:id":"N2"}]}]}]}]},"mei_head":{"@xml:id":"mei_head","music":[{"body":[{"@link":"TC-01"}]}]}}';

$subject = XmlDocument::from($xmlString)->setSplitSymbols(['mdiv']);

//TODO: Should compare Json to Json, but splitsymbol Strings are not valid
//->Discuss
self::assertEquals(trim($this->processString($jsonString)),trim($this->processString($subject->toJson())));
}

private function processString(String $str) {

return str_replace(array("\n"," "),'',$str);

self::assertJsonStringEqualsJsonString($expected, $subject->toJson());
}
}

0 comments on commit 703ad57

Please sign in to comment.