|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Serendipity\Notation; |
| 6 | + |
| 7 | +use Jawira\CaseConverter\Convert; |
| 8 | +use Morph\Support\Reflective\Notation; |
| 9 | + |
| 10 | +use function Serendipity\Type\Cast\stringify; |
| 11 | + |
| 12 | +if (! function_exists(__NAMESPACE__ . '\format')) { |
| 13 | + function format(string $string, Notation $notation): string |
| 14 | + { |
| 15 | + return match ($notation) { |
| 16 | + Notation::SNAKE => snakify($string), |
| 17 | + Notation::CAMEL => camelify($string), |
| 18 | + Notation::PASCAL => pascalify($string), |
| 19 | + Notation::ADA => adaify($string), |
| 20 | + Notation::MACRO => macroify($string), |
| 21 | + Notation::KEBAB => kebabify($string), |
| 22 | + Notation::TRAIN => trainify($string), |
| 23 | + Notation::COBOL => cobolify($string), |
| 24 | + Notation::LOWER => lowerify($string), |
| 25 | + Notation::UPPER => upperify($string), |
| 26 | + Notation::TITLE => titlelify($string), |
| 27 | + Notation::SENTENCE => sentencify($string), |
| 28 | + Notation::DOT => dotify($string), |
| 29 | + Notation::NONE => $string, |
| 30 | + }; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +if (! function_exists(__NAMESPACE__ . '\snakify')) { |
| 35 | + function snakify(string $string, bool $includeDigits = true): string |
| 36 | + { |
| 37 | + $string = (new Convert($string))->toSnake(); |
| 38 | + if ($includeDigits) { |
| 39 | + return ltrim(stringify(preg_replace('/(\d+)/', '_$0', $string)), '_'); |
| 40 | + } |
| 41 | + return $string; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +if (! function_exists(__NAMESPACE__ . '\camelify')) { |
| 46 | + function camelify(string $string): string |
| 47 | + { |
| 48 | + return (new Convert($string))->toCamel(); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +if (! function_exists(__NAMESPACE__ . '\pascalify')) { |
| 53 | + function pascalify(string $string): string |
| 54 | + { |
| 55 | + return (new Convert($string))->toPascal(); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +if (! function_exists(__NAMESPACE__ . '\adaify')) { |
| 60 | + function adaify(string $string): string |
| 61 | + { |
| 62 | + return (new Convert($string))->toAda(); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +if (! function_exists(__NAMESPACE__ . '\macroify')) { |
| 67 | + function macroify(string $string): string |
| 68 | + { |
| 69 | + return (new Convert($string))->toMacro(); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +if (! function_exists(__NAMESPACE__ . '\kebabify')) { |
| 74 | + function kebabify(string $string): string |
| 75 | + { |
| 76 | + return (new Convert($string))->toKebab(); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +if (! function_exists(__NAMESPACE__ . '\trainify')) { |
| 81 | + function trainify(string $string): string |
| 82 | + { |
| 83 | + return (new Convert($string))->toTrain(); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +if (! function_exists(__NAMESPACE__ . '\cobolify')) { |
| 88 | + function cobolify(string $string): string |
| 89 | + { |
| 90 | + return (new Convert($string))->toCobol(); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +if (! function_exists(__NAMESPACE__ . '\lowerify')) { |
| 95 | + function lowerify(string $string): string |
| 96 | + { |
| 97 | + return (new Convert($string))->toLower(); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +if (! function_exists(__NAMESPACE__ . '\upperify')) { |
| 102 | + function upperify(string $string): string |
| 103 | + { |
| 104 | + return (new Convert($string))->toUpper(); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +if (! function_exists(__NAMESPACE__ . '\titlelify')) { |
| 109 | + function titlelify(string $string): string |
| 110 | + { |
| 111 | + return (new Convert($string))->toTitle(); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +if (! function_exists(__NAMESPACE__ . '\sentencify')) { |
| 116 | + function sentencify(string $string): string |
| 117 | + { |
| 118 | + return (new Convert($string))->toSentence(); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +if (! function_exists(__NAMESPACE__ . '\dotify')) { |
| 123 | + function dotify(string $string): string |
| 124 | + { |
| 125 | + return (new Convert($string))->toDot(); |
| 126 | + } |
| 127 | +} |
0 commit comments