'object', 'properties' => [ 'path' => ['type' => 'string', 'description' => 'Путь к CSV относительно корня.'], 'where' => ['type' => 'string', 'description' => 'Фильтр: city=Тула или name~иван.'], 'columns' => ['type' => 'string', 'description' => 'Список колонок через запятую.'], 'limit' => ['type' => 'integer', 'description' => 'Сколько строк, по умолчанию 20, максимум ' . self::MAX_ROWS . '.'], 'delimiter' => ['type' => 'string', 'description' => 'Разделитель, по умолчанию запятая.'], ], 'required' => ['path'], 'additionalProperties' => false, ]; } public function handle(array $args): string { $path = (string) ($args['path'] ?? ''); $limit = (int) ($args['limit'] ?? 20); $delimiter = (string) ($args['delimiter'] ?? ','); $delimiter = $delimiter !== '' ? $delimiter[0] : ','; if ($limit < 1) { $limit = 20; } $limit = min($limit, self::MAX_ROWS); try { $file = $this->sandbox->resolve($path); } catch (\RuntimeException $e) { return $e->getMessage(); } if (!is_file($file)) { return 'Это не файл: ' . $path; } $size = filesize($file); if ($size !== false && $size > self::MAX_BYTES) { return 'CSV слишком большой: ' . round($size / 1048576, 1) . ' МБ (максимум 5 МБ).'; } $handle = fopen($file, 'rb'); if ($handle === false) { return 'Не удалось открыть файл.'; } $header = fgetcsv($handle, 0, $delimiter); if ($header === false || $header === [null] || $header === []) { fclose($handle); return 'Не удалось прочитать заголовок CSV.'; } $header = array_map(static fn ($name): string => trim((string) $name), $header); $requested = $this->requestedColumns($header, (string) ($args['columns'] ?? '')); if (is_string($requested)) { fclose($handle); return $requested; } $filter = $this->parseWhere($header, (string) ($args['where'] ?? '')); if (is_string($filter)) { fclose($handle); return $filter; } $allRows = $this->readRows($handle, $delimiter); fclose($handle); $total = count($allRows); $rows = $this->pickRows($allRows, $header, $requested, $filter, $limit); if ($rows === []) { return 'Подходящих строк нет (всего строк: ' . $total . ').'; } $table = [array_map(static fn (int $i): string => $header[$i], $requested)]; $table = array_merge($table, $rows); return 'CSV ' . $path . ': ' . count($rows) . ' из ' . $total . " строк\n" . $this->render($table); } /** * @param resource $handle * @return list> */ private function readRows($handle, string $delimiter): array { $rows = []; while (($row = fgetcsv($handle, 0, $delimiter)) !== false) { $rows[] = array_map(static fn ($cell): string => trim((string) $cell), $row); } return $rows; } /** * @param list> $allRows * @param list $header * @param list $requested * @param array{index: int, value: string, contains: bool}|null $filter * @return list> */ private function pickRows(array $allRows, array $header, array $requested, ?array $filter, int $limit): array { $rows = []; foreach ($allRows as $row) { if ($filter !== null && !$this->matches($row, $header, $filter)) { continue; } $picked = []; foreach ($requested as $index) { $picked[] = $this->cell($row[$index] ?? ''); } $rows[] = $picked; if (count($rows) >= $limit) { break; } } return $rows; } /** * @param list $header * @return list|string индексы колонок или текст ошибки */ private function requestedColumns(array $header, string $columns): array|string { if (trim($columns) === '') { return array_keys($header); } $wanted = array_map('trim', explode(',', $columns)); $indexes = []; foreach ($wanted as $name) { $index = array_search($name, $header, true); if ($index === false) { return 'Нет колонки "' . $name . '". Есть: ' . implode(', ', $header) . '.'; } $indexes[] = (int) $index; } return $indexes; } /** * @param list $header * @return array{index: int, value: string, contains: bool}|string|null */ private function parseWhere(array $header, string $where): array|string|null { $where = trim($where); if ($where === '') { return null; } if (preg_match('/^([^=~]+)([=~])(.*)$/u', $where, $match) !== 1) { return 'Фильтр должен быть вида "колонка=значение" или "колонка~значение".'; } $column = trim($match[1]); $index = array_search($column, $header, true); if ($index === false) { return 'Нет колонки "' . $column . '". Есть: ' . implode(', ', $header) . '.'; } return [ 'index' => (int) $index, 'value' => trim($match[3]), 'contains' => $match[2] === '~', ]; } /** * @param list $row * @param list $header * @param array{index: int, value: string, contains: bool} $filter */ private function matches(array $row, array $header, array $filter): bool { $cell = $row[$filter['index']] ?? ''; if ($filter['contains']) { return mb_stripos($cell, $filter['value']) !== false; } return mb_strtolower($cell) === mb_strtolower($filter['value']); } private function cell(string $value): string { $value = str_replace(["\r", "\n"], ' ', $value); return mb_strlen($value) > self::CELL_MAX ? mb_substr($value, 0, self::CELL_MAX) . '…' : $value; } /** * @param list> $table */ private function render(array $table): string { $widths = []; foreach ($table as $row) { foreach ($row as $index => $cell) { $widths[$index] = max($widths[$index] ?? 0, mb_strlen($cell)); } } $lines = []; foreach ($table as $rowIndex => $row) { $parts = []; foreach ($row as $index => $cell) { $parts[] = str_pad($cell, $widths[$index]); } $lines[] = implode(' | ', $parts); if ($rowIndex === 0) { $lines[] = implode('-+-', array_map( static fn (int $width): string => str_repeat('-', $width), $widths )); } } return implode("\n", $lines); } }