Skip to content

Commit 49d5ed8

Browse files
committed
Using $this->updateKeys to determine the current values of updateable properties.
1 parent 2377b06 commit 49d5ed8

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

lib/OpenCloud/Common/Resource/PersistentResource.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,16 +341,18 @@ public function checkExtension($alias)
341341
/**
342342
* Returns the object's properties as an array
343343
*/
344-
protected function getPropertiesAsArray()
344+
protected function getUpdateablePropertiesAsArray()
345345
{
346346
$properties = get_object_vars($this);
347347

348-
$propertiesToRemove = array('aliases', 'service', 'parent', 'metadata');
349-
foreach ($propertiesToRemove as $property) {
350-
unset($properties[$property]);
348+
$propertiesToKeep = array();
349+
foreach ($this->updateKeys as $key) {
350+
if (isset($properties[$key])) {
351+
$propertiesToKeep[$key] = $properties[$key];
352+
}
351353
}
352354

353-
return $properties;
355+
return $propertiesToKeep;
354356
}
355357

356358
/**
@@ -362,7 +364,7 @@ protected function getPropertiesAsArray()
362364
protected function generateJsonPatch($updatedProperties)
363365
{
364366
// Normalize current and updated properties into nested arrays
365-
$currentProperties = json_decode(json_encode($this->getPropertiesAsArray()), true);
367+
$currentProperties = json_decode(json_encode($this->getUpdateablePropertiesAsArray()), true);
366368
$updatedProperties = json_decode(json_encode($updatedProperties), true);
367369

368370
// Add any properties that haven't changed to generate the correct patch

tests/OpenCloud/Tests/Common/Resource/PersistentResourceTest.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,22 @@ class PublicPersistentResource extends PersistentResource
2727
'foo_bar' => 'fooBar'
2828
);
2929

30+
protected $updateKeys = array(
31+
'baz',
32+
'tags',
33+
'domains',
34+
'origins',
35+
'status'
36+
);
37+
3038
public function recursivelyAliasPropertyValue($propertyValue)
3139
{
3240
return parent::recursivelyAliasPropertyValue($propertyValue);
3341
}
3442

35-
public function getPropertiesAsArray()
43+
public function getUpdateablePropertiesAsArray()
3644
{
37-
return parent::getPropertiesAsArray();
45+
return parent::getUpdateablePropertiesAsArray();
3846
}
3947

4048
public function generateJsonPatch($updateParams)
@@ -117,7 +125,7 @@ public function testRecursivelyAliasPropertyValueWithObjects()
117125
$this->persistentResource->recursivelyAliasPropertyValue($obj3));
118126
}
119127

120-
public function testGetPropertiesAsArray()
128+
public function testGetUpdateablePropertiesAsArray()
121129
{
122130
$this->persistentResource->id = 17;
123131
$this->persistentResource->tags = array('foo', 'bar');
@@ -131,7 +139,6 @@ public function testGetPropertiesAsArray()
131139
$this->persistentResource->status = (object) array('message' => 'Creation in progress');
132140

133141
$expectedArray = array(
134-
'id' => 17,
135142
'tags' => array('foo', 'bar'),
136143
'domains' => array(
137144
(object) array('domain' => 'foo.phpopencloud.com'),
@@ -140,10 +147,10 @@ public function testGetPropertiesAsArray()
140147
'origins' => array(
141148
array('origin' => 'origin1.phpopencloud.com'),
142149
),
143-
'status' => (object) array('message' => 'Creation in progress'),
150+
'status' => (object) array('message' => 'Creation in progress')
144151
);
145152

146-
$this->assertEquals($expectedArray, $this->persistentResource->getPropertiesAsArray());
153+
$this->assertEquals($expectedArray, $this->persistentResource->getUpdateablePropertiesAsArray());
147154
}
148155

149156
public function testGenerateJsonPatch()

0 commit comments

Comments
 (0)