|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OPGG\LaravelMcpServer\Concerns; |
| 4 | + |
| 5 | +use JsonSerializable; |
| 6 | + |
| 7 | +/** |
| 8 | + * Helper utilities for converting flat tabular tool output into |
| 9 | + * multiple LLM friendly representations (JSON, CSV, Markdown). |
| 10 | + */ |
| 11 | +trait FormatsTabularData |
| 12 | +{ |
| 13 | + /** |
| 14 | + * CSV delimiter character. |
| 15 | + */ |
| 16 | + protected string $tabularCsvDelimiter = ','; |
| 17 | + |
| 18 | + /** |
| 19 | + * CSV enclosure character. |
| 20 | + */ |
| 21 | + protected string $tabularCsvEnclosure = '"'; |
| 22 | + |
| 23 | + /** |
| 24 | + * MIME type used for CSV responses. |
| 25 | + */ |
| 26 | + protected string $tabularCsvMimeType = 'text/csv'; |
| 27 | + |
| 28 | + /** |
| 29 | + * MIME type used for Markdown responses. |
| 30 | + */ |
| 31 | + protected string $tabularMarkdownMimeType = 'text/markdown'; |
| 32 | + |
| 33 | + /** |
| 34 | + * Returns the MIME types registered for tabular helper outputs. |
| 35 | + * |
| 36 | + * @return array<string, string> |
| 37 | + */ |
| 38 | + protected function tabularContentFormats(): array |
| 39 | + { |
| 40 | + return [ |
| 41 | + 'csv' => $this->tabularCsvMimeType, |
| 42 | + 'markdown' => $this->tabularMarkdownMimeType, |
| 43 | + ]; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Builds additional content payload entries (CSV + Markdown) when a result |
| 48 | + * contains a 1-depth list of associative rows. |
| 49 | + * |
| 50 | + * @param mixed $data |
| 51 | + * @return array<int, array{type: string, text: string, mimeType: string}> |
| 52 | + */ |
| 53 | + protected function buildTabularContent(mixed $data): array |
| 54 | + { |
| 55 | + $rows = $this->resolveTabularRows($data); |
| 56 | + |
| 57 | + if ($rows === null) { |
| 58 | + return []; |
| 59 | + } |
| 60 | + |
| 61 | + return [ |
| 62 | + [ |
| 63 | + 'type' => 'text', |
| 64 | + 'text' => $this->convertTabularRowsToCsv($rows), |
| 65 | + 'mimeType' => $this->tabularCsvMimeType, |
| 66 | + ], |
| 67 | + [ |
| 68 | + 'type' => 'text', |
| 69 | + 'text' => $this->convertTabularRowsToMarkdown($rows), |
| 70 | + 'mimeType' => $this->tabularMarkdownMimeType, |
| 71 | + ], |
| 72 | + ]; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Detects and normalises a flat list of tabular rows. |
| 77 | + * |
| 78 | + * @param mixed $data |
| 79 | + * @return array<int, array<string, scalar|null>>|null |
| 80 | + */ |
| 81 | + protected function resolveTabularRows(mixed $data): ?array |
| 82 | + { |
| 83 | + if (! is_array($data) || $data === []) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + if (! array_is_list($data)) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + $normalizedRows = []; |
| 92 | + $allKeys = []; |
| 93 | + |
| 94 | + foreach ($data as $row) { |
| 95 | + $rowArray = $this->convertRowToArray($row); |
| 96 | + |
| 97 | + if ($rowArray === null) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + foreach ($rowArray as $value) { |
| 102 | + if (is_array($value) || is_object($value)) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + $normalizedRows[] = $rowArray; |
| 108 | + $allKeys = array_merge($allKeys, array_keys($rowArray)); |
| 109 | + } |
| 110 | + |
| 111 | + if ($normalizedRows === []) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + $orderedKeys = array_values(array_unique($allKeys)); |
| 116 | + |
| 117 | + if ($orderedKeys === []) { |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + return array_map(function (array $row) use ($orderedKeys): array { |
| 122 | + $ordered = []; |
| 123 | + foreach ($orderedKeys as $key) { |
| 124 | + $ordered[$key] = $row[$key] ?? null; |
| 125 | + } |
| 126 | + |
| 127 | + return $ordered; |
| 128 | + }, $normalizedRows); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Converts an individual row into an associative array. |
| 133 | + * |
| 134 | + * @param mixed $row |
| 135 | + * @return array<string, mixed>|null |
| 136 | + */ |
| 137 | + protected function convertRowToArray(mixed $row): ?array |
| 138 | + { |
| 139 | + if (is_array($row)) { |
| 140 | + return $row; |
| 141 | + } |
| 142 | + |
| 143 | + if (is_object($row)) { |
| 144 | + if ($row instanceof JsonSerializable) { |
| 145 | + $serialized = $row->jsonSerialize(); |
| 146 | + |
| 147 | + if (is_array($serialized)) { |
| 148 | + return $serialized; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + $vars = get_object_vars($row); |
| 153 | + |
| 154 | + if (! empty($vars)) { |
| 155 | + return $vars; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return null; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Converts a list of rows into CSV text. |
| 164 | + * |
| 165 | + * @param array<int, array<string, scalar|null>> $rows |
| 166 | + */ |
| 167 | + protected function convertTabularRowsToCsv(array $rows): string |
| 168 | + { |
| 169 | + $stream = fopen('php://temp', 'r+'); |
| 170 | + |
| 171 | + if ($stream === false) { |
| 172 | + return ''; |
| 173 | + } |
| 174 | + |
| 175 | + $headers = array_keys($rows[0]); |
| 176 | + fputcsv($stream, $headers, $this->tabularCsvDelimiter, $this->tabularCsvEnclosure); |
| 177 | + |
| 178 | + foreach ($rows as $row) { |
| 179 | + $values = array_map(fn ($value) => $this->stringifyTabularValue($value), $row); |
| 180 | + fputcsv($stream, $values, $this->tabularCsvDelimiter, $this->tabularCsvEnclosure); |
| 181 | + } |
| 182 | + |
| 183 | + rewind($stream); |
| 184 | + $csv = stream_get_contents($stream); |
| 185 | + fclose($stream); |
| 186 | + |
| 187 | + if ($csv === false) { |
| 188 | + return ''; |
| 189 | + } |
| 190 | + |
| 191 | + return rtrim($csv, "\r\n"); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Converts a list of rows into a Markdown table string. |
| 196 | + * |
| 197 | + * @param array<int, array<string, scalar|null>> $rows |
| 198 | + */ |
| 199 | + protected function convertTabularRowsToMarkdown(array $rows): string |
| 200 | + { |
| 201 | + $headers = array_keys($rows[0]); |
| 202 | + $headerLine = '| '.implode(' | ', array_map([$this, 'escapeMarkdownValue'], $headers)).' |'; |
| 203 | + $separatorLine = '| '.implode(' | ', array_fill(0, count($headers), '---')).' |'; |
| 204 | + |
| 205 | + $lines = [$headerLine, $separatorLine]; |
| 206 | + |
| 207 | + foreach ($rows as $row) { |
| 208 | + $cells = array_map(function ($value) { |
| 209 | + return $this->escapeMarkdownValue($this->stringifyTabularValue($value)); |
| 210 | + }, $row); |
| 211 | + |
| 212 | + $lines[] = '| '.implode(' | ', $cells).' |'; |
| 213 | + } |
| 214 | + |
| 215 | + return implode(PHP_EOL, $lines); |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * Converts scalar/null values to their string representations. |
| 220 | + */ |
| 221 | + protected function stringifyTabularValue(mixed $value): string |
| 222 | + { |
| 223 | + if ($value === null) { |
| 224 | + return ''; |
| 225 | + } |
| 226 | + |
| 227 | + if (is_bool($value)) { |
| 228 | + return $value ? 'true' : 'false'; |
| 229 | + } |
| 230 | + |
| 231 | + if (is_scalar($value)) { |
| 232 | + return (string) $value; |
| 233 | + } |
| 234 | + |
| 235 | + $encoded = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
| 236 | + |
| 237 | + return $encoded === false ? '' : $encoded; |
| 238 | + } |
| 239 | + |
| 240 | + /** |
| 241 | + * Escapes Markdown table control characters. |
| 242 | + */ |
| 243 | + protected function escapeMarkdownValue(string $value): string |
| 244 | + { |
| 245 | + $escaped = str_replace('|', '\\|', $value); |
| 246 | + $escaped = str_replace(["\r\n", "\r", "\n"], '<br />', $escaped); |
| 247 | + |
| 248 | + return $escaped; |
| 249 | + } |
| 250 | +} |
0 commit comments