|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * An OpenRegister result as an array, through its own serialisation only |
| 5 | + * |
| 6 | + * OpenRegister answers a plain array or an entity depending on the call path. |
| 7 | + * Many readers keep an array, take an entity's jsonSerialize() array, and |
| 8 | + * treat anything else as empty. Unlike NormalisesObjectRows::objectToArray(), |
| 9 | + * an object without jsonSerialize() is not cast: it is empty. |
| 10 | + * |
| 11 | + * @category Service |
| 12 | + * @package OCA\Dossiq\Service\Support |
| 13 | + * |
| 14 | + * @author Conduction Development Team <info@conduction.nl> |
| 15 | + * @copyright 2026 Conduction B.V. |
| 16 | + * @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 |
| 17 | + * |
| 18 | + * SPDX-License-Identifier: EUPL-1.2 |
| 19 | + * SPDX-FileCopyrightText: 2026 Conduction B.V. <info@conduction.nl> |
| 20 | + * |
| 21 | + * @link https://conduction.nl |
| 22 | + * |
| 23 | + * @spec openspec/specs/case-management/spec.md |
| 24 | + */ |
| 25 | + |
| 26 | +declare(strict_types=1); |
| 27 | + |
| 28 | +namespace OCA\Dossiq\Service\Support; |
| 29 | + |
| 30 | +/** |
| 31 | + * Turns a result into the array it serialises to, or an empty array. |
| 32 | + * |
| 33 | + * @spec openspec/specs/case-management/spec.md |
| 34 | + */ |
| 35 | +final class SerialisedArray { |
| 36 | + /** |
| 37 | + * The result as an array: itself, or its jsonSerialize() array, or empty. |
| 38 | + * |
| 39 | + * @param mixed $value An array, an entity or anything else OpenRegister answered. |
| 40 | + * |
| 41 | + * @return array<mixed> The array; empty when the value serialises to no array. |
| 42 | + * |
| 43 | + * @spec openspec/specs/case-management/spec.md |
| 44 | + */ |
| 45 | + public static function of(mixed $value): array { |
| 46 | + if (is_array($value) === true) { |
| 47 | + return $value; |
| 48 | + } |
| 49 | + |
| 50 | + if (is_object($value) === true && method_exists($value, 'jsonSerialize') === true) { |
| 51 | + $serialised = $value->jsonSerialize(); |
| 52 | + if (is_array($serialised) === true) { |
| 53 | + return $serialised; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return []; |
| 58 | + }//end of() |
| 59 | +}//end class |
0 commit comments