File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments