Skip to content

Commit f6f795a

Browse files
dplewisBenjamin Wilson Friedman
authored andcommitted
PFObject associativeArray to _encode fix
* Associative array to encode error encoding and decoding associative returns incorrect value * ParseObject associativeArray to _encode Encoding and decoding associative array returns incorrect value * Removed verbose code
1 parent 83350f2 commit f6f795a

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/Parse/ParseObject.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -909,11 +909,11 @@ public function _encode()
909909
$out[$key] = $value->_encode();
910910
} elseif (is_array($value)) {
911911
$out[$key] = [];
912-
foreach ($value as $item) {
912+
foreach ($value as $itemKey => $item) {
913913
if (is_object($item) && $item instanceof Encodable) {
914-
$out[$key][] = $item->_encode();
914+
$out[$key][$itemKey] = $item->_encode();
915915
} else {
916-
$out[$key][] = $item;
916+
$out[$key][$itemKey] = $item;
917917
}
918918
}
919919
} else {
@@ -934,7 +934,7 @@ private function getSaveJSON()
934934
$this->beforeSave();
935935
return ParseClient::_encode($this->operationSet, true);
936936
}
937-
937+
938938
/**
939939
* Before save stub
940940
*

tests/Parse/ParseObjectTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,29 @@ public function testToJSONUnsavedObject()
728728
$this->assertTrue(isset($decoded->foo));
729729
}
730730

731+
public function testAssocToJSONSavedObject()
732+
{
733+
$obj = ParseObject::create('TestObject');
734+
$assoc = ["foo" => "bar", "baz" => "yay"];
735+
$obj->setAssociativeArray('obj', $assoc);
736+
$obj->save();
737+
$json = $obj->_encode();
738+
$decoded = json_decode($json, true);
739+
$this->assertEquals($decoded['obj'], $assoc);
740+
$this->assertEquals($obj->get('obj'), $assoc);
741+
}
742+
743+
public function testAssocToJSONUnsavedObject()
744+
{
745+
$obj = ParseObject::create('TestObject');
746+
$assoc = ["foo" => "bar", "baz" => "yay"];
747+
$obj->setAssociativeArray('obj', $assoc);
748+
$json = $obj->_encode();
749+
$decoded = json_decode($json, true);
750+
$this->assertEquals($decoded['obj'], $assoc);
751+
$this->assertEquals($obj->get('obj'), $assoc);
752+
}
753+
731754
public function testRemoveOperation()
732755
{
733756
$obj = ParseObject::create('TestObject');
@@ -1229,7 +1252,7 @@ public function testSetWithArrayValue()
12291252
);
12301253
$obj = new ParseObject('TestClass');
12311254
$obj->set('key', ['is-an-array' => 'yes']);
1232-
1255+
12331256
}
12341257

12351258
public function testSetArrayNullKey()

0 commit comments

Comments
 (0)