-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHTMLChangesDetector.php
62 lines (48 loc) · 1.82 KB
/
HTMLChangesDetector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
declare(strict_types=1);
namespace Aeon\Automation\Changes\Detector;
use Aeon\Automation\Changes\Change;
use Aeon\Automation\Changes\Changes;
use Aeon\Automation\Changes\ChangesDetector;
use Aeon\Automation\Changes\ChangesSource;
use Aeon\Automation\Changes\DescriptionPurifier;
use Aeon\Automation\Changes\Type;
use Symfony\Component\DomCrawler\Crawler;
final class HTMLChangesDetector implements ChangesDetector
{
/**
* @var DescriptionPurifier
*/
private DescriptionPurifier $purifier;
public function __construct(DescriptionPurifier $purifier)
{
$this->purifier = $purifier;
}
public function support(ChangesSource $changesSource) : bool
{
if (\strip_tags($changesSource->description()) === $changesSource->description()) {
return false;
}
$crawler = new Crawler('<html><body><div id="changes">' . $changesSource->description() . '</div></body></html>');
if (!$crawler->filter('#change-log')->count()) {
return false;
}
return true;
}
public function detect(ChangesSource $changesSource) : Changes
{
$crawler = new Crawler('<html><body><div id="changes">' . $changesSource->description() . '</div></body></html>');
if (!$crawler->filter('#change-log')->count()) {
throw new \RuntimeException("Invalid html format, can't extract changes");
}
$changes = [];
foreach (Type::all() as $type) {
if ($crawler->filter('ul#' . $type->name())->count()) {
foreach ($crawler->filter('ul#' . $type->name())->children('li') as $node) {
$changes[] = new Change($changesSource, $type, $this->purifier->purify($node->textContent));
}
}
}
return new Changes(...$changes);
}
}