Skip to content

Commit

Permalink
Refactor due to php 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
vvval committed Mar 11, 2024
1 parent 4a71f9e commit 6ff0241
Show file tree
Hide file tree
Showing 22 changed files with 489 additions and 290 deletions.
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
"email": "[email protected]"
}
],
"require": {
"php": "^8.1",
"ext-zlib": "*",
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
"phpunit/phpunit": "^11.0"
},
"autoload": {
"psr-4": {
Expand All @@ -20,4 +25,4 @@
"Vvval\\Binary\\Tests\\": "tests"
}
}
}
}
10 changes: 2 additions & 8 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
stopOnError="false"
syntaxCheck="true">
stopOnError="false">
<testsuites>
<testsuite name="Application Tests">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
</phpunit>
34 changes: 34 additions & 0 deletions src/Binary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Vvval\Binary;

use Vvval\Binary\Binary\Reader;
use Vvval\Binary\Binary\Writer;
use Vvval\Binary\Handler\Handler;
use Vvval\Binary\Handler\Mode;

class Binary
{
public static function reader(string $filename, bool $compression = false): Reader
{
$handler = new Handler($filename, Mode::READ, $compression);

return new Reader($handler, $filename);
}

public static function appender(string $filename, bool $compression = false): Writer
{
$handler = new Handler($filename, Mode::APPEND, $compression);

return new Writer($handler, $filename);
}

public static function writer(string $filename, bool $compression = false): Writer
{
$handler = new Handler($filename, Mode::WRITE, $compression);

return new Writer($handler, $filename);
}
}
85 changes: 85 additions & 0 deletions src/Binary/Reader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Vvval\Binary\Binary;

use Vvval\Binary\Exceptions\ReadDataException;
use Vvval\Binary\Handler\ReadableHandlerInterface;

/**
* @internal
*/
class Reader
{
private const FORMAT_LENGTH = 4;
private const FORMAT = 'L';

private ?ReadableHandlerInterface $handler;

public function __construct(
ReadableHandlerInterface $handler,
private readonly string $filename,
) {
$this->handler = $handler;
}

public function finish(): void
{
unset($this->handler);
}

/**
* @return iterable<string>
* @throws ReadDataException
*/
public function readAll(): iterable
{
while (true) {
$line = $this->readLine();
if ($line === null) {
$this->finish();
return;
}

yield $line;
}
}

/**
* @throws ReadDataException
*/
public function readLine(): ?string
{
return $this->read();
}

public function __destruct()
{
$this->finish();
}

/**
* @throws ReadDataException
*/
private function read(): ?string
{
if (empty($this->handler)) {
throw ReadDataException::handlerAlreadyClosed($this->filename);
}

$length = $this->handler->read(self::FORMAT_LENGTH);
if (empty($length)) {
return null;
}

if (mb_strlen($length) !== self::FORMAT_LENGTH) {
throw ReadDataException::dataCorrupted($this->filename);
}

$data = unpack(self::FORMAT . 'len', $length);
$line = $this->handler->read((int)$data['len']);

return $line !== false ? $line : null;
}
}
50 changes: 50 additions & 0 deletions src/Binary/Writer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Vvval\Binary\Binary;

use Vvval\Binary\Exceptions\BinaryException;
use Vvval\Binary\Exceptions\WriteDataException;
use Vvval\Binary\Handler\WriteableHandlerInterface;

/**
* @internal
*/
class Writer
{
private const FORMAT = 'L';

private ?WriteableHandlerInterface $handler;

public function __construct(
WriteableHandlerInterface $handler,
private readonly string $filename,
) {
$this->handler = $handler;
}

public function finish(): void
{
unset($this->handler);
}

public function writeLine(string $data): int|false
{
if (empty($this->handler)) {
throw WriteDataException::handlerAlreadyClosed($this->filename);
}

$length = pack(self::FORMAT, mb_strlen($data));

return $this->handler->write($length, $data);
}

/**
* @throws BinaryException
*/
public function __destruct()
{
$this->finish();
}
}
Loading

0 comments on commit 6ff0241

Please sign in to comment.