-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReader.php
More file actions
42 lines (36 loc) · 1 KB
/
Reader.php
File metadata and controls
42 lines (36 loc) · 1 KB
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
<?php
declare(strict_types = 1);
namespace Innmind\LogReader;
use Innmind\Filesystem\File\Content;
use Innmind\Immutable\Sequence;
/**
* Return a stream of already parsed log lines
*/
final class Reader
{
private LineParser $parse;
private function __construct(LineParser $parser)
{
$this->parse = $parser;
}
#[\NoDiscard]
public function __invoke(Content $file): Sequence
{
/**
* @psalm-suppress InvalidArgument Due to the empty sequence
* @var Sequence<Log>
*/
return $file
->lines()
->map(static fn($line) => $line->str())
->filter(static fn($line) => !$line->empty())
->flatMap(fn($line) => ($this->parse)($line)->match(
static fn($log) => Sequence::of($log),
static fn() => Sequence::of(), // discard the lines we can't parse
));
}
public static function of(LineParser $parser): self
{
return new self($parser);
}
}