byname
![Software License][ico-license]
![Coverage Status][ico-scrutinizer]
![Total Downloads][ico-downloads], (*1)
PHP trait for bynaming (nicknaming) classes/objects., (*2)
Install
Via Composer, (*3)
``` bash
$ composer require gregoriohc/byname, (*4)
## Usage
By default, the class byname is the class basename (without namespace).
``` php
class MyClass {
use HasByname;
}
echo MyClass::byname();
// MyClass
Removing prefix and/or suffix
You can remove some characters from the beginning (prefix) or the end (suffix) of the byname overriding the bynamePrefix and bynameSuffix methods and returning the string to remove or the number of characters., (*5)
``` php
class MyClass {
use HasByname;
protected static function bynamePrefix()
{
return 'My';
}
}, (*6)
echo MyClass::byname();
// Class, (*7)
``` php
class MyClass {
use HasByname;
protected static function bynameSuffix()
{
return 5;
}
}
echo MyClass::byname();
// My
A more complex example, mixing this with inheritance:, (*8)
``` php
abstract class BaseController {
use HasByname;
protected static function bynameSuffix()
{
return 'Controller';
}
public function model()
{
$class = '\App\' . $this->byname();
return new $class();
}
}, (*9)
class UserController extends BaseController {
...
}, (*10)
echo UserController::byname();
// User, (*11)
$user = (new UserController)->model();
print_r($user);
// App\User Object (...), (*12)
### Custom byname
You can set a custom byname overriding the `bynameValue` method.
``` php
class MyClass {
use HasByname;
protected static function bynameValue()
{
return 'Cool';
}
}
echo MyClass::byname();
// Cool
Case
Yu can also get the name in three diferent cases:, (*13)
``` php
class MyClass {
use HasByname;
}, (*14)
echo MyClass::bynameSnake();
// my_class
echo MyClass::bynameCamel();
// myClass
echo MyClass::bynameStudly();
// MyClass, (*15)
## Testing
``` bash
$ composer test
Change log
Please see CHANGELOG for more information on what has changed recently., (*16)
Contributing
Please see CONTRIBUTING for details., (*17)
Security
If you discover any security related issues, please email gregoriohc@gmail.com instead of using the issue tracker., (*18)
Socialware
You're free to use this package, but if it makes it to your production environment I highly appreciate you sharing it on any social network., (*19)
Credits
License
The MIT License (MIT). Please see License File for more information., (*20)