fix: complete InvesteMoz validation and error corrections
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Support\CheckedPattern;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/** Public projection only. Management retains the complete stored records and links. */
|
||||
@@ -19,28 +20,38 @@ final class PublicDocumentPresentation
|
||||
|
||||
private function text(?string $value): ?string
|
||||
{
|
||||
if ($value === null) return null;
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
// Imported descriptions can contain implementation notes. Never mutate the original.
|
||||
$sentences = preg_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));
|
||||
$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);
|
||||
$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;
|
||||
$public['source_url']=is_string($url)&&filter_var($url,FILTER_VALIDATE_URL)&&in_array(strtolower(parse_url($url,PHP_URL_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)
|
||||
$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();
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user