Skip to content

Commit

Permalink
Refactor appendSingleAdvertising method in AdsPostParser class
Browse files Browse the repository at this point in the history
  • Loading branch information
murdercode committed Jul 1, 2024
1 parent a1947a8 commit d44c598
Showing 1 changed file with 19 additions and 26 deletions.
45 changes: 19 additions & 26 deletions src/AdsPostParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,31 @@ public function appendAdvertising(): string
/**
* Append a single advertising
*/
public function appendSingleAdvertising(int $index, int $advIndex): string
{
$items = $this->dom->find('#adv__parsed__content > *');
$maxLoop = count($items);
public function appendSingleAdvertising(int $index, int $advIndex): string
{
$items = $this->dom->find('#adv__parsed__content > *');
$maxLoop = count($items);

if ($index >= $maxLoop) {
return $this->dom->save();
}
if ($index >= $maxLoop) {
return $this->dom->save();
}

$currentItem = $items[$index];
$previousItem = $index > 0 ? $items[$index - 1] : null;
$currentItem = $items[$index];
$previousItem = $index > 0 ? $items[$index - 1] : null;

if (
! preg_match($this->blacklist, $currentItem->outertext) &&
(! $previousItem || ! preg_match($this->blacklist, $previousItem->outertext)) &&
strip_tags($currentItem->outertext) !== ''
) {
$currentItem->outertext .= Blade::render('ads-post-parser::ads'.$advIndex);
}
else {
// Se l'elemento corrente o il precedente è in blacklist, cerca il prossimo elemento non in blacklist
for ($nextIndex = $index + 1; $nextIndex < $maxLoop; $nextIndex++) {
$nextItem = $items[$nextIndex];
if (! preg_match($this->blacklist, $nextItem->outertext) && strip_tags($nextItem->outertext) !== '') {
$nextItem->outertext .= Blade::render('ads-post-parser::ads'.$advIndex);
break;
}
if (
! preg_match($this->blacklist, $currentItem->outertext) &&
(! $previousItem || ! preg_match($this->blacklist, $previousItem->outertext))
) {
$currentItem->outertext .= Blade::render('ads-post-parser::ads'.$advIndex);
}
else {
$this->appendSingleAdvertising($index + 1, $advIndex);
}

return $this->dom->save();
}

return $this->dom->save();
}
/**
* Remove the wrapping div
*/
Expand Down

0 comments on commit d44c598

Please sign in to comment.