diff --git a/src/Detective/FilenameDetector.php b/src/Detective/FilenameDetector.php new file mode 100644 index 0000000..e7ede67 --- /dev/null +++ b/src/Detective/FilenameDetector.php @@ -0,0 +1,45 @@ +pattern = $pattern; + return $this; + } + + public function setWeight(float $weight): static + { + $this->weight = $weight; + return $this; + } + + public function detect(): bool|float + { + $path = $this->logFile->getPath(); + if ($path === null || $this->pattern === null) { + return false; + } + + if (preg_match($this->pattern, $path) === 1) { + return $this->weight; + } + + return false; + } +} diff --git a/test/tests/Detective/FilenameDetectorTest.php b/test/tests/Detective/FilenameDetectorTest.php new file mode 100644 index 0000000..5e10e04 --- /dev/null +++ b/test/tests/Detective/FilenameDetectorTest.php @@ -0,0 +1,47 @@ +setPattern('/simple\.log$/') + ->setWeight(0.9); + $detector->setLogFile(new PathLogFile(__DIR__ . "/../../data/simple.log")); + + $this->assertSame(0.9, $detector->detect()); + } + + public function testReturnsFalseWhenPatternDoesNotMatch(): void + { + $detector = (new FilenameDetector()) + ->setPattern('/notthere\.txt$/'); + $detector->setLogFile(new PathLogFile(__DIR__ . "/../../data/simple.log")); + + $this->assertFalse($detector->detect()); + } + + public function testReturnsFalseWhenLogFileHasNoPath(): void + { + $detector = (new FilenameDetector()) + ->setPattern('/anything/'); + $detector->setLogFile(new StringLogFile("content")); + + $this->assertFalse($detector->detect()); + } + + public function testReturnsFalseWhenPatternUnset(): void + { + $detector = new FilenameDetector(); + $detector->setLogFile(new PathLogFile(__DIR__ . "/../../data/simple.log")); + + $this->assertFalse($detector->detect()); + } +}