Skip to content

Commit f3a84d1

Browse files
authored
addresse keytype conflict (#1310)
1 parent c144e99 commit f3a84d1

File tree

3 files changed

+41
-35
lines changed

3 files changed

+41
-35
lines changed

src/Google/Collection.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,10 @@ public function offsetUnset($offset)
9191

9292
private function coerceType($offset)
9393
{
94-
$typeKey = $this->keyType($this->collection_key);
95-
if (isset($this->$typeKey) && !is_object($this->{$this->collection_key}[$offset])) {
96-
$type = $this->$typeKey;
94+
$keyType = $this->keyType($this->collection_key);
95+
if ($keyType && !is_object($this->{$this->collection_key}[$offset])) {
9796
$this->{$this->collection_key}[$offset] =
98-
new $type($this->{$this->collection_key}[$offset]);
97+
new $keyType($this->{$this->collection_key}[$offset]);
9998
}
10099
}
101100
}

src/Google/Model.php

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,30 @@ final public function __construct()
5353
*/
5454
public function __get($key)
5555
{
56-
$keyTypeName = $this->keyType($key);
56+
$keyType = $this->keyType($key);
5757
$keyDataType = $this->dataType($key);
58-
if (isset($this->$keyTypeName) && !isset($this->processed[$key])) {
58+
if ($keyType && !isset($this->processed[$key])) {
5959
if (isset($this->modelData[$key])) {
6060
$val = $this->modelData[$key];
61-
} else if (isset($this->$keyDataType) &&
62-
($this->$keyDataType == 'array' || $this->$keyDataType == 'map')) {
61+
} elseif ($keyDataType == 'array' || $keyDataType == 'map') {
6362
$val = array();
6463
} else {
6564
$val = null;
6665
}
6766

6867
if ($this->isAssociativeArray($val)) {
69-
if (isset($this->$keyDataType) && 'map' == $this->$keyDataType) {
68+
if ($keyDataType && 'map' == $keyDataType) {
7069
foreach ($val as $arrayKey => $arrayItem) {
7170
$this->modelData[$key][$arrayKey] =
72-
$this->createObjectFromName($keyTypeName, $arrayItem);
71+
new $keyType($arrayItem);
7372
}
7473
} else {
75-
$this->modelData[$key] = $this->createObjectFromName($keyTypeName, $val);
74+
$this->modelData[$key] = new $keyType($val);
7675
}
7776
} else if (is_array($val)) {
7877
$arrayObject = array();
7978
foreach ($val as $arrayIndex => $arrayItem) {
80-
$arrayObject[$arrayIndex] =
81-
$this->createObjectFromName($keyTypeName, $arrayItem);
79+
$arrayObject[$arrayIndex] = new $keyType($arrayItem);
8280
}
8381
$this->modelData[$key] = $arrayObject;
8482
}
@@ -98,22 +96,21 @@ protected function mapTypes($array)
9896
{
9997
// Hard initialise simple types, lazy load more complex ones.
10098
foreach ($array as $key => $val) {
101-
if (property_exists($this, $this->keyType($key))) {
102-
$propertyClass = $this->{$this->keyType($key)};
103-
$dataType = $this->{$this->dataType($key)};
99+
if ($keyType = $this->keyType($key)) {
100+
$dataType = $this->dataType($key);
104101
if ($dataType == 'array' || $dataType == 'map') {
105102
$this->$key = array();
106103
foreach ($val as $itemKey => $itemVal) {
107-
if ($itemVal instanceof $propertyClass) {
104+
if ($itemVal instanceof $keyType) {
108105
$this->{$key}[$itemKey] = $itemVal;
109106
} else {
110-
$this->{$key}[$itemKey] = new $propertyClass($itemVal);
107+
$this->{$key}[$itemKey] = new $keyType($itemVal);
111108
}
112109
}
113-
} elseif ($val instanceof $propertyClass) {
110+
} elseif ($val instanceof $keyType) {
114111
$this->$key = $val;
115112
} else {
116-
$this->$key = new $propertyClass($val);
113+
$this->$key = new $keyType($val);
117114
}
118115
unset($array[$key]);
119116
} elseif (property_exists($this, $key)) {
@@ -234,19 +231,6 @@ protected function isAssociativeArray($array)
234231
return false;
235232
}
236233

237-
/**
238-
* Given a variable name, discover its type.
239-
*
240-
* @param $name
241-
* @param $item
242-
* @return object The object from the item.
243-
*/
244-
private function createObjectFromName($name, $item)
245-
{
246-
$type = $this->$name;
247-
return new $type($item);
248-
}
249-
250234
/**
251235
* Verify if $obj is an array.
252236
* @throws Google_Exception Thrown if $obj isn't an array.
@@ -291,12 +275,21 @@ public function offsetUnset($offset)
291275

292276
protected function keyType($key)
293277
{
294-
return $key . "Type";
278+
$keyType = $key . "Type";
279+
280+
// ensure keyType is a valid class
281+
if (property_exists($this, $keyType) && class_exists($this->$keyType)) {
282+
return $this->$keyType;
283+
}
295284
}
296285

297286
protected function dataType($key)
298287
{
299-
return $key . "DataType";
288+
$dataType = $key . "DataType";
289+
290+
if (property_exists($this, $dataType)) {
291+
return $this->$dataType;
292+
}
300293
}
301294

302295
public function __isset($key)

tests/Google/ModelTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,18 @@ public function testPassingInstanceInConstructorForMap()
264264
$this->assertEquals('#FFF', $collection->calendar['regular']->getBackground());
265265
$this->assertEquals('#FFF', $collection->calendar['inverted']->getForeground());
266266
}
267+
268+
/**
269+
* @see https://github.com/google/google-api-php-client/issues/1308
270+
*/
271+
public function testKeyTypePropertyConflict()
272+
{
273+
$data = [
274+
"duration" => 0,
275+
"durationType" => "unknown",
276+
];
277+
$creativeAsset = new Google_Service_Dfareporting_CreativeAsset($data);
278+
$this->assertEquals(0, $creativeAsset->getDuration());
279+
$this->assertEquals('unknown', $creativeAsset->getDurationType());
280+
}
267281
}

0 commit comments

Comments
 (0)