Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions reader/FileIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Reader;


final class FileIterator implements \Iterator
{

/** @var string */
private $filePath;

/** File handle */
private $f;

/** @var string */
private $current;

/** @var int */
private $key = -1;


public function __construct(string $filePath)
{
$this->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;
}
}
69 changes: 69 additions & 0 deletions reader/Filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Reader;


abstract class Filter implements \Iterator
{

/** @var \Iterator */
protected $delegate;

/** @var int */
private $key = 0;

/** @var array */
private $buffer = [];


public function __construct(\Iterator $delegate)
{
$this->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;
}
}
}
25 changes: 25 additions & 0 deletions reader/Filter/MinLengthJoiner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Reader\Filter;


final class MinLengthJoiner extends \Reader\Filter
{

const MIN_LENGTH = 5;


public function bufferCurrent(): array
{
$current = $this->delegate->current();

while (strlen($current) < static::MIN_LENGTH && $this->delegate->valid()) {
$this->delegate->next();
if ($this->delegate->valid()) {
$current .= '|' . $this->delegate->current();
}
}

return [$current];
}
}
15 changes: 15 additions & 0 deletions reader/Filter/SemicolonSplitter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Reader\Filter;


final class SemicolonSplitter extends \Reader\Filter
{


public function bufferCurrent(): array
{
$current = $this->delegate->current();
return explode(';', $current);
}
}
37 changes: 37 additions & 0 deletions reader/Filter/StopwordsOmitter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Reader\Filter;


final class StopwordsOmitter extends \Reader\Filter
{

/** @var string */
private $stopword;


public function __construct(\Iterator $delegate, string $stopword)
{
parent::__construct($delegate);
$this->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;
}
}
11 changes: 11 additions & 0 deletions reader/IPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Reader;


interface IPresenter
{


public function apply(string $inputLine): string;
}
14 changes: 14 additions & 0 deletions reader/Presenter/SimplePrinter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Reader\Presenter;


final class SimplePrinter implements \Reader\IPresenter
{


public function apply(string $inputLine): string
{
return $inputLine;
}
}
42 changes: 42 additions & 0 deletions reader/Presenter/WordsCounter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Reader\Presenter;


final class WordsCounter implements \Reader\IPresenter
{

/** @var array */
private $words = [];

/** @var counts */
private $counts = [];


public function __construct(array $words)
{
$this->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;
}
}
35 changes: 35 additions & 0 deletions reader/Processor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Reader;


final class Processor
{

/** @var \Iterator */
private $inputStream;

/** @var Presenter */
private $presenter;


public function __construct(\Iterator $inputStream, IPresenter $presenter)
{
$this->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;
}
}
}
10 changes: 10 additions & 0 deletions reader/data.txt
Original file line number Diff line number Diff line change
@@ -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 [] []
Loading