-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically add a role title when missing
- Loading branch information
1 parent
f945d9d
commit 160c114
Showing
4 changed files
with
121 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
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,13 @@ | ||
<?php | ||
|
||
namespace Silber\Bouncer\Database\Titles; | ||
|
||
use Illuminate\Database\Eloquent\model; | ||
|
||
class RoleTitle extends Title | ||
{ | ||
public function __construct(Model $role) | ||
{ | ||
$this->title = $this->humanize($role->name); | ||
} | ||
} |
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,47 @@ | ||
<?php | ||
|
||
namespace Silber\Bouncer\Database\Titles; | ||
|
||
use Illuminate\Support\Str; | ||
|
||
abstract class Title | ||
{ | ||
/** | ||
* The human-readable title. | ||
* | ||
* @var string | ||
*/ | ||
protected $title; | ||
|
||
/** | ||
* Convert the given string into a human-readable format. | ||
* | ||
* @param string|null $value | ||
* @return string | ||
*/ | ||
protected function humanize($value) | ||
{ | ||
if (is_null($value)) { | ||
return ''; | ||
} | ||
|
||
// First we'll convert the string to snake case. Then we'll | ||
// convert all dashes and underscores to spaces. Finally, | ||
// we'll uppercase the 1st letter of the whole string. | ||
$value = Str::snake($value); | ||
|
||
$value = preg_replace('~(?:-|_)+~', ' ', $value); | ||
|
||
return ucfirst($value); | ||
} | ||
|
||
/** | ||
* Get the title as a string. | ||
* | ||
* @return string | ||
*/ | ||
public function __toString() | ||
{ | ||
return $this->title; | ||
} | ||
} |
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,56 @@ | ||
<?php | ||
|
||
use Silber\Bouncer\Database\Role; | ||
use Silber\Bouncer\Database\Ability; | ||
|
||
class AutoTitlesTest extends BaseTestCase | ||
{ | ||
public function test_role_title_is_never_overwritten() | ||
{ | ||
$role = Role::create(['name' => 'admin', 'title' => 'Something Else']); | ||
|
||
$this->assertEquals('Something Else', $role->title); | ||
} | ||
|
||
public function test_role_title_is_capitalized() | ||
{ | ||
$role = Role::create(['name' => 'admin']); | ||
|
||
$this->assertEquals('Admin', $role->title); | ||
} | ||
|
||
public function test_role_title_with_spaces() | ||
{ | ||
$role = Role::create(['name' => 'site admin']); | ||
|
||
$this->assertEquals('Site admin', $role->title); | ||
} | ||
|
||
public function test_role_title_with_dashes() | ||
{ | ||
$role = Role::create(['name' => 'site-admin']); | ||
|
||
$this->assertEquals('Site admin', $role->title); | ||
} | ||
|
||
public function test_role_title_with_underscores() | ||
{ | ||
$role = Role::create(['name' => 'site_admin']); | ||
|
||
$this->assertEquals('Site admin', $role->title); | ||
} | ||
|
||
public function test_role_title_with_camel_casing() | ||
{ | ||
$role = Role::create(['name' => 'siteAdmin']); | ||
|
||
$this->assertEquals('Site admin', $role->title); | ||
} | ||
|
||
public function test_role_title_with_studly_casing() | ||
{ | ||
$role = Role::create(['name' => 'SiteAdmin']); | ||
|
||
$this->assertEquals('Site admin', $role->title); | ||
} | ||
} |