diff --git a/src/Google/Model.php b/src/Google/Model.php index d5d25e3c1..2b6e67ad5 100644 --- a/src/Google/Model.php +++ b/src/Google/Model.php @@ -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. diff --git a/tests/general/ApiModelTest.php b/tests/general/ApiModelTest.php index 740afaaaa..a7514f620 100644 --- a/tests/general/ApiModelTest.php +++ b/tests/general/ApiModelTest.php @@ -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); } } diff --git a/tests/youtube/YouTubeTest.php b/tests/youtube/YouTubeTest.php index 5d4239e1c..747548771 100644 --- a/tests/youtube/YouTubeTest.php +++ b/tests/youtube/YouTubeTest.php @@ -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]); } -} +} \ No newline at end of file