Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
22 / 22 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| Caesar | |
100.00% |
22 / 22 |
|
100.00% |
2 / 2 |
8 | |
100.00% |
1 / 1 |
| encode | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
| decode | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Utils\Encoding |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Utils\Encoding; |
| 16 | |
| 17 | /** |
| 18 | * Gray encoding class |
| 19 | * |
| 20 | * @package phpOMS\Utils\Encoding |
| 21 | * @license OMS License 2.0 |
| 22 | * @link https://jingga.app |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | class Caesar |
| 26 | { |
| 27 | /** |
| 28 | * ASCII lower char limit. |
| 29 | * |
| 30 | * @var int |
| 31 | * @since 1.0.0 |
| 32 | */ |
| 33 | public const LIMIT_LOWER = 0; |
| 34 | |
| 35 | /** |
| 36 | * ASCII upper char limit. |
| 37 | * |
| 38 | * @var int |
| 39 | * @since 1.0.0 |
| 40 | */ |
| 41 | public const LIMIT_UPPER = 127; |
| 42 | |
| 43 | /** |
| 44 | * Encode source text |
| 45 | * |
| 46 | * @param string $source Source to encode |
| 47 | * @param string $key Key used for encoding |
| 48 | * |
| 49 | * @return string |
| 50 | * |
| 51 | * @since 1.0.0 |
| 52 | */ |
| 53 | public static function encode(string $source, string $key) : string |
| 54 | { |
| 55 | $result = ''; |
| 56 | $length = \strlen($source); |
| 57 | $keyLength = \strlen($key) - 1; |
| 58 | |
| 59 | for ($i = 0, $j = 0; $i < $length; ++$i, ++$j) { |
| 60 | if ($j > $keyLength) { |
| 61 | $j = 0; |
| 62 | } |
| 63 | |
| 64 | $ascii = \ord($source[$i]) + \ord($key[$j]); |
| 65 | |
| 66 | if ($ascii > self::LIMIT_UPPER) { |
| 67 | $ascii = self::LIMIT_LOWER + ($ascii - self::LIMIT_UPPER); |
| 68 | } |
| 69 | |
| 70 | $result .= \chr($ascii); |
| 71 | } |
| 72 | |
| 73 | return $result; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Decode text |
| 78 | * |
| 79 | * @param string $raw Source to encode |
| 80 | * @param string $key Key used for decoding |
| 81 | * |
| 82 | * @return string |
| 83 | * |
| 84 | * @since 1.0.0 |
| 85 | */ |
| 86 | public static function decode(string $raw, string $key) : string |
| 87 | { |
| 88 | $result = ''; |
| 89 | $length = \strlen($raw); |
| 90 | $keyLength = \strlen($key) - 1; |
| 91 | |
| 92 | for ($i = 0, $j = 0; $i < $length; ++$i, ++$j) { |
| 93 | if ($j > $keyLength) { |
| 94 | $j = 0; |
| 95 | } |
| 96 | |
| 97 | $ascii = \ord($raw[$i]) - \ord($key[$j]); |
| 98 | |
| 99 | if ($ascii < self::LIMIT_LOWER) { |
| 100 | $ascii = self::LIMIT_UPPER + ($ascii - self::LIMIT_LOWER); |
| 101 | } |
| 102 | |
| 103 | $result .= \chr($ascii); |
| 104 | } |
| 105 | |
| 106 | return $result; |
| 107 | } |
| 108 | } |