Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| CodeSyntaxes | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
| resolve | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Field; |
| 6 | |
| 7 | /** |
| 8 | * The syntaxes a code field can offer: the ones the panel's editor ships |
| 9 | * (panel/src/elements/code/languages.js). Both lists change together; |
| 10 | * panel/tests/contract/code-syntaxes.test.ts compares them. |
| 11 | */ |
| 12 | final class CodeSyntaxes |
| 13 | { |
| 14 | public const string DEFAULT = 'plaintext'; |
| 15 | |
| 16 | /** @var list<string> */ |
| 17 | public const array KEYS = [ |
| 18 | 'plaintext', |
| 19 | 'apacheconf', |
| 20 | 'bash', |
| 21 | 'clojure', |
| 22 | 'csharp', |
| 23 | 'css', |
| 24 | 'diff', |
| 25 | 'django', |
| 26 | 'docker', |
| 27 | 'fsharp', |
| 28 | 'go', |
| 29 | 'handlebars', |
| 30 | 'html', |
| 31 | 'ini', |
| 32 | 'java', |
| 33 | 'javascript', |
| 34 | 'json', |
| 35 | 'jsx', |
| 36 | 'latte', |
| 37 | 'liquid', |
| 38 | 'lisp', |
| 39 | 'lua', |
| 40 | 'markdown', |
| 41 | 'nginx', |
| 42 | 'nim', |
| 43 | 'ocaml', |
| 44 | 'odin', |
| 45 | 'php', |
| 46 | 'python', |
| 47 | 'ruby', |
| 48 | 'rust', |
| 49 | 'scss', |
| 50 | 'sql', |
| 51 | 'toml', |
| 52 | 'tsx', |
| 53 | 'twig', |
| 54 | 'typescript', |
| 55 | 'xml', |
| 56 | 'yaml', |
| 57 | 'zig', |
| 58 | ]; |
| 59 | |
| 60 | /** @var array<string, string> */ |
| 61 | public const array ALIASES = [ |
| 62 | 'js' => 'javascript', |
| 63 | 'ts' => 'typescript', |
| 64 | 'md' => 'markdown', |
| 65 | 'yml' => 'yaml', |
| 66 | 'sh' => 'bash', |
| 67 | 'hbs' => 'handlebars', |
| 68 | 'mustache' => 'handlebars', |
| 69 | 'jinja' => 'django', |
| 70 | 'jinja2' => 'django', |
| 71 | 'plain' => 'plaintext', |
| 72 | 'text' => 'plaintext', |
| 73 | ]; |
| 74 | |
| 75 | /** The key a syntax name or alias stands for, or null for one the panel lacks. */ |
| 76 | public static function resolve(string $syntax): ?string |
| 77 | { |
| 78 | $key = strtolower(trim($syntax)); |
| 79 | $key = self::ALIASES[$key] ?? $key; |
| 80 | |
| 81 | return in_array($key, self::KEYS, true) ? $key : null; |
| 82 | } |
| 83 | } |