|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Dossiq record field shape. |
| 5 | + * |
| 6 | + * A raw value of a source record in the shape its field declaration asks |
| 7 | + * for: JSON, a boolean, a date, a moment, an integer, a number, a value from |
| 8 | + * a declared list, or text with its whitespace collapsed and cut at a word. |
| 9 | + * |
| 10 | + * @category Service |
| 11 | + * @package OCA\Dossiq\Service\Import |
| 12 | + * |
| 13 | + * @author Conduction Development Team <info@conduction.nl> |
| 14 | + * @copyright 2026 Conduction B.V. |
| 15 | + * @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 |
| 16 | + * |
| 17 | + * @version GIT: <git-id> |
| 18 | + * |
| 19 | + * @link https://conduction.nl |
| 20 | + * |
| 21 | + * SPDX-License-Identifier: EUPL-1.2 |
| 22 | + * SPDX-FileCopyrightText: 2026 Conduction B.V. <info@conduction.nl> |
| 23 | + * |
| 24 | + * @spec openspec/changes/method-decomposition/tasks.md |
| 25 | + */ |
| 26 | + |
| 27 | +declare(strict_types=1); |
| 28 | + |
| 29 | +namespace OCA\Dossiq\Service\Import; |
| 30 | + |
| 31 | +use DateTimeInterface; |
| 32 | +use InvalidArgumentException; |
| 33 | +use OCA\Dossiq\Service\CaseDateNormaliser; |
| 34 | + |
| 35 | +/** |
| 36 | + * One declared field's value, shaped; reads no store. |
| 37 | + * |
| 38 | + * @spec openspec/specs/case-record-import/spec.md#requirement-a-declaration-reads-nested-typed-source-values-and-answers-the-case-types-fields-req-cri-005 |
| 39 | + * @spec openspec/changes/method-decomposition/tasks.md |
| 40 | + */ |
| 41 | +class RecordFieldShape { |
| 42 | + |
| 43 | + /** |
| 44 | + * The words `as: boolean` reads as true. |
| 45 | + */ |
| 46 | + private const TRUE_WORDS = ['1', 'true', 'yes']; |
| 47 | + |
| 48 | + /** |
| 49 | + * Constructor. |
| 50 | + * |
| 51 | + * @param CaseDateNormaliser $dates The one reader of a date and the tenant's time zone. |
| 52 | + * @param RecordValues $values Lists, numbers and answers as the case stores them. |
| 53 | + */ |
| 54 | + public function __construct( |
| 55 | + private readonly CaseDateNormaliser $dates, |
| 56 | + private readonly RecordValues $values = new RecordValues(), |
| 57 | + ) { |
| 58 | + }//end __construct() |
| 59 | + |
| 60 | + /** |
| 61 | + * A raw value in the shape its declaration asks for. |
| 62 | + * |
| 63 | + * @param array<string, mixed> $field The field declaration. |
| 64 | + * @param mixed $raw The raw value. |
| 65 | + * |
| 66 | + * @return mixed The value, or null when empty or unreadable. |
| 67 | + * |
| 68 | + * @throws InvalidArgumentException When a value is outside its declared list. |
| 69 | + * |
| 70 | + * @spec openspec/specs/case-record-import/spec.md#requirement-a-declaration-reads-nested-typed-source-values-and-answers-the-case-types-fields-req-cri-005 |
| 71 | + * @spec openspec/changes/method-decomposition/tasks.md |
| 72 | + */ |
| 73 | + public function shaped(array $field, mixed $raw): mixed { |
| 74 | + $shape = (string)($field['as'] ?? 'text'); |
| 75 | + if ($shape === 'json') { |
| 76 | + return $this->values->json(raw: $raw); |
| 77 | + } |
| 78 | + |
| 79 | + if ($shape === 'boolean' && is_bool($raw) === true) { |
| 80 | + return $raw; |
| 81 | + } |
| 82 | + |
| 83 | + $text = $this->textOf(raw: $raw); |
| 84 | + |
| 85 | + if (isset($field['values']) === true) { |
| 86 | + return $this->listed(field: $field, text: $text); |
| 87 | + } |
| 88 | + |
| 89 | + return $this->typed(field: $field, text: $text); |
| 90 | + }//end shaped() |
| 91 | + |
| 92 | + /** |
| 93 | + * A scalar raw value as text with its whitespace collapsed; '' for anything else. |
| 94 | + * |
| 95 | + * @param mixed $raw The raw value. |
| 96 | + * |
| 97 | + * @return string The text. |
| 98 | + */ |
| 99 | + private function textOf(mixed $raw): string { |
| 100 | + $text = ''; |
| 101 | + if (is_scalar($raw) === true) { |
| 102 | + $text = trim((string)preg_replace('/\s+/u', ' ', (string)$raw)); |
| 103 | + } |
| 104 | + |
| 105 | + return $text; |
| 106 | + }//end textOf() |
| 107 | + |
| 108 | + /** |
| 109 | + * A text value in the shape its declaration asks for. |
| 110 | + * |
| 111 | + * @param array<string, mixed> $field The field declaration, with its shape (`as`, text when absent). |
| 112 | + * @param string $text The raw value as text. |
| 113 | + * |
| 114 | + * @return mixed The value, or null when empty or unreadable. |
| 115 | + */ |
| 116 | + private function typed(array $field, string $text): mixed { |
| 117 | + if ($text === '') { |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + return match ((string)($field['as'] ?? 'text')) { |
| 122 | + 'date' => $this->dates->toCalendarDateOrNull(value: $text), |
| 123 | + 'dateTime' => $this->dates->tryParse(value: $text)?->format(DateTimeInterface::ATOM), |
| 124 | + 'integer' => (int)$text, |
| 125 | + 'number' => $this->values->number(text: $text), |
| 126 | + 'boolean' => in_array(strtolower($text), self::TRUE_WORDS, true), |
| 127 | + default => $this->cut(text: $text, length: (int)($field['maxLength'] ?? 0)), |
| 128 | + }; |
| 129 | + }//end typed() |
| 130 | + |
| 131 | + /** |
| 132 | + * A value read through its declared list. |
| 133 | + * |
| 134 | + * @param array<string, mixed> $field The field declaration, with `values` and an optional `default`. |
| 135 | + * @param string $text The raw value as text. |
| 136 | + * |
| 137 | + * @return mixed The listed value, or the default for an empty one. |
| 138 | + * |
| 139 | + * @throws InvalidArgumentException When the value is not in the list. |
| 140 | + */ |
| 141 | + private function listed(array $field, string $text): mixed { |
| 142 | + $values = (array)$field['values']; |
| 143 | + if ($text === '') { |
| 144 | + return ($field['default'] ?? null); |
| 145 | + } |
| 146 | + |
| 147 | + if (array_key_exists($text, $values) === false) { |
| 148 | + throw new InvalidArgumentException( |
| 149 | + 'The ' . (string)($field['from'] ?? '') . ' must be one of: ' . implode(', ', array_keys($values)) . '.' |
| 150 | + ); |
| 151 | + } |
| 152 | + |
| 153 | + return $values[$text]; |
| 154 | + }//end listed() |
| 155 | + |
| 156 | + /** |
| 157 | + * Text cut to a length at the last word that fits. |
| 158 | + * |
| 159 | + * @param string $text The text. |
| 160 | + * @param int $length The length, or 0 for no limit. |
| 161 | + * |
| 162 | + * @return string The text. |
| 163 | + */ |
| 164 | + private function cut(string $text, int $length): string { |
| 165 | + if ($length <= 0 || mb_strlen($text) <= $length) { |
| 166 | + return $text; |
| 167 | + } |
| 168 | + |
| 169 | + $head = mb_substr($text, 0, $length + 1); |
| 170 | + $space = mb_strrpos($head, ' '); |
| 171 | + if ($space === false || $space === 0) { |
| 172 | + return mb_substr($text, 0, $length); |
| 173 | + } |
| 174 | + |
| 175 | + return rtrim(mb_substr($head, 0, $space)); |
| 176 | + }//end cut() |
| 177 | +}//end class |
0 commit comments