Skip to content

Commit 3c3d9a8

Browse files
authored
Merge pull request #21 from petrknap/ascii
Added `Ascii` enum with handy bytes
2 parents cd99a46 + ac88484 commit 3c3d9a8

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Library for work with binary data and objects
22

33
Simple library for work with binary data and objects in PHP.
4-
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).
4+
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).
5+
6+
## Coder
57

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

18+
## Serializer
19+
1620
```php
1721
namespace PetrKnap\Binary;
1822

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

33+
## Self-serializer
34+
2935
```php
3036
namespace PetrKnap\Binary;
3137

@@ -54,6 +60,8 @@ printf(
5460
);
5561
```
5662

63+
## Byter
64+
5765
```php
5866
namespace PetrKnap\Binary;
5967

@@ -70,6 +78,19 @@ printf(
7078
);
7179
```
7280

81+
## ASCII
82+
83+
```php
84+
namespace PetrKnap\Binary;
85+
86+
printf(
87+
Ascii::GroupSeparator->join(
88+
Ascii::RecordSeparator->join(Ascii::UnitSeparator->join('200', 'EUR'), 'Maya Wilson'),
89+
Ascii::RecordSeparator->join(Ascii::UnitSeparator->join('1600', 'USD'), 'Quinton Rice'),
90+
),
91+
);
92+
```
93+
7394
---
7495

7596
Run `composer require petrknap/binary` to install it.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
],
2626
"homepage": "https://github.com/petrknap/php-binary",
2727
"keywords": [
28+
"ascii",
2829
"base64",
2930
"binary",
3031
"checksum",

src/Ascii.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PetrKnap\Binary;
6+
7+
/**
8+
* @see https://datatracker.ietf.org/doc/html/rfc20
9+
*/
10+
enum Ascii: string
11+
{
12+
public const FILE_SEPARATOR = "\x1C";
13+
public const GROUP_SEPARATOR = "\x1D";
14+
public const NULL = "\x00";
15+
public const RECORD_SEPARATOR = "\x1E";
16+
public const UNIT_SEPARATOR = "\x1F";
17+
18+
case FileSeparator = self::FILE_SEPARATOR;
19+
case GroupSeparator = self::GROUP_SEPARATOR;
20+
case Null = self::NULL;
21+
case RecordSeparator = self::RECORD_SEPARATOR;
22+
case UnitSeparator = self::UNIT_SEPARATOR;
23+
24+
/**
25+
* @see implode()
26+
*/
27+
public function join(string $element1, string ...$elementN): string
28+
{
29+
return implode($this->value, [$element1, ...$elementN]);
30+
}
31+
}

tests/ReadmeTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public static function getExpectedOutputsOfPhpExamples(): iterable
2424
'serializer' => 'Data was serialized into `S7QysqoutjKxUiqpLEhVsi62srRSysxNTE/VL8hLB/GBUimJJYkgpoWxlVJngJ87L5cUFwMDA6+nh0sQkGYEYQ42ICkveqQTxCkOcndiWHdO5iVYlYtjiER48o/9Ux7aM7C9Z1qixnnFBCjB4Onq57LOKaFJyboWAA==` successfully.',
2525
'self-serializer' => 'Data object was serialized into `DckxCsMwDAXQq4jMJbTd6qwdewnjfMoHSw6W1KX07s324NVyK1+W6+blcS/La0yo8PBU2UcfU5whVREXacMcLRA5pe486I32FnTGKs+kywcGq3Eqe0w2ws+GwiJ1XbbfHw==` successfully.',
2626
'byter' => 'Hashes and data was unbitten into `IoPwxcGHZQM0gfF966vHI3kleehoRKHtC32Xh30RDlg5E026hmlpFnFwbchsoQARSibVpfbWVfuwAHLbGxjFl9eC8fiGaWkWcXBtyGyhABFKJtWl9tZV+7AActsbGMWX14Lx+A==` successfully.',
27+
'ascii' => '200' . Ascii::UNIT_SEPARATOR . 'EUR' . Ascii::RECORD_SEPARATOR . 'Maya Wilson' . Ascii::GROUP_SEPARATOR . '1600' . Ascii::UNIT_SEPARATOR . 'USD' . Ascii::RECORD_SEPARATOR . 'Quinton Rice',
2728
];
2829
}
2930
}

0 commit comments

Comments
 (0)