Skip to content

Commit 31ff939

Browse files
authored
Merge pull request #21 from The-3Labs-Team/New-AdsPostParser-logic
New AdsPostParser logic
2 parents 10d1ab0 + 846d5f3 commit 31ff939

File tree

2 files changed

+119
-2
lines changed

2 files changed

+119
-2
lines changed

src/AdsPostParser.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,42 @@ public function __construct(string $content)
3030
* Append all the advertising
3131
*/
3232
public function appendAdvertising(): string
33+
{
34+
$thresholds = config('ads-post-parser.thresholds');
35+
$items = $this->dom->find('#adv__parsed__content > *');
36+
$adsCount = 0;
37+
38+
foreach ($items as $index => $item) {
39+
// === BLACKLIST ===
40+
$blacklist = explode('|', trim($this->blacklistAfter, '/'));
41+
42+
$currentElement = $item;
43+
$afterElement = $index < count($items) - 1 ? $items[$index + 1] : null;
44+
$isBlackList = preg_match('/'.implode('|', $blacklist).'/', $currentElement->outertext) || ($afterElement ? preg_match('/'.implode('|', $blacklist).'/', $afterElement->outertext) : false);
45+
// === END BLACKLIST ===
46+
47+
if (in_array($index, $thresholds)) {
48+
if ($isBlackList) {
49+
$thresholds = array_map(function ($value) {
50+
return $value + 1;
51+
}, $thresholds);
52+
53+
continue;
54+
}
55+
56+
try {
57+
$currentElement->outertext .= Blade::render('ads-post-parser::ads'.array_keys($thresholds)[$adsCount]);
58+
} catch (\Exception $e) {
59+
// Content without ADV
60+
}
61+
$adsCount++;
62+
}
63+
}
64+
65+
return $this->dom->save();
66+
}
67+
68+
public function oldappendAdvertising(): string
3369
{
3470
$thresholds = config('ads-post-parser.thresholds');
3571

@@ -59,8 +95,7 @@ public function appendSingleAdvertising(int $index, int $advIndex): string
5995
$nextItem = $index < $maxLoop - 1 ? $items[$index + 1] : null;
6096

6197
if (
62-
! preg_match($this->blacklistBefore, $beforeItem->outertext)
63-
&& ($nextItem === null || ! preg_match($this->blacklistAfter, $nextItem->outertext))
98+
! preg_match($this->blacklistBefore, $beforeItem->outertext) && ($nextItem === null || ! preg_match($this->blacklistAfter, $nextItem->outertext))
6499
) {
65100
try {
66101
$beforeItem->outertext .= Blade::render('ads-post-parser::ads'.$advIndex);

tests/AdsPostParserTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,71 @@
1919
<p>Paragraph10</p>
2020
<p>Paragraph11</p>
2121
<p>Paragraph12</p>
22+
<p>Paragraph13</p>
23+
<p>Paragraph15</p>
24+
<p>Paragraph16</p>
25+
<p>Paragraph17</p>
26+
<p>Paragraph18</p>
27+
<p>Paragraph19</p>
28+
<p>Paragraph20</p>
2229
HTML;
30+
31+
$this->smallContent = <<<'HTML'
32+
<p>Paragraph1</p>
33+
<p>Paragraph2</p>
34+
<p>Paragraph3</p>
35+
<img src="https://example.com/image.jpg" alt="Image">
36+
<p>Paragraph4</p>
37+
<p>Paragraph5</p>
38+
HTML;
39+
40+
$this->contentOnlyParagraphs = <<<'HTML'
41+
<p>Paragraph1</p>
42+
<p>Paragraph2</p>
43+
<p>Paragraph3</p>
44+
<p>Paragraph4</p>
45+
<p>Paragraph5</p>
46+
<p>Paragraph6</p>
47+
<p>Paragraph7</p>
48+
<p>Paragraph8</p>
49+
<p>Paragraph9</p>
50+
<p>Paragraph10</p>
51+
<p>Paragraph11</p>
52+
<p>Paragraph12</p>
53+
<p>Paragraph13</p>
54+
<p>Paragraph14</p>
55+
<p>Paragraph15</p>
56+
<p>Paragraph16</p>
57+
<p>Paragraph17</p>
58+
<p>Paragraph18</p>
59+
<p>Paragraph19</p>
60+
<p>Paragraph20</p>
61+
HTML;
62+
63+
});
64+
65+
it('can append first and second advertising only paragraphs', function () {
66+
$content = (new AdsPostParser($this->contentOnlyParagraphs))->appendAdvertising();
67+
68+
expect($content)
69+
->toContain('<p>Paragraph1</p><div>--- YOUR AD1 HERE ---</div>')
70+
->toContain('<p>Paragraph4</p><div>--- YOUR AD2 HERE ---</div>');
71+
});
72+
73+
it('can append first and second advertising', function () {
74+
$content = (new AdsPostParser($this->content))->appendAdvertising();
75+
76+
expect($content)
77+
->toContain("<p>Paragraph1</p>\n<div>--- YOUR AD1 HERE ---</div>")
78+
->toContain("<p>Paragraph7</p>\n<div>--- YOUR AD2 HERE ---</div>");
79+
});
80+
81+
it('can append advertising in small content with specific position', function () {
82+
$content = (new AdsPostParser($this->smallContent))->appendAdvertising();
83+
84+
expect($content)
85+
->toContain("<p>Paragraph1</p>\n<div>--- YOUR AD1 HERE ---</div>")
86+
->toContain("<p>Paragraph4</p>\n<div>--- YOUR AD2 HERE ---</div>");
2387
});
2488

2589
it('can append advertising', function () {
@@ -32,6 +96,24 @@
3296
->toContain('YOUR AD4 HERE');
3397
});
3498

99+
it('can append advertising in small content', function () {
100+
$content = (new AdsPostParser($this->smallContent))->appendAdvertising();
101+
102+
expect($content)
103+
->toContain('YOUR AD1 HERE')
104+
->toContain('YOUR AD2 HERE');
105+
});
106+
107+
it('can append advertising only paragraphs', function () {
108+
$content = (new AdsPostParser($this->content))->appendAdvertising();
109+
110+
expect($content)
111+
->toContain('YOUR AD1 HERE')
112+
->toContain('YOUR AD2 HERE')
113+
->toContain('YOUR AD3 HERE')
114+
->toContain('YOUR AD4 HERE');
115+
});
116+
35117
it('can append single advertising', function () {
36118
$content = (new AdsPostParser($this->content))
37119
->appendSingleAdvertising(2, 1);

0 commit comments

Comments
 (0)