Skip to content

Added Ascii enum with handy bytes #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2025
Merged
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
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Library for work with binary data and objects

Simple library for work with binary data and objects in PHP.
See the examples below for more information, or check out [`Encoder`](./src/Encoder.php), [`Decoder`](./src/Decoder.php), [`Serializer`](./src/Serializer.php) and [`Byter`](./src/Byter.php).
See the examples below for more information, or check out [`Encoder`](./src/Encoder.php), [`Decoder`](./src/Decoder.php), [`Serializer`](./src/Serializer.php), [`Byter`](./src/Byter.php) and [`Ascii`](./src/Ascii.php).

## Coder

```php
namespace PetrKnap\Binary;
Expand All @@ -13,6 +15,8 @@ $decoded = Binary::decode($encoded)->base64()->zlib()->checksum()->data;
printf('Data was coded into `%s` %s.', $encoded, $decoded === $data ? 'successfully' : 'unsuccessfully');
```

## Serializer

```php
namespace PetrKnap\Binary;

Expand All @@ -26,6 +30,8 @@ $unserialized = Binary::unserialize($serialized);
printf('Data was serialized into `%s` %s.', base64_encode($serialized), $unserialized === $data ? 'successfully' : 'unsuccessfully');
```

## Self-serializer

```php
namespace PetrKnap\Binary;

Expand Down Expand Up @@ -54,6 +60,8 @@ printf(
);
```

## Byter

```php
namespace PetrKnap\Binary;

Expand All @@ -70,6 +78,19 @@ printf(
);
```

## ASCII

```php
namespace PetrKnap\Binary;

printf(
Ascii::GroupSeparator->join(
Ascii::RecordSeparator->join(Ascii::UnitSeparator->join('200', 'EUR'), 'Maya Wilson'),
Ascii::RecordSeparator->join(Ascii::UnitSeparator->join('1600', 'USD'), 'Quinton Rice'),
),
);
```

---

Run `composer require petrknap/binary` to install it.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
],
"homepage": "https://github.com/petrknap/php-binary",
"keywords": [
"ascii",
"base64",
"binary",
"checksum",
Expand Down
31 changes: 31 additions & 0 deletions src/Ascii.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace PetrKnap\Binary;

/**
* @see https://datatracker.ietf.org/doc/html/rfc20
*/
enum Ascii: string
{
public const FILE_SEPARATOR = "\x1C";
public const GROUP_SEPARATOR = "\x1D";
public const NULL = "\x00";
public const RECORD_SEPARATOR = "\x1E";
public const UNIT_SEPARATOR = "\x1F";

case FileSeparator = self::FILE_SEPARATOR;
case GroupSeparator = self::GROUP_SEPARATOR;
case Null = self::NULL;
case RecordSeparator = self::RECORD_SEPARATOR;
case UnitSeparator = self::UNIT_SEPARATOR;

/**
* @see implode()
*/
public function join(string $element1, string ...$elementN): string
{
return implode($this->value, [$element1, ...$elementN]);
}
}
1 change: 1 addition & 0 deletions tests/ReadmeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static function getExpectedOutputsOfPhpExamples(): iterable
'serializer' => 'Data was serialized into `S7QysqoutjKxUiqpLEhVsi62srRSysxNTE/VL8hLB/GBUimJJYkgpoWxlVJngJ87L5cUFwMDA6+nh0sQkGYEYQ42ICkveqQTxCkOcndiWHdO5iVYlYtjiER48o/9Ux7aM7C9Z1qixnnFBCjB4Onq57LOKaFJyboWAA==` successfully.',
'self-serializer' => 'Data object was serialized into `DckxCsMwDAXQq4jMJbTd6qwdewnjfMoHSw6W1KX07s324NVyK1+W6+blcS/La0yo8PBU2UcfU5whVREXacMcLRA5pe486I32FnTGKs+kywcGq3Eqe0w2ws+GwiJ1XbbfHw==` successfully.',
'byter' => 'Hashes and data was unbitten into `IoPwxcGHZQM0gfF966vHI3kleehoRKHtC32Xh30RDlg5E026hmlpFnFwbchsoQARSibVpfbWVfuwAHLbGxjFl9eC8fiGaWkWcXBtyGyhABFKJtWl9tZV+7AActsbGMWX14Lx+A==` successfully.',
'ascii' => '200' . Ascii::UNIT_SEPARATOR . 'EUR' . Ascii::RECORD_SEPARATOR . 'Maya Wilson' . Ascii::GROUP_SEPARATOR . '1600' . Ascii::UNIT_SEPARATOR . 'USD' . Ascii::RECORD_SEPARATOR . 'Quinton Rice',
];
}
}
Loading