Skip to content

Commit 7d01efc

Browse files
author
Niels Theen
committed
Refactor ':' parameter to strategy class
1 parent 640922b commit 7d01efc

File tree

4 files changed

+89
-11
lines changed

4 files changed

+89
-11
lines changed

src/Paramaters/Label/Colon.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
namespace Legionth\React\Http\Rest\Paramaters\Label;
3+
4+
class Colon implements Strategy
5+
{
6+
const IDENTIFIER = ':';
7+
8+
public function extractParametersFromPath(array $pathAsArray, array $requestPathArray) : array
9+
{
10+
$result = array();
11+
foreach ($pathAsArray as $id => $valueName) {
12+
$position = strpos($valueName, self::IDENTIFIER);
13+
if (0 === $position) {
14+
$valueName = substr($valueName, 1);
15+
$result[$valueName] = $requestPathArray[$id];
16+
}
17+
}
18+
19+
return $result;
20+
}
21+
22+
public function getFirstIdentifier() : string
23+
{
24+
return self::IDENTIFIER;
25+
}
26+
}

src/Paramaters/Label/Strategy.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Legionth\React\Http\Rest\Paramaters\Label;
4+
5+
interface Strategy
6+
{
7+
public function extractParametersFromPath(array $pathAsArray, array $requestPathArray) : array;
8+
9+
public function getFirstIdentifier() : string;
10+
}

src/Server.php

+22-11
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,45 @@
22

33
namespace Legionth\React\Http\Rest;
44

5+
use Legionth\React\Http\Rest\Paramaters\Label\Colon;
6+
use Legionth\React\Http\Rest\Paramaters\Label\Strategy;
57
use Psr\Http\Message\RequestInterface;
68
use Psr\Http\Message\ServerRequestInterface;
79
use React\Socket\ServerInterface;
810
use RingCentral\Psr7\Response;
911

1012
class Server
1113
{
14+
15+
const DOUBLE_POINT = ':';
16+
const CURLY_BRACKETS = '{}';
17+
const SQUARE_BRACKETS = '[]';
18+
1219
/**
1320
* @var array
1421
*/
1522
private $functions;
1623

24+
/**
25+
* @var Strategy
26+
*/
27+
private $strategy;
28+
1729
/**
1830
* @param ServerInterface $socket
1931
* @param callable|null $callback
32+
* @param string $strategy
2033
*/
21-
public function listen(ServerInterface $socket, callable $callback = null)
34+
public function listen(ServerInterface $socket, callable $callback = null, Strategy $strategy = null)
2235
{
2336
$middleWareFunctions = array();
2437

38+
if ($strategy === null) {
39+
$strategy = new Colon();
40+
}
41+
42+
$this->strategy = $strategy;
43+
2544
foreach ($this->functions as $httpMethod => $pathFunctionArray) {
2645
foreach ($pathFunctionArray as $path => $function) {
2746
$middleWareFunctions[] = $this->createRestfulFunction($httpMethod, $path, $function);
@@ -99,19 +118,11 @@ private function createRestfulFunction($httpMethod, $path, $function)
99118
return $function($request, $next);
100119
}
101120

102-
if (false === strpos($path, ':')) {
121+
if (false === strpos($path, $this->strategy->getFirstIdentifier())) {
103122
return $next($request);
104123
}
105124

106-
$argument = array();
107-
108-
foreach ($pathArray as $id => $valueName) {
109-
$position = strpos($valueName, ':');
110-
if (0 === $position) {
111-
$valueName = substr($valueName, 1);
112-
$argument[$valueName] = $requestPathArray[$id];
113-
}
114-
}
125+
$argument = $this->strategy->extractParametersFromPath($pathArray, $requestPathArray);
115126

116127
return $function($request, $next, $argument);
117128
}

tests/ColonStrategyTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Tests\Legionth\React\Http\Rest;
4+
5+
6+
use Legionth\React\Http\Rest\Paramaters\Label\Colon;
7+
8+
class ColonStrategyTest extends TestCase
9+
{
10+
public function testColonWillBeExtracted()
11+
{
12+
$colonStrategy = new Colon();
13+
$pathArray = array('say', ':word');
14+
$requestArray = array('say', 'hello');
15+
16+
$result = $colonStrategy->extractParametersFromPath($pathArray, $requestArray);
17+
18+
$this->assertEquals(array('word' => 'hello'), $result);
19+
}
20+
21+
public function testNothingWillBeExtracted()
22+
{
23+
$colonStrategy = new Colon();
24+
$pathArray = array('say', 'word');
25+
$requestArray = array('say', 'hello');
26+
27+
$result = $colonStrategy->extractParametersFromPath($pathArray, $requestArray);
28+
29+
$this->assertEquals(array(), $result);
30+
}
31+
}

0 commit comments

Comments
 (0)