Skip to content

Commit

Permalink
Google_Model toSimpleObject didn't convert Arrays
Browse files Browse the repository at this point in the history
Array and Map types weren't be recursively converted, so we
would be returning the raw object. This could lead to nulls or
other unexpected data being sent. Should resolve the underlying issue
for googleapis#52
  • Loading branch information
Ian Barber committed Jan 29, 2014
1 parent c5ee901 commit 8698757
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
35 changes: 27 additions & 8 deletions src/Google/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,42 @@ public function toSimpleObject()
$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
foreach ($props as $member) {
$name = $member->getName();
if ($this->$name instanceof Google_Model) {
$object->$name = $this->$name->toSimpleObject();
} else if ($this->$name !== null) {
$object->$name = $this->$name;
$result = $this->getSimpleValue($this->$name);
if ($result != null) {
$object->$name = $result;
}
}

// Process all other data.
foreach ($this->data as $key => $val) {
if ($val instanceof Google_Model) {
$object->$key = $val->toSimpleObject();
} else if ($val !== null) {
$object->$key = $val;
$result = $this->getSimpleValue($val);
if ($result != null) {
$object->$key = $result;
}
}
return $object;
}

/**
* Handle different types of values, primarily
* other objects and map and array data types.
*/
private function getSimpleValue($value)
{
if ($value instanceof Google_Model) {
return $value->toSimpleObject();
} else if (is_array($value)) {
$return = array();
foreach ($value as $key => $a_value) {
$a_value = $this->getSimpleValue($a_value);
if ($a_value != null) {
$return[$key] = $a_value;
}
}
return $return;
}
return $value;
}

/**
* Returns true only if the array is associative.
Expand Down
7 changes: 7 additions & 0 deletions tests/general/ApiModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,18 @@ public function testJsonStructure() {
$model2->publicC = 12345;
$model2->publicD = null;
$model->publicB = $model2;
$model3 = new Google_Model();
$model3->publicE = 54321;
$model3->publicF = null;
$model->publicG = array($model3, "hello");
$string = json_encode($model->toSimpleObject());
$data = json_decode($string, true);
$this->assertEquals(12345, $data['publicB']['publicC']);
$this->assertEquals("This is a string", $data['publicA']);
$this->assertArrayNotHasKey("publicD", $data['publicB']);
$this->assertArrayHasKey("publicE", $data['publicG'][0]);
$this->assertArrayNotHasKey("publicF", $data['publicG'][0]);
$this->assertEquals("hello", $data['publicG'][1]);
$this->assertArrayNotHasKey("data", $data);
}
}
10 changes: 9 additions & 1 deletion tests/youtube/YouTubeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,13 @@ public function testMissingFieldsAreNull() {
$simpleManual = $o_channel->toSimpleObject();
$this->assertObjectHasAttribute('timeLinked', $simpleManual->contentOwnerDetails);
$this->assertObjectNotHasAttribute('contentOwner', $simpleManual->contentOwnerDetails);

$ping = new Google_Service_YouTube_ChannelConversionPing();
$ping->setContext("hello");
$pings = new Google_Service_YouTube_ChannelConversionPings();
$pings->setPings(array($ping));
$simplePings = $pings->toSimpleObject();
$this->assertObjectHasAttribute('context', $simplePings->pings[0]);
$this->assertObjectNotHasAttribute('conversionUrl', $simplePings->pings[0]);
}
}
}

0 comments on commit 8698757

Please sign in to comment.