Skip to content

Commit 8698757

Browse files
author
Ian Barber
committed
Google_Model toSimpleObject didn't convert Arrays
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 #52
1 parent c5ee901 commit 8698757

3 files changed

Lines changed: 43 additions & 9 deletions

File tree

src/Google/Model.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,23 +113,42 @@ public function toSimpleObject()
113113
$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
114114
foreach ($props as $member) {
115115
$name = $member->getName();
116-
if ($this->$name instanceof Google_Model) {
117-
$object->$name = $this->$name->toSimpleObject();
118-
} else if ($this->$name !== null) {
119-
$object->$name = $this->$name;
116+
$result = $this->getSimpleValue($this->$name);
117+
if ($result != null) {
118+
$object->$name = $result;
120119
}
121120
}
122121

123122
// Process all other data.
124123
foreach ($this->data as $key => $val) {
125-
if ($val instanceof Google_Model) {
126-
$object->$key = $val->toSimpleObject();
127-
} else if ($val !== null) {
128-
$object->$key = $val;
124+
$result = $this->getSimpleValue($val);
125+
if ($result != null) {
126+
$object->$key = $result;
129127
}
130128
}
131129
return $object;
132130
}
131+
132+
/**
133+
* Handle different types of values, primarily
134+
* other objects and map and array data types.
135+
*/
136+
private function getSimpleValue($value)
137+
{
138+
if ($value instanceof Google_Model) {
139+
return $value->toSimpleObject();
140+
} else if (is_array($value)) {
141+
$return = array();
142+
foreach ($value as $key => $a_value) {
143+
$a_value = $this->getSimpleValue($a_value);
144+
if ($a_value != null) {
145+
$return[$key] = $a_value;
146+
}
147+
}
148+
return $return;
149+
}
150+
return $value;
151+
}
133152

134153
/**
135154
* Returns true only if the array is associative.

tests/general/ApiModelTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@ public function testJsonStructure() {
2828
$model2->publicC = 12345;
2929
$model2->publicD = null;
3030
$model->publicB = $model2;
31+
$model3 = new Google_Model();
32+
$model3->publicE = 54321;
33+
$model3->publicF = null;
34+
$model->publicG = array($model3, "hello");
3135
$string = json_encode($model->toSimpleObject());
3236
$data = json_decode($string, true);
3337
$this->assertEquals(12345, $data['publicB']['publicC']);
3438
$this->assertEquals("This is a string", $data['publicA']);
3539
$this->assertArrayNotHasKey("publicD", $data['publicB']);
40+
$this->assertArrayHasKey("publicE", $data['publicG'][0]);
41+
$this->assertArrayNotHasKey("publicF", $data['publicG'][0]);
42+
$this->assertEquals("hello", $data['publicG'][1]);
3643
$this->assertArrayNotHasKey("data", $data);
3744
}
3845
}

tests/youtube/YouTubeTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,13 @@ public function testMissingFieldsAreNull() {
7272
$simpleManual = $o_channel->toSimpleObject();
7373
$this->assertObjectHasAttribute('timeLinked', $simpleManual->contentOwnerDetails);
7474
$this->assertObjectNotHasAttribute('contentOwner', $simpleManual->contentOwnerDetails);
75+
76+
$ping = new Google_Service_YouTube_ChannelConversionPing();
77+
$ping->setContext("hello");
78+
$pings = new Google_Service_YouTube_ChannelConversionPings();
79+
$pings->setPings(array($ping));
80+
$simplePings = $pings->toSimpleObject();
81+
$this->assertObjectHasAttribute('context', $simplePings->pings[0]);
82+
$this->assertObjectNotHasAttribute('conversionUrl', $simplePings->pings[0]);
7583
}
76-
}
84+
}

0 commit comments

Comments
 (0)