Skip to content

Commit

Permalink
Feature/readable caption (#114)
Browse files Browse the repository at this point in the history
* add ReadableCaptionTrait

* Apply fixes from StyleCI
  • Loading branch information
DarkSide666 authored and romaninsh committed Nov 26, 2019
1 parent 7dc8342 commit 6fb6505
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/ReadableCaptionTrait.php
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;
}
}
30 changes: 30 additions & 0 deletions tests/ReadableCaptionTraitTest.php
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

0 comments on commit 6fb6505

Please sign in to comment.