Files
Investidor-V2/app/Services/PublicDocumentPresentation.php
T

58 lines
3.5 KiB
PHP

<?php
namespace App\Services;
use App\Support\CheckedPattern;
use Illuminate\Support\Facades\DB;
/** Public projection only. Management retains the complete stored records and links. */
final class PublicDocumentPresentation
{
private const ACTIONS = [
'simulator' => ['href' => '/simulador-fiscal', 'label' => 'Abrir simulador fiscal'],
'procedures' => ['href' => '/como-investir', 'label' => 'Consultar como investir'],
'investment' => ['href' => '/oportunidades', 'label' => 'Consultar oportunidades'],
'zones' => ['href' => '/zonas', 'label' => 'Consultar zonas e parques'],
'education' => ['href' => '/educacao', 'label' => 'Consultar educação e competências'],
'business' => ['href' => '/negocios', 'label' => 'Consultar negócios'],
'land' => ['href' => '/investidor/terra-duat', 'label' => 'Consultar Terra e DUAT'],
];
private function text(?string $value): ?string
{
if ($value === null) {
return null;
}
// Imported descriptions can contain implementation notes. Never mutate the original.
$sentences = CheckedPattern::split('/(?<=[.!?])\s+|[\r\n]+/u', strip_tags($value));
$safe = array_filter($sentences, fn ($text) => ! preg_match('~(?:\b(?:supports|cites|evidence)\b|\btheme\s*:|\bfiscal\.[\w.]+|\b(?:SHA-?256|hash|preflight|repositório|autenticidade|comparação binária|estado de vigência|revisão de direitos)\b|Cópia mantida em privado|\b[0-9a-f]{40,64}\b|(?:^|\s)(?:[A-Z]:[\\/]|(?:docs|outputs|resources|storage|app)/))~iu', $text));
$result = trim(implode(' ', $safe));
return $result !== '' ? $result : null;
}
/**
* @return array<array-key, mixed>
*/
public function resource(object $row): array
{
$r = (array) $row;
$public = array_intersect_key($r, array_flip(['id', 'document_type', 'document_date', 'applicable_from', 'applicable_until', 'language', 'territorial_scope', 'territory_name', 'source_confirmation', 'legal_status', 'size_bytes']));
foreach (['title', 'description', 'issuer', 'document_number', 'category', 'topic', 'version_label'] as $key) {
$public[$key] = $this->text($r[$key] ?? null);
}
$public['title'] ??= 'Documento';
$url = $r['source_url'] ?? null;
$scheme = is_string($url) ? parse_url($url, PHP_URL_SCHEME) : null;
$public['source_url'] = is_string($url) && filter_var($url, FILTER_VALIDATE_URL) && is_string($scheme) && in_array(strtolower($scheme), ['https', 'http'], true) ? $url : null;
$public['download_url'] = route('services.download', ['id' => $r['id']]);
$public['view_url'] = in_array($r['media_type'] ?? null, ['application/pdf', 'image/jpeg', 'image/png', 'image/webp'], true) ? route('services.document.view', ['id' => $r['id']]) : null;
$public['usage_notice'] = in_array($r['document_type'] ?? '', ['legislation', 'procedure', 'official_form', 'digital_adaptation', 'guidance_template'], true)
? 'Confirme no documento ou junto da entidade competente as condições aplicáveis ao seu caso. A consulta não garante benefícios nem aplicação fiscal.' : null;
$keys = DB::table('portal_resource_links')->where('resource_id', $r['id'])->where('target_type', 'module')->pluck('target_key');
$public['context_actions'] = $keys->filter(fn ($key) => isset(self::ACTIONS[$key]))->map(fn ($key) => self::ACTIONS[$key])->unique('href')->values()->all();
return $public;
}
}