-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPrefixDetector.php
69 lines (58 loc) · 2.14 KB
/
PrefixDetector.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
63
64
65
66
67
68
69
<?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;
final class PrefixDetector implements ChangesDetector
{
private const PREFIXES = [
'added' => ['add', 'added', 'adding'],
'changed' => ['change', 'changed', 'replaced'],
'updated' => ['updated', 'update', 'bump', 'bumped'],
'fixed' => ['fix', 'fixed', 'fixing'],
'removed' => ['rm', 'removed', 'rem', 'drop', 'dropped'],
'deprecated' => ['deprecated', 'dep'],
'security' => ['security', 'sec'],
];
private DescriptionPurifier $purifier;
public function __construct(DescriptionPurifier $purifier)
{
$this->purifier = $purifier;
}
public function support(ChangesSource $changesSource) : bool
{
foreach (self::PREFIXES as $type => $prefixes) {
foreach ($prefixes as $prefix) {
if ($this->startsWith($prefix . ' ', \strtolower($changesSource->title()))) {
return true;
}
}
}
return false;
}
public function detect(ChangesSource $changesSource) : Changes
{
foreach (self::PREFIXES as $type => $prefixes) {
foreach ($prefixes as $prefix) {
if ($this->startsWith($prefix . ' ', \strtolower($changesSource->title()))) {
return new Changes(
new Change(
$changesSource,
Type::$type(),
$this->purifier->purify(\substr($changesSource->title(), \strlen($prefix) + 1))
)
);
}
}
}
throw new \RuntimeException("Can't get changes from source title prefix");
}
private function startsWith(string $needle, string $haystack) : bool
{
return \substr($haystack, 0, \strlen($needle)) === $needle;
}
}