| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Core; |
| 6: | |
| 7: | trait ReadableCaptionTrait |
| 8: | { |
| 9: | /** |
| 10: | * Generates human readable caption from camelCase model class name or field names. |
| 11: | * |
| 12: | * This will translate 'this\ _isNASA_MyBigBull shit_123\Foo' into 'This Is NASA My Big Bull Shit 123 Foo'. |
| 13: | */ |
| 14: | public function readableCaption(string $s): string |
| 15: | { |
| 16: | // first remove not allowed characters and uppercase words |
| 17: | $s = ucwords(preg_replace('~[^a-z\d]+~i', ' ', $s)); |
| 18: | |
| 19: | // and then run regex to split camelcased words too |
| 20: | $s = array_map('trim', preg_split('~(?:^|[A-Z\d])[^A-Z\d]+\K~', $s, -1, \PREG_SPLIT_NO_EMPTY)); |
| 21: | $s = implode(' ', $s); |
| 22: | |
| 23: | // replace "Id" with "ID" |
| 24: | $s = preg_replace('~(?<=^| )Id~', 'ID', $s); |
| 25: | |
| 26: | return $s; |
| 27: | } |
| 28: | } |
| 29: |