Skip to content

Commit

Permalink
refactor: removed root en/de/coder interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
petrknap committed Nov 2, 2024
1 parent 9f08f42 commit 0236656
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 106 deletions.
51 changes: 30 additions & 21 deletions src/Coder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,43 @@

namespace PetrKnap\Binary;

use PetrKnap\Shorts\Exception;

/**
* @internal please use subclass
*
* @phpstan-consistent-constructor override {@see self::create()} if not
*
* @implements CoderInterface<Exception\CouldNotProcessData>
*/
abstract class Coder implements CoderInterface
abstract class Coder
{
public function __construct(
protected readonly string $data = '',
final public function __construct(
public readonly string $data = '',
) {
}

public function withData(string $data): static
{
return static::create($this, $data);
return new static($data);
}

public function getData(): string
{
return $this->data;
}
/**
* @see Coder\Base64
*
* @throws Coder\Exception\CoderException
*/
abstract public function base64(): static;

protected static function create(self $coder, string $data): static
{
return new static($data);
}
/**
* @see Coder\Checksum
*
* @throws Coder\Exception\CoderException
*/
abstract public function checksum(string|null $algorithm = null): static;

/**
* @see Coder\Hex
*
* @throws Coder\Exception\CoderException
*/
abstract public function hex(): static;

/**
* @see Coder\zlib
*
* @throws Coder\Exception\CoderException
*/
abstract public function zlib(): static;
}
47 changes: 0 additions & 47 deletions src/CoderInterface.php

This file was deleted.

10 changes: 5 additions & 5 deletions src/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@

namespace PetrKnap\Binary;

final class Decoder extends Coder implements DecoderInterface
final class Decoder extends Coder
{
public function base64(): static
{
return static::create($this, (new Coder\Base64())->decode(
return $this->withData((new Coder\Base64())->decode(
$this->data,
));
}

public function checksum(string|null $algorithm = null): static
{
return static::create($this, (new Coder\Checksum())->decode(
return $this->withData((new Coder\Checksum())->decode(
$this->data,
algorithm: $algorithm,
));
}

public function hex(): static
{
return static::create($this, (new Coder\Hex())->decode(
return $this->withData((new Coder\Hex())->decode(
$this->data,
));
}

public function zlib(int|null $maxLength = null): static
{
return static::create($this, (new Coder\Zlib())->decode(
return $this->withData((new Coder\Zlib())->decode(
$this->data,
maxLength: $maxLength,
));
Expand Down
13 changes: 0 additions & 13 deletions src/DecoderInterface.php

This file was deleted.

10 changes: 5 additions & 5 deletions src/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@

namespace PetrKnap\Binary;

final class Encoder extends Coder implements EncoderInterface
final class Encoder extends Coder
{
public function base64(bool|null $urlSafe = null): static
{
return static::create($this, (new Coder\Base64())->encode(
return $this->withData((new Coder\Base64())->encode(
$this->data,
urlSafe: $urlSafe,
));
}

public function checksum(string|null $algorithm = null): static
{
return static::create($this, (new Coder\Checksum())->encode(
return $this->withData((new Coder\Checksum())->encode(
$this->data,
algorithm: $algorithm,
));
}

public function hex(): static
{
return static::create($this, (new Coder\Hex())->encode(
return $this->withData((new Coder\Hex())->encode(
$this->data,
));
}

public function zlib(int|null $encoding = null, int|null $level = null): static
{
return static::create($this, (new Coder\Zlib())->encode(
return $this->withData((new Coder\Zlib())->encode(
$this->data,
encoding: $encoding,
level: $level,
Expand Down
15 changes: 0 additions & 15 deletions src/EncoderInterface.php

This file was deleted.

0 comments on commit 0236656

Please sign in to comment.