From e26fdc3faac5d57ab3e9466da68cde46a6cd69a7 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 17 Mar 2020 15:09:16 -0400 Subject: [PATCH] APIv4 - convert Result object to array when running through json_encode Before: entire Result object was getting serialized leading to some weird output After: only the array gets serialized --- Civi/Api4/Generic/Result.php | 9 ++++- tests/phpunit/api/v4/Action/ResultTest.php | 39 ++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/api/v4/Action/ResultTest.php diff --git a/Civi/Api4/Generic/Result.php b/Civi/Api4/Generic/Result.php index fa6561a330..675b752e70 100644 --- a/Civi/Api4/Generic/Result.php +++ b/Civi/Api4/Generic/Result.php @@ -22,7 +22,7 @@ namespace Civi\Api4\Generic; * For example, BasicReplaceAction returns ReplaceResult which includes the additional $deleted property to list any items deleted by the operation. * 3. Provide convenience methods like `$result->first()` and `$result->indexBy($field)`. */ -class Result extends \ArrayObject { +class Result extends \ArrayObject implements \JsonSerializable { /** * @var string */ @@ -124,4 +124,11 @@ class Result extends \ArrayObject { return array_column($this->getArrayCopy(), $name, $this->indexedBy); } + /** + * @return array + */ + public function jsonSerialize() { + return $this->getArrayCopy(); + } + } diff --git a/tests/phpunit/api/v4/Action/ResultTest.php b/tests/phpunit/api/v4/Action/ResultTest.php new file mode 100644 index 0000000000..a4594d2fac --- /dev/null +++ b/tests/phpunit/api/v4/Action/ResultTest.php @@ -0,0 +1,39 @@ +setCheckPermissions(FALSE)->setIncludeCustom(FALSE)->execute(); + $json = json_encode($result); + $this->assertStringStartsWith('[{"', $json); + $this->assertTrue(is_array(json_decode($json))); + } + +} -- 2.25.1