Skip to content

Commit

Permalink
Merge pull request #118 from tomaszhanc/feature/class-name-inflector
Browse files Browse the repository at this point in the history
Class Name Inflector
  • Loading branch information
rosstuck authored Nov 30, 2017
2 parents b7963ca + 4bfe1d4 commit 112a6bc
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 8 deletions.
28 changes: 28 additions & 0 deletions src/Handler/MethodNameInflector/ClassNameInflector.php
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);
}
}
11 changes: 3 additions & 8 deletions src/Handler/MethodNameInflector/HandleClassNameInflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,15 @@
* - \MyGlobalCommand => $handler->handleMyGlobalCommand()
* - \My\App\TaskCompletedCommand => $handler->handleTaskCompletedCommand()
*/
class HandleClassNameInflector implements MethodNameInflector
class HandleClassNameInflector extends ClassNameInflector
{
/**
* {@inheritdoc}
*/
public function inflect($command, $commandHandler)
{
$commandName = get_class($command);
$commandName = parent::inflect($command, $commandHandler);

// If class name has a namespace separator, only take last portion
if (strpos($commandName, '\\') !== false) {
$commandName = substr($commandName, strrpos($commandName, '\\') + 1);
}

return 'handle' . $commandName;
return 'handle' . ucfirst($commandName);
}
}
48 changes: 48 additions & 0 deletions tests/Handler/MethodNameInflector/ClassNameInflectorTest.php
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)
);
}
}

0 comments on commit 112a6bc

Please sign in to comment.