Skip to content

Commit

Permalink
add option for arguments transformation method #11
Browse files Browse the repository at this point in the history
  • Loading branch information
gregor-j committed Jul 10, 2024
1 parent 8c2baae commit 6c276ba
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/JsonRPC/ProcedureHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ class ProcedureHandler
*/
protected $beforeMethodName = '';

/**
* Procedure arguments transformation method to call
*
* @var string
*/
protected $argsTransformMethod = '';

/**
* Register a new procedure
*
Expand Down Expand Up @@ -107,6 +114,17 @@ public function withBeforeMethod($methodName)
return $this;
}

/**
* Procedure arguments transformation method to call.
* @param $methodName
* @return $this
*/
public function withArgsTransformMethod($methodName)
{
$this->argsTransformMethod = $methodName;
return $this;
}

/**
* Register multiple procedures from array
*
Expand Down Expand Up @@ -218,6 +236,12 @@ public function executeMethod($class, $method, $params)
$reflection->getNumberOfParameters()
);

/**
* Execute procedure arguments transformation method before invoking
* the procedure.
*/
$arguments = $this->executeArgsTransformMethod($instance, $method, $arguments);

return $reflection->invokeArgs($instance, $arguments);
}

Expand All @@ -234,6 +258,25 @@ public function executeBeforeMethod($object, $method)
}
}

/**
* Execute procedure arguments transformation method before invoking
* the procedure.
* @param mixed $object
* @param string $method
* @param array<int|string, mixed> $arguments
* @return array<int|string, mixed>
*/
public function executeArgsTransformMethod($object, $method, $arguments)
{
if ($this->argsTransformMethod !== '' && method_exists($object, $this->argsTransformMethod)) {
$arguments = call_user_func_array([$object, $this->argsTransformMethod], [$method, $arguments]);
if ($arguments === false) {
throw new BadFunctionCallException('Unable to transform procedure arguments!');
}
}
return $arguments;
}

/**
* Get procedure arguments
*
Expand Down
25 changes: 25 additions & 0 deletions tests/ProcedureHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ public function myProcedure()
}
}

class ClassWithArgsTransformMethod
{
public static string $foobar = '';

public function transform($procedure, $args): array
{
$args[0] .= self::$foobar;
return $args;
}

public function getAll($arg1)
{
return $arg1;
}
}

class ProcedureHandlerTest extends TestCase
{
public function testProcedureNotFound()
Expand Down Expand Up @@ -161,4 +177,13 @@ public function testBeforeMethod()
$handler->withBeforeMethod('before');
$this->assertEquals('myProcedure', $handler->executeProcedure('myProcedure'));
}

public function testArgTranformMethod()
{
$handler = new ProcedureHandler();
ClassWithArgsTransformMethod::$foobar = 'o';
$handler->withObject(new ClassWithArgsTransformMethod());
$handler->withArgsTransformMethod('transform');
$this->assertEquals('hello', $handler->executeProcedure('getAll', ['hell']));
}
}

0 comments on commit 6c276ba

Please sign in to comment.