-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add ReadableCaptionTrait * Apply fixes from StyleCI
- Loading branch information
1 parent
7dc8342
commit 6fb6505
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace atk4\core; | ||
|
||
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' | ||
* | ||
* @param string $s | ||
* | ||
* @return string | ||
*/ | ||
public function readableCaption($s) | ||
{ | ||
//$s = 'this\\ _isNASA_MyBigBull shit_123\Foo'; | ||
|
||
// first remove not allowed characters and uppercase words | ||
$s = ucwords(preg_replace('/[^a-z0-9]+/i', ' ', $s)); | ||
|
||
// and then run regex to split camelcased words too | ||
$s = array_map('trim', preg_split('/^[^A-Z\d]+\K|[A-Z\d][^A-Z\d]+\K/', $s, -1, PREG_SPLIT_NO_EMPTY)); | ||
$s = implode(' ', $s); | ||
|
||
return $s; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace atk4\core\tests; | ||
|
||
use atk4\core\ReadableCaptionTrait; | ||
|
||
/** | ||
* @coversDefaultClass \atk4\core\ReadableCaptionTrait | ||
*/ | ||
class ReadableCaptionTraitTest extends \atk4\core\PHPUnit7_AgileTestCase | ||
{ | ||
/** | ||
* Test readableCaption method. | ||
*/ | ||
public function testReadableCaption() | ||
{ | ||
$a = new ReadableCaptionMock(); | ||
|
||
$this->assertEquals('User Defined Entity', $a->readableCaption('userDefinedEntity')); | ||
$this->assertEquals('New NASA Module', $a->readableCaption('newNASA_module')); | ||
$this->assertEquals('This Is NASA My Big Bull Shit 123 Foo', $a->readableCaption('this\\ _isNASA_MyBigBull shit_123\Foo')); | ||
} | ||
} | ||
|
||
// @codingStandardsIgnoreStart | ||
class ReadableCaptionMock | ||
{ | ||
use ReadableCaptionTrait; | ||
} | ||
// @codingStandardsIgnoreEnd |