-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from tomaszhanc/feature/class-name-inflector
Class Name Inflector
- Loading branch information
Showing
3 changed files
with
79 additions
and
8 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,28 @@ | ||
<?php | ||
|
||
namespace League\Tactician\Handler\MethodNameInflector; | ||
|
||
/** | ||
* Assumes the method is only the last portion of the class name. | ||
* | ||
* Examples: | ||
* - \MyGlobalCommand => $handler->myGlobalCommand() | ||
* - \My\App\CreateUser => $handler->createUser() | ||
*/ | ||
class ClassNameInflector implements MethodNameInflector | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function inflect($command, $commandHandler) | ||
{ | ||
$commandName = get_class($command); | ||
|
||
// If class name has a namespace separator, only take last portion | ||
if (strpos($commandName, '\\') !== false) { | ||
$commandName = substr($commandName, strrpos($commandName, '\\') + 1); | ||
} | ||
|
||
return strtolower($commandName[0]) . substr($commandName, 1); | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
tests/Handler/MethodNameInflector/ClassNameInflectorTest.php
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,48 @@ | ||
<?php | ||
|
||
namespace League\Tactician\Tests\Handler\MethodNameInflector; | ||
|
||
use League\Tactician\Handler\MethodNameInflector\ClassNameInflector; | ||
use League\Tactician\Tests\Fixtures\Command\CompleteTaskCommand; | ||
use League\Tactician\Tests\Fixtures\Handler\ConcreteMethodsHandler; | ||
use CommandWithoutNamespace; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ClassNameInflectorTest extends TestCase | ||
{ | ||
/** | ||
* @var ClassNameInflector | ||
*/ | ||
private $inflector; | ||
|
||
/** | ||
* @var object | ||
*/ | ||
private $mockHandler; | ||
|
||
protected function setUp() | ||
{ | ||
$this->inflector = new ClassNameInflector(); | ||
$this->mockHandler = new ConcreteMethodsHandler(); | ||
} | ||
|
||
public function testHandlesClassesWithoutNamespace() | ||
{ | ||
$command = new CommandWithoutNamespace(); | ||
|
||
$this->assertEquals( | ||
'commandWithoutNamespace', | ||
$this->inflector->inflect($command, $this->mockHandler) | ||
); | ||
} | ||
|
||
public function testHandlesNamespacedClasses() | ||
{ | ||
$command = new CompleteTaskCommand(); | ||
|
||
$this->assertEquals( | ||
'completeTaskCommand', | ||
$this->inflector->inflect($command, $this->mockHandler) | ||
); | ||
} | ||
} |