-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
489 additions
and
290 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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": { | ||
|
@@ -20,4 +25,4 @@ | |
"Vvval\\Binary\\Tests\\": "tests" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.