Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| ColoredBlockquotes | |
100.00% |
15 / 15 |
|
100.00% |
5 / 5 |
8 | |
100.00% |
1 / 1 |
| signature | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| resolve | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getSignatures | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| expand | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| stringStartsWithSignature | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Hyde\Markdown\Processing; |
| 6 | |
| 7 | use Hyde\Markdown\Contracts\MarkdownShortcodeContract; |
| 8 | use Hyde\Markdown\Models\Markdown; |
| 9 | |
| 10 | use function ltrim; |
| 11 | use function explode; |
| 12 | use function sprintf; |
| 13 | use function str_starts_with; |
| 14 | use function trim; |
| 15 | |
| 16 | /** |
| 17 | * @internal This class may be refactored further, thus extending this class is discouraged. |
| 18 | */ |
| 19 | class ColoredBlockquotes implements MarkdownShortcodeContract |
| 20 | { |
| 21 | /** @var string The core signature. We combine this with an additional check for color later. */ |
| 22 | protected static string $signature = '>'; |
| 23 | |
| 24 | /** @var array<string> */ |
| 25 | protected static array $signatures = ['>danger', '>info', '>success', '>warning']; |
| 26 | |
| 27 | public static function signature(): string |
| 28 | { |
| 29 | return static::$signature; |
| 30 | } |
| 31 | |
| 32 | public static function resolve(string $input): string |
| 33 | { |
| 34 | return self::stringStartsWithSignature($input) |
| 35 | ? static::expand($input) |
| 36 | : $input; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @internal |
| 41 | * |
| 42 | * @return array<string> |
| 43 | */ |
| 44 | public static function getSignatures(): array |
| 45 | { |
| 46 | return self::$signatures; |
| 47 | } |
| 48 | |
| 49 | protected static function expand(string $input): string |
| 50 | { |
| 51 | $parts = explode(' ', $input, 2); |
| 52 | $class = ltrim($parts[0], '>'); |
| 53 | $contents = trim($parts[1] ?? '', ' '); |
| 54 | |
| 55 | return sprintf('<blockquote class="%s">%s</blockquote>', |
| 56 | $class, trim(Markdown::render($contents)) |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | protected static function stringStartsWithSignature(string $input): bool |
| 61 | { |
| 62 | foreach (static::$signatures as $signature) { |
| 63 | if (str_starts_with($input, $signature)) { |
| 64 | return true; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return false; |
| 69 | } |
| 70 | } |