Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| ContentLines | |
100.00% |
11 / 11 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| escape | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fold | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| render | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Faker\Support; |
| 3 | |
| 4 | /** |
| 5 | * Helpers for the line based text formats iCalendar and vCard (RFC 5545 and RFC 2426). |
| 6 | */ |
| 7 | final class ContentLines |
| 8 | { |
| 9 | /** |
| 10 | * @codeCoverageIgnore |
| 11 | */ |
| 12 | private function __construct() |
| 13 | { |
| 14 | } |
| 15 | |
| 16 | public static function escape(string $value): string |
| 17 | { |
| 18 | return str_replace(['\\', "\r\n", "\n", ',', ';'], ['\\\\', '\n', '\n', '\,', '\;'], $value); |
| 19 | } |
| 20 | |
| 21 | /** Lines should not be longer than 75 octets, longer lines continue on the next line starting with a space. */ |
| 22 | public static function fold(string $line): string |
| 23 | { |
| 24 | $parts = []; |
| 25 | $limit = 75; |
| 26 | while (strlen($line) > $limit) { |
| 27 | $chunk = mb_strcut($line, 0, $limit, 'UTF-8'); |
| 28 | $parts[] = $chunk; |
| 29 | $line = substr($line, strlen($chunk)); |
| 30 | $limit = 74; |
| 31 | } |
| 32 | $parts[] = $line; |
| 33 | |
| 34 | return implode("\r\n ", $parts); |
| 35 | } |
| 36 | |
| 37 | /** @param array<int, string> $lines */ |
| 38 | public static function render(array $lines): string |
| 39 | { |
| 40 | return implode('', array_map(fn (string $line) => self::fold($line) . "\r\n", $lines)); |
| 41 | } |
| 42 | } |