Skip to content

Commit 08cccf3

Browse files
committed
added weighted single pattern detector
1 parent 415f251 commit 08cccf3

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Aternos\Codex\Detective;
4+
5+
/**
6+
* Class WeightedSinglePatternDetector
7+
*
8+
* @package Aternos\Codex\Detective
9+
*/
10+
class WeightedSinglePatternDetector extends SinglePatternDetector
11+
{
12+
/**
13+
* @var float
14+
*/
15+
protected $weight;
16+
17+
/**
18+
* Set the weight that will be returned if the pattern matches
19+
*
20+
* @param float $weight
21+
* @return $this
22+
*/
23+
public function setWeight(float $weight)
24+
{
25+
$this->weight = $weight;
26+
return $this;
27+
}
28+
29+
/**
30+
* Detect if the log matches
31+
*
32+
* Checks if the pattern matches anywhere in the log file
33+
*
34+
* Returns either true or false
35+
*
36+
* @return bool|float
37+
*/
38+
public function detect()
39+
{
40+
if (parent::detect()) {
41+
return $this->weight;
42+
} else {
43+
return false;
44+
}
45+
}
46+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Aternos\Codex\Detective\WeightedSinglePatternDetector;
4+
use Aternos\Codex\Log\File\StringLogFile;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class WeightedSinglePatternDetectorTest extends TestCase
8+
{
9+
public function testDetect()
10+
{
11+
$this->assertEquals(1, (new WeightedSinglePatternDetector())
12+
->setLogFile(new \Aternos\Codex\Log\File\PathLogFile(__DIR__ . '/../../data/simple.log'))
13+
->setPattern('/This/')
14+
->setWeight(1)
15+
->detect()
16+
);
17+
18+
$this->assertEquals(0.5, (new WeightedSinglePatternDetector())
19+
->setLogFile(new \Aternos\Codex\Log\File\PathLogFile(__DIR__ . '/../../data/simple.log'))
20+
->setPattern('/This/')
21+
->setWeight(0.5)
22+
->detect()
23+
);
24+
25+
$this->assertEquals(0, (new WeightedSinglePatternDetector())
26+
->setLogFile(new \Aternos\Codex\Log\File\PathLogFile(__DIR__ . '/../../data/simple.log'))
27+
->setPattern('/This/')
28+
->setWeight(0)
29+
->detect()
30+
);
31+
32+
$this->assertFalse((new WeightedSinglePatternDetector())
33+
->setLogFile(new StringLogFile("You cannot detect this."))
34+
->setPattern('/missing/')
35+
->detect()
36+
);
37+
}
38+
}

0 commit comments

Comments
 (0)