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

fix(JSON): Fix nested json #29

Closed
wants to merge 2 commits into from
Closed
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
16 changes: 16 additions & 0 deletions src/SilbinaryWolf/Components/ComponentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function generateTemplateCode(array $res, $parser)
}
foreach ($jsonData as $propertyName => $value) {
if (is_array($value)) {
$value = self::recursivelyConvertFlatArraysToArrayList($value);
$value = 'new '.ArrayList::class.'('.var_export($value, true).')';
}
$phpCodeValueParts[] = "\$_props['".$propertyName."'][] = ".$value.";";
Expand Down Expand Up @@ -244,4 +245,19 @@ public function renderComponent($name, array $props, SSViewer_Scope $scope)
//
return $result;
}

private static function recursivelyConvertFlatArraysToArrayList(array $array)
{
foreach ($array as $prop => &$value) {
if (is_array($value)) {
$value = self::recursivelyConvertFlatArraysToArrayList($value);
}
unset($value);
}
if (isset($array[0])) {
$array = new ArrayList($array);
//$array = 'new '.ArrayList::class.'('.var_export($array, true).')';
}
return $array;
}
}
58 changes: 58 additions & 0 deletions tests/ComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,64 @@ public function testJSONPropertyErrorHandling()
}
}

public function testJSONDeeplyNested()
{
$template = <<<SSTemplate
<:JSONNestedTest
_json='{
"SubMenu": [
{
"Title": "Subnav item number one with children",
"Link": "https://google.com",
"LinkingMode": "section",
"Children": [
{
"Title": "Tertiary item number one",
"Link": "https://google.com",
"LinkingMode": "link"
},
{
"Title": "Tertiary item number two",
"Link": "https://google.com",
"LinkingMode": "link"
},
{
"Title": "Tertiary item number three",
"Link": "https://google.com",
"LinkingMode": "link"
}
]
},
{
"Title": "Subnav item number two",
"Link": "https://google.com",
"LinkingMode": "link",
"Children": ""
},
{
"Title": "Subnav item number three",
"Link": "https://google.com",
"LinkingMode": "current",
"Children": ""
},
{
"Title": "Subnav item number four",
"Link": "https://google.com",
"LinkingMode": "link",
"Children": ""
}
]
}'
/>
SSTemplate;
$expectedHTML = <<<HTML
<div></div>
HTML;

$resultHTML = SSViewer::fromString($template)->process(null);
$this->assertEqualIgnoringWhitespace($expectedHTML, $resultHTML, 'Unexpected output');
}

/**
* Taken from "framework\tests\view\SSViewerTest.php"
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/templates/components/JSONNestedTest.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<% if $SubMenu %>
<% loop $SubMenu %>
<h2>$Title</h2>
<% if $Children %>
<ul>
<% loop $Children %>
<li>
<a class="$LinkingMode" href="$Link">$Title</a>
</li>
<% end_loop %>
</ul>
<% end_if %>
<% end_loop %>
<% end_if %>