Skip to content

Commit 0740a56

Browse files
committed
Classes without a constructor didn't work
A class without a constructor were throwing an error while decoding. This happened because the ReflectionMethod gave an exception if it was missing. I've wrapped the method in a try/catch so this not longer occurs. I also renamed the root name for and XML encoded object to "encoded".
1 parent 0ee3b53 commit 0740a56

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

src/PE/Encoder.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,16 +242,21 @@ protected function _decodeNode($nodeName, $nodeData, EncoderOptions $options, En
242242
}
243243

244244
protected function getRequiredConstructorVariables($className) {
245-
$reflector = new \ReflectionMethod($className, '__construct');
246-
$requiredVariables = $reflector->getParameters();
247-
248-
$arr = array();
249-
foreach ($requiredVariables as $variable) {
250-
if (!$variable->isOptional()) {
251-
array_push($arr, $variable->getName());
245+
try {
246+
$reflector = new \ReflectionMethod($className, '__construct');
247+
$requiredVariables = $reflector->getParameters();
248+
249+
$arr = array();
250+
foreach ($requiredVariables as $variable) {
251+
if (!$variable->isOptional()) {
252+
array_push($arr, $variable->getName());
253+
}
252254
}
255+
return $arr;
256+
}
257+
catch(\Exception $e) {
258+
return array();
253259
}
254-
return $arr;
255260
}
256261

257262

src/PE/Encoders/XmlEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class XmlEncoder extends Encoder {
1313

14-
const ROOT_NODE_NAME = 'tb';
14+
const ROOT_NODE_NAME = 'encoded';
1515

1616
public function encode($object, EncoderOptions $options = null) {
1717
$arr = parent::encode($object, $options);

tests/PE/Samples/Specials/AccessorMethodActionTypeNode.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
class AccessorMethodActionTypeNode {
66

77
private $special;
8-
function __construct() {
9-
10-
}
118

129
public function setSpecial($value) {
1310
$this->special = $value;

tests/PE/Tests/Encoders/XmlEncoderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ public function testEncodeDecode() {
3636
$xmlString = trim(preg_replace('/\s+/', ' ', $encoded->saveXML()));
3737

3838
$this->assertEquals('<?xml version="1.0" encoding="UTF-8"?> '
39-
. '<tb> '
39+
. '<encoded> '
4040
. '<building type="house"> '
4141
. '<animals> '
4242
. '<animal type="cat" name="Cat"/> '
4343
. '</animals> '
4444
. '</building> '
45-
. '</tb>', $xmlString);
45+
. '</encoded>', $xmlString);
4646

4747
$decoded = $this->encoder()->decode($xmlString);
4848
$this->assertArrayHasKey('building', $decoded);
@@ -65,11 +65,11 @@ public function testEncodeDecoded() {
6565
$xmlString = trim(preg_replace('/\s+/', ' ', $encoded->saveXML()));
6666

6767
$this->assertEquals('<?xml version="1.0" encoding="UTF-8"?> '
68-
. '<tb> '
68+
. '<encoded> '
6969
. '<single-child> '
7070
. '<thing thingVar="hello world"/> '
7171
. '</single-child> '
72-
. '</tb>', $xmlString);
72+
. '</encoded>', $xmlString);
7373

7474
$decoded = $this->encoder()->decode($xmlString);
7575
$this->assertArrayHasKey('single-child', $decoded);

0 commit comments

Comments
 (0)