Skip to content

Commit

Permalink
Fix "ID" caption case (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jul 6, 2022
1 parent dd25d5f commit 8ed793d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/ReadableCaptionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ trait ReadableCaptionTrait
/**
* Generates human readable caption from camelCase model class name or field names.
*
* This will translate 'this\\ _isNASA_MyBigBull shit_123\Foo'
* into 'This Is NASA My Big Bull Shit 123 Foo'
* This will translate 'this\ _isNASA_MyBigBull shit_123\Foo' into 'This Is NASA My Big Bull Shit 123 Foo'.
*/
public function readableCaption(string $s): string
{
Expand All @@ -21,6 +20,9 @@ public function readableCaption(string $s): string
$s = array_map('trim', preg_split('~(?:^|[A-Z\d])[^A-Z\d]+\K~', $s, -1, \PREG_SPLIT_NO_EMPTY));
$s = implode(' ', $s);

// replace "Id" with "ID"
$s = preg_replace('~(?<=^| )Id~', 'ID', $s);

return $s;
}
}
5 changes: 4 additions & 1 deletion tests/ReadableCaptionTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public function testReadableCaption(): void

$this->assertSame('User Defined Entity', $a->readableCaption('userDefinedEntity'));
$this->assertSame('New NASA Module', $a->readableCaption('newNASA_module'));
$this->assertSame('This Is NASA My Big Bull Shit 123 Foo', $a->readableCaption('this\\ _isNASA_MyBigBull shit_123\Foo'));
$this->assertSame('This Is NASA My Big Bull Shit 123 Foo', $a->readableCaption('this\ _isNASA_MyBigBull shit_123\Foo'));

$this->assertSame('ID', $a->readableCaption('id'));
$this->assertSame('Account ID', $a->readableCaption('account_id'));
}
}

Expand Down

0 comments on commit 8ed793d

Please sign in to comment.