|
| 1 | +# co-stack/reversible - Reversible functions for PHP |
| 2 | + |
| 3 | +## What is a reversible function |
| 4 | + |
| 5 | +A reversible function is a function that can be executed forwards and backwards (reverted). |
| 6 | +They are side effect free (idempotent) and stateless. |
| 7 | + |
| 8 | +These functions are especially useful for transport encoding, persistence mapping, encryption and many other use cases. |
| 9 | + |
| 10 | +## Notice |
| 11 | + |
| 12 | +Some encodings are not fully idempotent, like `Base64Encoding`. The `base64_encode` function does not preserve data types (int, float, bool). |
| 13 | +Implementations of `Reversible` that may not be fully idempotent implement the `Lossy` interface. |
| 14 | + |
| 15 | +## Examples |
| 16 | + |
| 17 | +I have prepared some useful examples for you, which show the versatility of this package |
| 18 | + |
| 19 | +### Scalar value send over the air |
| 20 | + |
| 21 | +On System A: |
| 22 | +```php |
| 23 | + |
| 24 | +// System A |
| 25 | +$input = random_bytes(256); |
| 26 | +$encoding = new \CoStack\Reversible\Encoding\Base64Encoding(); |
| 27 | +$output = $encoding->execute($input); |
| 28 | + |
| 29 | +// System B |
| 30 | +$encoding = new \CoStack\Reversible\Encoding\Base64Encoding(); |
| 31 | +$restoredInput = $encoding->reverse($output); |
| 32 | +// $restoredInput is exactly $input what was generated on System A |
| 33 | +``` |
| 34 | + |
| 35 | +### Associative array transportation via string without serialization |
| 36 | + |
| 37 | +```php |
| 38 | + // Shared Library |
| 39 | + function getPipe(): \CoStack\Reversible\ReversiblePipe { |
| 40 | + $pipe = new \CoStack\Reversible\ReversiblePipe(); |
| 41 | + $pipe->enqueue(new \CoStack\Reversible\Mapping\ArrayKeyMapping(['key1', 'key2', 'payload'])); |
| 42 | + $pipe->enqueue(new \CoStack\Reversible\RecursiveReversible(new \CoStack\Reversible\Encoding\Base64Encoding())); |
| 43 | + $pipe->enqueue(new \CoStack\Reversible\Transform\ImplodeTransform()); |
| 44 | + return $pipe; |
| 45 | +} |
| 46 | + |
| 47 | +// System A |
| 48 | +$array = [ |
| 49 | + 'key1' => 1, |
| 50 | + 'key2' => 'value', |
| 51 | + 'payload' => uniqid(), |
| 52 | +]; |
| 53 | +$pipe = getPipe(); |
| 54 | +$safeEncodedObject = $pipe->execute($array); |
| 55 | + |
| 56 | +// The string will contain base64 encoded values, imploded with "|". There are no associative keys in the string because they have been replaced by the ArrayKeyMapping |
| 57 | + |
| 58 | +// System B |
| 59 | +$pipe = getPipe(); |
| 60 | +$array = $pipe->reverse($safeEncodedObject); |
| 61 | + ``` |
| 62 | +Please notice that `ImplodeTransform` is lossy because `explode(',', implode(',', [2])) === ['2']` (an integer will become a string). |
| 63 | + |
| 64 | +### Object transportation via problematic medium (e.g. get parameter) |
| 65 | + |
| 66 | +```php |
| 67 | +// Shared Library |
| 68 | +function getPipe(): \CoStack\Reversible\ReversiblePipe { |
| 69 | + $pipe = new \CoStack\Reversible\ReversiblePipe(); |
| 70 | + $pipe->enqueue(new \CoStack\Reversible\Encoding\SerializationEncoding()); |
| 71 | + $pipe->enqueue(new \CoStack\Reversible\Encoding\UrlEncode()); |
| 72 | + return $pipe; |
| 73 | +} |
| 74 | + |
| 75 | +// System A |
| 76 | +$object = new SplFileInfo('file.txt'); |
| 77 | +$pipe = getPipe(); |
| 78 | +$safeEncodedObject = $pipe->execute($object); |
| 79 | + |
| 80 | +// System B |
| 81 | +$pipe = getPipe(); |
| 82 | +$object = $pipe->reverse($safeEncodedObject); |
| 83 | +``` |
0 commit comments