Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions lib/Service/Advice/AdviceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use OCA\Dossiq\AppInfo\Application;
use OCA\Dossiq\Service\SettingsService;
use OCA\Dossiq\Service\Support\SearchesObjects;
use OCA\Dossiq\Service\Support\SerialisedArray;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Throwable;
Expand Down Expand Up @@ -232,17 +233,6 @@ public function save(array $update, string $adviceId): array {
* @spec openspec/specs/advice-management/spec.md
*/
public function normalize(mixed $result): array {
if (is_array($result) === true) {
return $result;
}

if (is_object($result) === true && method_exists($result, 'jsonSerialize') === true) {
$data = $result->jsonSerialize();
if (is_array($data) === true) {
return $data;
}
}

return [];
return SerialisedArray::of(value: $result);
}//end normalize()
}//end class
14 changes: 2 additions & 12 deletions lib/Service/DsoCaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCA\Dossiq\AppInfo\Application;
use OCA\Dossiq\Service\Lifecycle\CaseJournal;
use OCA\Dossiq\Service\Support\SearchesObjects;
use OCA\Dossiq\Service\Support\SerialisedArray;
use OCA\OpenRegister\Contract\ObjectServiceInterface;
use OCP\IAppConfig;
use OCP\IUser;
Expand Down Expand Up @@ -437,18 +438,7 @@ private function getObjectService(): object {
* @return array<string, mixed> The normalised array (empty when not coercible).
*/
private function normalizeToArray(mixed $value): array {
if (is_array($value) === true) {
return $value;
}

if (is_object($value) === true && method_exists($value, 'jsonSerialize') === true) {
$serialized = $value->jsonSerialize();
if (is_array($serialized) === true) {
return $serialized;
}
}

return [];
return SerialisedArray::of(value: $value);
}//end normalizeToArray()

/**
Expand Down
14 changes: 2 additions & 12 deletions lib/Service/FormsIntakeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
namespace OCA\Dossiq\Service;

use OCA\Dossiq\Service\Intake\IntakeFormBinding;
use OCA\Dossiq\Service\Support\SerialisedArray;
use OCA\OpenRegister\Service\Form\FormSubmission;
use OCA\OpenRegister\Service\Form\FormSubmitter;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -351,18 +352,7 @@ private function rowsOf(mixed $result): array {
* @return array<string, mixed> The row as an array.
*/
private function arrayOf(mixed $row): array {
if (is_array($row) === true) {
return $row;
}

if (is_object($row) === true && method_exists($row, 'jsonSerialize') === true) {
$serialised = $row->jsonSerialize();
if (is_array($serialised) === true) {
return $serialised;
}
}

return [];
return SerialisedArray::of(value: $row);
}//end arrayOf()

/**
Expand Down
14 changes: 2 additions & 12 deletions lib/Service/Intake/CaseIntakeFiling.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use OCA\Dossiq\Exception\RefusedException;
use OCA\Dossiq\Service\CaseTypeStore;
use OCA\Dossiq\Service\SettingsService;
use OCA\Dossiq\Service\Support\SerialisedArray;
use Psr\Log\LoggerInterface;
use Throwable;

Expand Down Expand Up @@ -270,17 +271,6 @@ private function reasonOf(Throwable $error): string {
* @return array<string, mixed> The array.
*/
private function asArray(mixed $value): array {
if (is_array($value) === true) {
return $value;
}

if (is_object($value) === true && method_exists($value, 'jsonSerialize') === true) {
$serialised = $value->jsonSerialize();
if (is_array($serialised) === true) {
return $serialised;
}
}

return [];
return SerialisedArray::of(value: $value);
}//end asArray()
}//end class
14 changes: 2 additions & 12 deletions lib/Service/Intake/ChannelIntake.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
use OCA\Dossiq\Service\Email\IntakeLog;
use OCA\Dossiq\Service\SettingsService;
use OCA\Dossiq\Service\Support\SearchesObjects;
use OCA\Dossiq\Service\Support\SerialisedArray;
use Psr\Log\LoggerInterface;
use Throwable;

Expand Down Expand Up @@ -479,17 +480,6 @@ private function refuse(string $channel, string $externalId, array $message, str
* @return array<string, mixed> The row, or an empty array.
*/
private function objectAsArray(mixed $value): array {
if (is_array($value) === true) {
return $value;
}

if (is_object($value) === true && method_exists($value, 'jsonSerialize') === true) {
$serialized = $value->jsonSerialize();
if (is_array($serialized) === true) {
return $serialized;
}
}

return [];
return SerialisedArray::of(value: $value);
}//end objectAsArray()
}//end class
59 changes: 59 additions & 0 deletions lib/Service/Support/SerialisedArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* An OpenRegister result as an array, through its own serialisation only
*
* OpenRegister answers a plain array or an entity depending on the call path.
* Many readers keep an array, take an entity's jsonSerialize() array, and
* treat anything else as empty. Unlike NormalisesObjectRows::objectToArray(),
* an object without jsonSerialize() is not cast: it is empty.
*
* @category Service
* @package OCA\Dossiq\Service\Support
*
* @author Conduction Development Team <info@conduction.nl>
* @copyright 2026 Conduction B.V.
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* SPDX-License-Identifier: EUPL-1.2
* SPDX-FileCopyrightText: 2026 Conduction B.V. <info@conduction.nl>
*
* @link https://conduction.nl
*
* @spec openspec/specs/case-management/spec.md
*/

declare(strict_types=1);

namespace OCA\Dossiq\Service\Support;

/**
* Turns a result into the array it serialises to, or an empty array.
*
* @spec openspec/specs/case-management/spec.md
*/
final class SerialisedArray {
/**
* The result as an array: itself, or its jsonSerialize() array, or empty.
*
* @param mixed $value An array, an entity or anything else OpenRegister answered.
*
* @return array<mixed> The array; empty when the value serialises to no array.
*
* @spec openspec/specs/case-management/spec.md
*/
public static function of(mixed $value): array {
if (is_array($value) === true) {
return $value;
}

if (is_object($value) === true && method_exists($value, 'jsonSerialize') === true) {
$serialised = $value->jsonSerialize();
if (is_array($serialised) === true) {
return $serialised;
}
}

return [];
}//end of()
}//end class
14 changes: 2 additions & 12 deletions lib/Service/Transitions/CapacityGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
namespace OCA\Dossiq\Service\Transitions;

use OCA\Dossiq\Service\SettingsService;
use OCA\Dossiq\Service\Support\SerialisedArray;
use OCP\IL10N;
use Throwable;

Expand Down Expand Up @@ -370,17 +371,6 @@ private static function firstId(array $row): string {
* @return array<string, mixed> The row.
*/
private function arrayOf(mixed $row): array {
if (is_array($row) === true) {
return $row;
}

if (is_object($row) === true && method_exists($row, 'jsonSerialize') === true) {
$serialised = $row->jsonSerialize();
if (is_array($serialised) === true) {
return $serialised;
}
}

return [];
return SerialisedArray::of(value: $row);
}//end arrayOf()
}//end class
149 changes: 149 additions & 0 deletions tests/Unit/Service/Support/SerialisedArrayIntakeCopiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

/**
* How the intake, permit, capacity and advice readers turn a result into an array.
*
* Pins each copy (normalizeToArray(), arrayOf(), asArray(), objectAsArray(),
* normalize()) before they share one helper: an array is
* kept, an object's jsonSerialize() array is used, and anything else (an
* object without it, a serialisation that is not an array, a scalar, null)
* becomes an empty array. The copies hold no state, so they are reached by
* reflection on an instance built without its constructor.
*
* @category Tests
* @package OCA\Dossiq\Tests\Unit\Service\Support
*
* @author Conduction Development Team <info@conduction.nl>
* @copyright 2026 Conduction B.V.
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* @link https://conduction.nl
*
* SPDX-FileCopyrightText: 2026 Conduction B.V. <info@conduction.nl>
* SPDX-License-Identifier: EUPL-1.2
*
* @spec openspec/specs/case-management/spec.md
*/

declare(strict_types=1);

namespace OCA\Dossiq\Tests\Unit\Service\Support;

use ArrayObject;
use JsonSerializable;
use OCA\Dossiq\Service\Advice\AdviceRepository;
use OCA\Dossiq\Service\DsoCaseService;
use OCA\Dossiq\Service\FormsIntakeService;
use OCA\Dossiq\Service\Intake\CaseIntakeFiling;
use OCA\Dossiq\Service\Intake\ChannelIntake;
use OCA\Dossiq\Service\Transitions\CapacityGuard;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionMethod;
use stdClass;

/**
* Every copy answers the same array for the same result.
*
* @covers \OCA\Dossiq\Service\Advice\AdviceRepository
* @covers \OCA\Dossiq\Service\DsoCaseService
* @covers \OCA\Dossiq\Service\FormsIntakeService
* @covers \OCA\Dossiq\Service\Intake\CaseIntakeFiling
* @covers \OCA\Dossiq\Service\Intake\ChannelIntake
* @covers \OCA\Dossiq\Service\Transitions\CapacityGuard
*
* @spec openspec/specs/case-management/spec.md
*/
class SerialisedArrayIntakeCopiesTest extends TestCase {
/**
* Every class carrying a copy, crossed with every kind of result.
*
* @return array<string, array{0: string, 1: mixed, 2: array<mixed>}>
*/
public static function cases(): array {
$classes = [
AdviceRepository::class => 'normalize',
DsoCaseService::class => 'normalizeToArray',
FormsIntakeService::class => 'arrayOf',
CaseIntakeFiling::class => 'asArray',
ChannelIntake::class => 'objectAsArray',
CapacityGuard::class => 'arrayOf',
];
$cases = [];
foreach ($classes as $class => $method) {
foreach (self::results() as $kind => [$value, $expected]) {
$cases[$class . ' ' . $kind] = [$class . '::' . $method, $value, $expected];
}
}

return $cases;
}//end cases()

/**
* Each kind of result with the array it becomes.
*
* @return array<string, array{0: mixed, 1: array<mixed>}>
*/
private static function results(): array {
$object = new stdClass();
$object->id = 'plain';

return [
'array' => [['id' => 'a'], ['id' => 'a']],
'empty array' => [[], []],
'serialisable to an array' => [self::serialisable(value: ['id' => 'b']), ['id' => 'b']],
'serialisable to a string' => [self::serialisable(value: 'b'), []],
'object without jsonSerialize' => [$object, []],
'array object without jsonSerialize' => [new ArrayObject(['id' => 'c']), []],
'string' => ['abc', []],
'integer' => [7, []],
'null' => [null, []],
];
}//end results()

/**
* A result whose jsonSerialize() answers the given value.
*
* @param mixed $value What jsonSerialize() answers.
*
* @return JsonSerializable The result.
*/
private static function serialisable(mixed $value): JsonSerializable {
return new class ($value) implements JsonSerializable {
/**
* Constructor.
*
* @param mixed $value What jsonSerialize() answers.
*/
public function __construct(private readonly mixed $value) {
}//end __construct()

/**
* The configured value.
*
* @return mixed The value.
*/
public function jsonSerialize(): mixed {
return $this->value;
}//end jsonSerialize()
};
}//end serialisable()

/**
* The copy turns the result into the expected array.
*
* @param string $copy The copy, as Class::method.
* @param mixed $value The result.
* @param array<mixed> $expected The array it becomes.
*
* @dataProvider cases
*
* @return void
*/
public function testTheResultBecomesTheExpectedArray(string $copy, mixed $value, array $expected): void {
[$class, $method] = explode(separator: '::', string: $copy);
$subject = (new ReflectionClass($class))->newInstanceWithoutConstructor();

self::assertSame(expected: $expected, actual: (new ReflectionMethod($subject, $method))->invoke($subject, $value));
}//end testTheResultBecomesTheExpectedArray()
}//end class