fix: complete InvesteMoz validation and error corrections
This commit is contained in:
@@ -5,13 +5,18 @@ namespace App\Services;
|
||||
use App\Models\Opportunity;
|
||||
use App\Models\Sector;
|
||||
use App\Models\User;
|
||||
use App\Support\CheckedPattern;
|
||||
use DOMDocument;
|
||||
use DOMElement;
|
||||
use DOMNode;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Str;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
/** A reading adapter over the existing library, never a second content/opportunity register. */
|
||||
final class LegacyContent
|
||||
@@ -68,13 +73,13 @@ final class LegacyContent
|
||||
$opportunities = Opportunity::published()->whereIn('sector_id', $relatedSectors->pluck('id'))->latest('published_at')->limit(12)->get(['id', 'title', 'slug', 'province', 'summary']);
|
||||
}
|
||||
$localHtml = $this->localLinks($this->sanitize($source['content_html']));
|
||||
$editorial = \Illuminate\Support\Facades\Schema::hasColumn('portal_resources', 'editorial_content')
|
||||
$editorial = Schema::hasColumn('portal_resources', 'editorial_content')
|
||||
? DB::table('portal_resources')->where('id', $row->resource_id)->value('editorial_content') : null;
|
||||
if ($editorial) {
|
||||
$adaptation = json_decode($editorial, true, 64, JSON_THROW_ON_ERROR);
|
||||
$localHtml = implode('', array_map(fn ($p) => '<p>'.nl2br(e($p)).'</p>', preg_split('/\n\s*\n/', $adaptation['body'])));
|
||||
$localHtml = implode('', array_map(fn ($p) => '<p>'.nl2br(e($p)).'</p>', CheckedPattern::split('/\n\s*\n/', $adaptation['body'])));
|
||||
$source['title'] = $row->title;
|
||||
if (!empty($adaptation['image_resource_id'])) {
|
||||
if (! empty($adaptation['image_resource_id'])) {
|
||||
$image = $this->publicResources()->where('r.id', $adaptation['image_resource_id'])->whereIn('d.media_type', ['image/png', 'image/jpeg', 'image/webp'])->first();
|
||||
}
|
||||
}
|
||||
@@ -86,11 +91,15 @@ final class LegacyContent
|
||||
$relatedArticles = [];
|
||||
if ($includeRelated) {
|
||||
foreach ($this->related($identity['group'], $slug) as $item) {
|
||||
if (in_array($item['href'], $alreadyLinked, true)) continue;
|
||||
if (in_array($item['href'], $alreadyLinked, true)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$relatedArticles[] = $this->article($item['slug'], false);
|
||||
} catch (\Symfony\Component\HttpKernel\Exception\HttpException $error) {
|
||||
if ($error->getStatusCode() !== 404) throw $error;
|
||||
} catch (HttpException $error) {
|
||||
if ($error->getStatusCode() !== 404) {
|
||||
throw $error;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -125,16 +134,21 @@ final class LegacyContent
|
||||
&& str_contains($plain, 'Infraestruturas turísticas') && str_contains($plain, 'Oportunidades Regionais')) {
|
||||
return 'Conheça os temas de turismo e hospitalidade abordados no guia: hotelaria, ecoturismo, infraestruturas de apoio e referências regionais. Use este enquadramento para preparar a sua ideia de investimento.';
|
||||
}
|
||||
preg_match_all('~<p\b[^>]*>(.*?)</p>~isu', $html, $paragraphs);
|
||||
foreach ([...($paragraphs[1] ?? []), $html] as $paragraph) {
|
||||
$text = trim(preg_replace('/\s+/u', ' ', html_entity_decode(strip_tags($paragraph), ENT_QUOTES | ENT_HTML5, 'UTF-8')));
|
||||
if (mb_strlen($text) >= 60) return \Illuminate\Support\Str::limit($text, 320);
|
||||
if (preg_match_all('~<p\b[^>]*>(.*?)</p>~isu', $html, $paragraphs) === false) {
|
||||
throw new \RuntimeException('Invalid paragraph encoding; original preserved.');
|
||||
}
|
||||
foreach ([...$paragraphs[1], $html] as $paragraph) {
|
||||
$text = trim(preg_replace('/\s+/u', ' ', html_entity_decode(strip_tags($paragraph), ENT_QUOTES | ENT_HTML5, 'UTF-8')));
|
||||
if (mb_strlen($text) >= 60) {
|
||||
return Str::limit($text, 320);
|
||||
}
|
||||
}
|
||||
|
||||
return trim(html_entity_decode(strip_tags($html), ENT_QUOTES | ENT_HTML5, 'UTF-8'));
|
||||
}
|
||||
|
||||
/** Resolve a library identity, preserving the already stored source URLs and versions. */
|
||||
/** @return array<string, mixed> */
|
||||
/** Resolve a library identity, preserving the already stored source URLs and versions.
|
||||
* @return array<string, mixed> */
|
||||
public function articleForResource(int $resourceId): array
|
||||
{
|
||||
abort_unless($this->available(), 404);
|
||||
@@ -239,8 +253,10 @@ final class LegacyContent
|
||||
});
|
||||
}
|
||||
|
||||
/** Resolve explicit transport aliases without changing original IDs, versions or relations. */
|
||||
private function identityResources(): \Illuminate\Support\Collection
|
||||
/** Resolve explicit transport aliases without changing original IDs, versions or relations.
|
||||
* @return Collection<int, \stdClass>
|
||||
*/
|
||||
private function identityResources(): Collection
|
||||
{
|
||||
$identities = array_column(config('legacy_content.articles', []), 'external_id');
|
||||
$aliases = config('legacy_content_aliases', []);
|
||||
@@ -269,6 +285,7 @@ final class LegacyContent
|
||||
$row->legacy_identity = $identity;
|
||||
$resolved->put($identity, $row);
|
||||
}
|
||||
|
||||
return $resolved;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user