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
38 changes: 38 additions & 0 deletions app/Ilex/Visits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace App\Ilex;

use JsonSerializable;

final class Visits implements JsonSerializable
{
public function __construct(
private array $dateVisit,
) {
}

public function add(string $data): void
{
if (isset($this->dateVisit[$data])) {
$this->dateVisit[$data]++;
return;
}

$this->dateVisit[$data] = 1;
}

public static function init(string $date): self
{

return new Visits([$date => 1]);
}

public function jsonSerialize(): array
{
ksort($this->dateVisit);
return $this->dateVisit;

}
}
35 changes: 34 additions & 1 deletion app/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,45 @@

namespace App;

use App\Ilex\Visits;
use Exception;

final class Parser
{

public function parse(string $inputPath, string $outputPath): void
{
throw new Exception('TODO');
$handle = \fopen($inputPath, 'rb');

if ($handle === false) {
throw new Exception("Cannot open file: $inputPath");
}

$ar = [];

try {
// Read file line by line
while (($line = fgets($handle)) !== false) {

[$key, $date] = \explode(',', $line);
$date = \substr($date, 0, 10);

$key = \strstr($key, '/blog');

if (isset($ar[$key])) {
$ar[$key]->add($date);
continue;
}

$ar[$key] = Visits::init($date);

}

} finally {
\fclose($handle);
}


file_put_contents($outputPath, \json_encode($ar, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT));
}
}