diff --git a/reader/FileIterator.php b/reader/FileIterator.php new file mode 100644 index 0000000..1ceca0a --- /dev/null +++ b/reader/FileIterator.php @@ -0,0 +1,83 @@ +filePath = $filePath; + } + + + public function __destruct() + { + if ($this->f) { + fclose($this->f); + } + } + + + public function current() + { + return $this->current; + } + + + public function key() + { + return $this->key; + } + + + public function next() + { + $this->current = trim(fgets($this->f), PHP_EOL); + $this->key++; + } + + + public function rewind() + { + $this->openFile(); + } + + + public function valid(): bool + { + return !feof($this->getFileHandle()); + } + + + private function getFileHandle() + { + return $this->f ?? $this->openFile(); + } + + + private function openFile() + { + if ($this->f) { + fclose($this->f); + } + $this->f = fopen($this->filePath, 'r'); + $this->key = -1; + $this->next(); + return $this->f; + } +} diff --git a/reader/Filter.php b/reader/Filter.php new file mode 100644 index 0000000..bf76c6f --- /dev/null +++ b/reader/Filter.php @@ -0,0 +1,69 @@ +delegate = $delegate; + } + + + abstract function bufferCurrent(): array; + + + public function current() + { + if (empty($this->buffer)) { + $this->buffer = $this->bufferCurrent(); + } + return $this->buffer[0]; + } + + + public function key() + { + return $this->key; + } + + + public function next() + { + array_shift($this->buffer); + $this->key++; + if (empty($this->buffer)) { + $this->delegate->next(); + } + } + + + public function rewind() + { + $this->key = 0; + $this->buffer = []; + $this->delegate->rewind(); + } + + + public function valid(): bool + { + if (empty($this->buffer)) { + return $this->delegate->valid(); + } else { + return TRUE; + } + } +} diff --git a/reader/Filter/MinLengthJoiner.php b/reader/Filter/MinLengthJoiner.php new file mode 100644 index 0000000..7c9766b --- /dev/null +++ b/reader/Filter/MinLengthJoiner.php @@ -0,0 +1,25 @@ +delegate->current(); + + while (strlen($current) < static::MIN_LENGTH && $this->delegate->valid()) { + $this->delegate->next(); + if ($this->delegate->valid()) { + $current .= '|' . $this->delegate->current(); + } + } + + return [$current]; + } +} diff --git a/reader/Filter/SemicolonSplitter.php b/reader/Filter/SemicolonSplitter.php new file mode 100644 index 0000000..92ed0b4 --- /dev/null +++ b/reader/Filter/SemicolonSplitter.php @@ -0,0 +1,15 @@ +delegate->current(); + return explode(';', $current); + } +} diff --git a/reader/Filter/StopwordsOmitter.php b/reader/Filter/StopwordsOmitter.php new file mode 100644 index 0000000..a128705 --- /dev/null +++ b/reader/Filter/StopwordsOmitter.php @@ -0,0 +1,37 @@ +stopword = $stopword; + } + + + public function bufferCurrent(): array + { + return [$this->delegate->current()]; + } + + + public function valid(): bool + { + if ($this->delegate->valid()) { + if (\strpos($this->delegate->current(), $this->stopword) !== FALSE) { + $this->delegate->next(); + return $this->valid(); + } + return TRUE; + } + return FALSE; + } +} diff --git a/reader/IPresenter.php b/reader/IPresenter.php new file mode 100644 index 0000000..ad57b81 --- /dev/null +++ b/reader/IPresenter.php @@ -0,0 +1,11 @@ +words = $words; + } + + + public function apply(string $inputLine): string + { + foreach ($this->words as $word) { + if (strpos($inputLine, $word) !== FALSE) { + $this->counts[$word] = ($this->counts[$word] ?? 0) + 1; + } + } + + return $this->output(); + } + + + private function output(): string + { + $output = "----------------------------\n"; + foreach ($this->counts as $word => $count) { + $output .= "$word\t$count\n"; + } + return $output; + } +} diff --git a/reader/Processor.php b/reader/Processor.php new file mode 100644 index 0000000..0397479 --- /dev/null +++ b/reader/Processor.php @@ -0,0 +1,35 @@ +inputStream = $inputStream; + $this->presenter = $presenter; + } + + + public function output($rolling = FALSE) + { + foreach ($this->inputStream as $inputLine) { + $output = $this->presenter->apply($inputLine); + if ($rolling) { + yield $output; + } + } + if (!$rolling) { + yield $output; + } + } +} diff --git a/reader/data.txt b/reader/data.txt new file mode 100644 index 0000000..f8625e1 --- /dev/null +++ b/reader/data.txt @@ -0,0 +1,10 @@ +[2018-03-13 12:16:10] test.DEBUG: Test message [] [] +[2018-03-13 12:16:10] test.ERROR: Test message [] [] +[2018-03-13 12:16:10] test.WARNING: Test message [] [] +[2018-03-13 12:16:10] test.WARNING: Test message [] [] +[2018-03-13 12:16:10] test.INFO: Test message [] [] +[2018-03-13 12:16:10] test.NOTICE: Test message [] [] +[2018-03-13 12:16:10] test.EMERGENCY: Test message [] [] +[2018-03-13 12:16:10] test.ALERT: Test message [] [] +[2018-03-13 12:16:10] test.ERROR: Test message [] [] +[2018-03-13 12:16:10] test.NOTICE: Test message [] [] \ No newline at end of file diff --git a/reader/index.php b/reader/index.php new file mode 100644 index 0000000..b1777a8 --- /dev/null +++ b/reader/index.php @@ -0,0 +1,79 @@ + $value) { + echo "$key: $value\n"; +} + + +// Presenter +echo "\n\nPresenter ---------------------------------------\n"; +$inputLines = [ + 'Hello world! This is words counter presenter speaking!', + 'Words counter is the built in presenter of Reader package', + 'This is the best Reader in the world!', +]; + +$presenter = new Presenter\WordsCounter(['world', 'Reader', 'is', 'in']); + +foreach ($inputLines as $inputLine) { + echo $presenter->apply($inputLine) . "\n"; +} + + +// Processor +echo "\n\nProcessor ---------------------------------------\n"; +$inputStream = [ + 'foo', + 'bar', + 'baz', +]; + +$p = new Processor(new \ArrayIterator($inputStream), new Presenter\SimplePrinter()); + +$output = $p->output(TRUE); +foreach ($output as $line) { + echo $line . "\n"; +} + + +//File iterator +echo "\n\nFileIterator ---------------------------------------\n"; +$inputStream = new FileIterator(__DIR__ . '/data.txt'); + +foreach ($inputStream as $key => $value) { + echo "$key: $value\n"; +} + +//Putting it all together: https://github.com/intraworlds/workshop-php +echo "\n\nHeureka ---------------------------------------\n"; + +$inputStream = new FileIterator(__DIR__ . '/data.txt'); +$filteredStream = new Filter\StopwordsOmitter($inputStream, 'test.DEBUG'); +$countsPresenter = new Presenter\WordsCounter(['ERROR', 'WARNING', 'INFO', 'NOTICE', 'EMERGENCY', 'ALERT']); + +$p = new Processor($filteredStream, $countsPresenter); + +foreach ($p->output(FALSE) as $outputLine) { + echo $outputLine . "\n"; +} + + +