Skip to content

Commit eb24183

Browse files
committed
feat: created ASCII enum
1 parent 8b63c5d commit eb24183

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

README.md

Lines changed: 20 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,17 @@ printf(
7078
);
7179
```
7280

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

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

src/Ascii.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
* @todo upgrade to PHP 8.2, move values to cases and move `values of cases` region under `groups of cases` region
11+
*/
12+
enum Ascii: string
13+
{
14+
#region values of cases
15+
public const FILE_SEPARATOR = "\x1C";
16+
public const GROUP_SEPARATOR = "\x1D";
17+
public const NULL = "\x00";
18+
public const RECORD_SEPARATOR = "\x1E";
19+
public const UNIT_SEPARATOR = "\x1F";
20+
#endregion
21+
22+
/**
23+
* Separates different files (databases)
24+
*/
25+
case FileSeparator = self::FILE_SEPARATOR;
26+
/**
27+
* Separates different groups (tables) of the same file (database)
28+
*/
29+
case GroupSeparator = self::GROUP_SEPARATOR;
30+
case Null = self::NULL;
31+
/**
32+
* Separates different records (rows) of the same group (table)
33+
*/
34+
case RecordSeparator = self::RECORD_SEPARATOR;
35+
/**
36+
* Separates different units (columns) of the same record (row)
37+
*/
38+
case UnitSeparator = self::UNIT_SEPARATOR;
39+
40+
#region groups of cases
41+
/**
42+
* Used to separate and qualify information in a logical sense
43+
*/
44+
public const INFORMATION_SEPARATORS = [
45+
self::FileSeparator,
46+
self::GroupSeparator,
47+
self::RecordSeparator,
48+
self::UnitSeparator,
49+
];
50+
#endregion
51+
52+
/**
53+
* @see implode()
54+
*/
55+
public function join(string $element1, string ...$elementN): string
56+
{
57+
return implode($this->value, [$element1, ...$elementN]);
58+
}
59+
}

0 commit comments

Comments
 (0)