Skip to content

Commit 5a3f38f

Browse files
committed
Model: Fix duplicate response structures
1 parent ce50dc4 commit 5a3f38f

File tree

3 files changed

+38
-29
lines changed

3 files changed

+38
-29
lines changed

src/PHPDraft/Model/HTTPResponse.php

+30-23
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ public function parse(stdClass $object): self
9090
$this->parse_headers($object->attributes->headers);
9191
}
9292

93-
$this->parse_content($object);
93+
foreach ($object->content as $value) {
94+
$this->parse_content($value);
95+
}
9496

9597
return $this;
9698
}
@@ -119,49 +121,54 @@ protected function parse_headers(stdClass $object): void
119121
/**
120122
* Parse request content.
121123
*
122-
* @param stdClass $object An object to parse for content
124+
* @param stdClass $value An object to parse for content
123125
*
124126
* @return void
125127
*/
126-
protected function parse_content(stdClass $object): void
128+
protected function parse_content(stdClass $value): void
127129
{
128-
foreach ($object->content as $value) {
129-
if ($value->element === 'copy') {
130-
$this->description = $value->content;
131-
continue;
132-
}
133-
134-
if ($value->element === 'dataStructure') {
135-
$data_content = is_array($value->content) ? $value->content : [$value->content];
136-
$this->parse_structure($data_content);
137-
continue;
138-
}
130+
if ($value->element === 'copy') {
131+
$this->description = $value->content;
132+
return;
133+
}
139134

135+
if ($value->element === 'asset') {
140136
if (isset($value->attributes->contentType->content)) {
141137
$this->content[$value->attributes->contentType->content] = $value->content;
142138
} elseif (isset($value->attributes->contentType)) {
143139
$this->content[$value->attributes->contentType] = $value->content;
144140
}
141+
return;
142+
}
143+
144+
if ($value->element === 'dataStructure') {
145+
foreach ($value->content->content as $object) {
146+
$this->parse_structure($object);
147+
}
148+
return;
145149
}
146150
}
147151

148152
/**
149153
* Parse structure of the content.
150154
*
151-
* @param stdClass[] $objects Objects containing the structure
155+
* @param stdClass $object Objects containing the structure
152156
*
153157
* @return void
154158
*/
155-
protected function parse_structure(array $objects): void
159+
protected function parse_structure(stdClass $object): void
156160
{
157-
foreach ($objects as $object) {
158-
$deps = [];
159-
$struct = new ObjectStructureElement();
160-
$struct->parse($object, $deps);
161-
$struct->deps = $deps;
162-
163-
$this->structure[] = $struct;
161+
$deps = [];
162+
$struct = new ObjectStructureElement();
163+
$struct->parse($object, $deps);
164+
$struct->deps = $deps;
165+
foreach ($this->structure as $prev) {
166+
if ($struct->__toString() === $prev->__toString()) {
167+
return;
168+
}
164169
}
170+
171+
$this->structure[] = $struct;
165172
}
166173

167174
/**

src/PHPDraft/Model/Tests/HTTPResponseTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function testParseIsCalledStructContentEmpty(): void
145145
{
146146
$this->set_reflection_property_value('parent', $this->parent);
147147

148-
$obj = '{"content":[{"element":"dataStructure", "content":[]}]}';
148+
$obj = '{"content":[{"element":"dataStructure", "content":{"content": {}}}]}';
149149

150150
$this->class->parse(json_decode($obj));
151151

@@ -160,7 +160,7 @@ public function testParseIsCalledStructContent(): void
160160
{
161161
$this->set_reflection_property_value('parent', $this->parent);
162162

163-
$obj = '{"content":[{"element":"dataStructure", "content":[{}]}]}';
163+
$obj = '{"content":[{"element":"dataStructure", "content":{"content": [{}]}}]}';
164164

165165
$this->class->parse(json_decode($obj));
166166

@@ -175,12 +175,12 @@ public function testParseIsCalledStructContentHasAttr(): void
175175
{
176176
$this->set_reflection_property_value('parent', $this->parent);
177177

178-
$obj = '{"content":[{"content":"hello", "attributes":{"contentType":"content"}, "element":"hello"}]}';
178+
$obj = '{"content":[{"content":"hello", "attributes":{"contentType":"content"}, "element":"asset"}]}';
179179

180180
$this->class->parse(json_decode($obj));
181-
182-
$this->assertArrayHasKey('content', $this->get_reflection_property_value('content'));
183-
$this->assertSame('hello', $this->get_reflection_property_value('content')['content']);
181+
$prop = $this->get_reflection_property_value('content');
182+
$this->assertArrayHasKey('content', $prop);
183+
$this->assertSame('hello', $prop['content']);
184184
}
185185

186186
/**

src/PHPDraft/Out/HTML/default/transition.twig

+2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@
129129
{% if response.structure|length > 0 %}
130130
<h5>Data Structure</h5>
131131
<div class="row">
132+
<table class="table table-striped mdl-data-table mdl-js-data-table">
132133
{% for value in response.structure %}{{ value|raw }}{% endfor %}
134+
</table>
133135
</div>
134136
{% endif %}
135137
{% for response_key,value in response.content %}

0 commit comments

Comments
 (0)