Skip to content

Commit

Permalink
fix: setting values of PodioItemField subtypes to empty array had no …
Browse files Browse the repository at this point in the history
…effect
  • Loading branch information
daniel-sc committed Aug 16, 2023
1 parent 7e6633b commit 89cf310
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 63 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* BREAKING: `Podio::debug` changed from public to protected: use `PodioClient::set_debug(..)`
* BREAKING: Kint is now an optional dependency of the package. Use `composer require kint-php/kint` to install it, if you need it.
* Bugfix: Error on fetching single contact with `PodioContact::get`.
* Bugfix: Setting values to empty array of several `PodioItemField` subtypes was broken.
* Bugfix: Debug output via Kint is now working again. #240
* See [migration guide](https://github.com/podio-community/podio-php/blob/master/MIGRATION_GUIDE_v7.md) for details.

Expand Down
30 changes: 14 additions & 16 deletions models/field/PodioAppItemField.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,22 @@ public function humanized_value()

public function set_value($values)
{
if ($values) {
// Ensure that we have an array of values
if (is_a($values, 'PodioCollection')) {
$values = $values->_get_items();
}
if (is_object($values) || (is_array($values) && !empty($values['item_id']))) {
$values = array($values);
}
// Ensure that we have an array of values
if (is_a($values, 'PodioCollection')) {
$values = $values->_get_items();
}
if (is_object($values) || (is_array($values) && !empty($values['item_id']))) {
$values = array($values);
}

$values = array_map(function ($value) {
if (is_object($value)) {
return array('value' => $value->as_json(false));
}
return array('value' => $value);
}, $values);
$values = array_map(function ($value) {
if (is_object($value)) {
return array('value' => $value->as_json(false));
}
return array('value' => $value);
}, $values);

parent::__set('values', $values);
}
parent::__set('values', $values);
}

public function api_friendly_values()
Expand Down
24 changes: 11 additions & 13 deletions models/field/PodioCategoryItemField.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,17 @@ public function api_friendly_values()

public function set_value($values)
{
if ($values) {
if (is_array($values)) {
$formatted_values = array_map(function ($value) {
if (is_array($value)) {
return array('value' => $value);
} else {
return array('value' => array('id' => $value));
}
}, $values);
parent::__set('values', $formatted_values);
} else {
parent::__set('values', array(array('value' => array('id' => $values))));
}
if (is_array($values)) {
$formatted_values = array_map(function ($value) {
if (is_array($value)) {
return array('value' => $value);
} else {
return array('value' => array('id' => $value));
}
}, $values);
parent::__set('values', $formatted_values);
} else {
parent::__set('values', array(array('value' => array('id' => $values))));
}
}

Expand Down
30 changes: 14 additions & 16 deletions models/field/PodioContactItemField.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,22 @@ public function humanized_value()

public function set_value($values)
{
if ($values) {
// Ensure that we have an array of values
if (is_a($values, 'PodioCollection')) {
$values = $values->_get_items();
}
if (is_object($values) || (is_array($values) && !empty($values['profile_id']))) {
$values = array($values);
}
// Ensure that we have an array of values
if (is_a($values, 'PodioCollection')) {
$values = $values->_get_items();
}
if (is_object($values) || (is_array($values) && !empty($values['profile_id']))) {
$values = array($values);
}

$values = array_map(function ($value) {
if (is_object($value)) {
return array('value' => $value->as_json(false));
}
return array('value' => $value);
}, $values);
$values = array_map(function ($value) {
if (is_object($value)) {
return array('value' => $value->as_json(false));
}
return array('value' => $value);
}, $values);

parent::__set('values', $values);
}
parent::__set('values', $values);
}

public function api_friendly_values()
Expand Down
34 changes: 16 additions & 18 deletions models/field/PodioEmbedItemField.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,25 @@ public function humanized_value()

public function set_value($values)
{
if ($values) {
// Ensure that we have an array of values
if (is_a($values, 'PodioCollection')) {
$values = $values->_get_items();
}
if (is_object($values) || (is_array($values) && !empty($values['embed']))) {
$values = array($values);
}
// Ensure that we have an array of values
if (is_a($values, 'PodioCollection')) {
$values = $values->_get_items();
}
if (is_object($values) || (is_array($values) && !empty($values['embed']))) {
$values = array($values);
}

$values = array_map(function ($value) {
if (is_object($value)) {
$file = $value->files ? $value->files[0] : null;
unset($value->files);
$values = array_map(function ($value) {
if (is_object($value)) {
$file = $value->files ? $value->files[0] : null;
unset($value->files);

return array('embed' => $value->as_json(false), 'file' => $file ? $file->as_json(false) : null);
}
return $value;
}, $values);
return array('embed' => $value->as_json(false), 'file' => $file ? $file->as_json(false) : null);
}
return $value;
}, $values);

parent::__set('values', $values);
}
parent::__set('values', $values);
}

public function api_friendly_values()
Expand Down
6 changes: 6 additions & 0 deletions tests/PodioAppItemFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ public function test_can_set_value_from_array_of_hashes(): void
], $this->object->__attribute('values'));
}

public function test_can_set_values_from_empty_array(): void
{
$this->object->values = [];
$this->assertSame([], $this->object->__attribute('values'));
}

public function test_can_humanize_value(): void
{
// Empty values
Expand Down
6 changes: 6 additions & 0 deletions tests/PodioAssetItemFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ public function test_can_set_value_from_array_of_hashes(): void
], $this->object->__attribute('values'));
}

public function test_can_set_value_from_empty_array(): void
{
$this->object->values = [];
$this->assertSame([], $this->object->__attribute('values'));
}

public function test_can_humanize_value(): void
{
// Empty values
Expand Down
6 changes: 6 additions & 0 deletions tests/PodioCategoryItemFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public function test_can_set_values_from_array(): void
$this->assertSame([['value' => ['id' => 4]]], $this->object->__attribute('values'));
}

public function test_can_set_values_from_empty_array(): void
{
$this->object->values = [];
$this->assertSame([], $this->object->__attribute('values'));
}

public function test_can_set_values_from_hash(): void
{
$this->object->values = [['id' => 4, 'text' => 'Captain Crunch']];
Expand Down
6 changes: 6 additions & 0 deletions tests/PodioContactItemFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ public function test_can_set_value_from_array_of_hashes(): void
], $this->object->__attribute('values'));
}

public function test_can_set_value_from_empty_array(): void
{
$this->object->values = [];
$this->assertSame([], $this->object->__attribute('values'));
}

public function test_can_humanize_value(): void
{
// Empty values
Expand Down
6 changes: 6 additions & 0 deletions tests/PodioEmbedItemFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ public function test_can_set_value_from_array_of_hashes(): void
], $this->object->__attribute('values'));
}

public function test_can_set_value_from_empty_array(): void
{
$this->object->values = [];
$this->assertSame([], $this->object->__attribute('values'));
}

public function test_can_humanize_value(): void
{
// Empty values
Expand Down

0 comments on commit 89cf310

Please sign in to comment.