Skip to content

Commit 05a5dd1

Browse files
author
Niels Theen
committed
Add two new strategies to support different strategies
1 parent 7d01efc commit 05a5dd1

6 files changed

+246
-0
lines changed
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Legionth\React\Http\Rest\Paramaters\Label;
4+
5+
class CurlyBrackets implements Strategy
6+
{
7+
8+
const PREFIX = '{';
9+
10+
const SUFFIX = '}';
11+
12+
/**
13+
* @var ExtractBetweenTwoCharacters
14+
*/
15+
private $extractor;
16+
17+
/**
18+
* @param ExtractBetweenTwoCharacters|null $extractor
19+
*/
20+
public function __construct(ExtractBetweenTwoCharacters $extractor = null)
21+
{
22+
if (null === $extractor) {
23+
$extractor = new ExtractBetweenTwoCharacters();
24+
}
25+
$this->extractor = $extractor;
26+
}
27+
28+
/**
29+
* @param array $pathAsArray
30+
* @param array $requestPathArray
31+
* @return array
32+
*/
33+
public function extractParametersFromPath(array $pathAsArray, array $requestPathArray) : array
34+
{
35+
$result = array();
36+
foreach ($pathAsArray as $id => $valueName) {
37+
$extractedName = $this->extractor->extract($valueName, self::PREFIX, self::SUFFIX);
38+
39+
if ('' !== $extractedName) {
40+
$result[$extractedName] = $requestPathArray[$id];
41+
}
42+
}
43+
44+
return $result;
45+
}
46+
47+
/**
48+
* @return string
49+
*/
50+
public function getFirstIdentifier() : string
51+
{
52+
return self::PREFIX;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Legionth\React\Http\Rest\Paramaters\Label;
4+
5+
class ExtractBetweenTwoCharacters
6+
{
7+
public function extract(string $value, string $firstCharacter, string $secondCharacter) : string
8+
{
9+
$start = strpos($value, $firstCharacter);
10+
if (false === $start || 0 !== $start) {
11+
return '';
12+
}
13+
14+
$end = strpos($value, $secondCharacter, $start + 1);
15+
if (false === $end || $end < $start) {
16+
return '';
17+
}
18+
19+
$length = $end - $start;
20+
$extractedName = substr($value, $start + 1, $length - 1);
21+
22+
return $extractedName;
23+
}
24+
}
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Legionth\React\Http\Rest\Paramaters\Label;
4+
5+
class SquareBrackets implements Strategy
6+
{
7+
const PREFIX = '[';
8+
9+
const SUFFIX = ']';
10+
11+
/**
12+
* @var ExtractBetweenTwoCharacters
13+
*/
14+
private $extractor;
15+
16+
/**
17+
* @param ExtractBetweenTwoCharacters|null $extractor
18+
*/
19+
public function __construct(ExtractBetweenTwoCharacters $extractor = null)
20+
{
21+
if (null === $extractor) {
22+
$extractor = new ExtractBetweenTwoCharacters();
23+
}
24+
$this->extractor = $extractor;
25+
}
26+
27+
/**
28+
* @param array $pathAsArray
29+
* @param array $requestPathArray
30+
* @return array
31+
*/
32+
public function extractParametersFromPath(array $pathAsArray, array $requestPathArray) : array
33+
{
34+
$result = array();
35+
foreach ($pathAsArray as $id => $valueName) {
36+
$extractedName = $this->extractor->extract($valueName, self::PREFIX, self::SUFFIX);
37+
38+
if ('' !== $extractedName) {
39+
$result[$extractedName] = $requestPathArray[$id];
40+
}
41+
}
42+
43+
return $result;
44+
}
45+
46+
/**
47+
* @return string
48+
*/
49+
public function getFirstIdentifier() : string
50+
{
51+
return self::PREFIX;
52+
}
53+
}

tests/CurlyBracketsStrategyTest.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Tests\Legionth\React\Http\Rest;
4+
5+
6+
use Legionth\React\Http\Rest\Paramaters\Label\CurlyBrackets;
7+
8+
class CurlyBracketsStrategyTest extends TestCase
9+
{
10+
public function testValueWillBeExtracted()
11+
{
12+
$colonStrategy = new CurlyBrackets();
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 CurlyBrackets();
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+
32+
public function testNothingWillBeExtractedBecauseBracketIsNotComplete()
33+
{
34+
$colonStrategy = new CurlyBrackets();
35+
$pathArray = array('say', '{word');
36+
$requestArray = array('say', 'hello');
37+
38+
$result = $colonStrategy->extractParametersFromPath($pathArray, $requestArray);
39+
40+
$this->assertEquals(array(), $result);
41+
}
42+
}

tests/ServerTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\Legionth\React\Http\Rest;
44

5+
use Legionth\React\Http\Rest\Paramaters\Label\CurlyBrackets;
56
use Legionth\React\Http\Rest\Server;
67
use Psr\Http\Message\ServerRequestInterface;
78

@@ -270,4 +271,34 @@ public function testNoSuchCallDefinedResultInNeverCalled()
270271

271272
$this->assertSame(null, $requestAssertion);
272273
}
274+
275+
public function testApiWithCurlyBracketsCanExtractParamters()
276+
{
277+
$requestAssertion = null;
278+
$idAssertion = null;
279+
$nameAssertion = null;
280+
281+
$server = new Server();
282+
283+
$server->put('/user/add/{id}/group/{name}', function (ServerRequestInterface $request, callable $next, array $parameters) use (&$requestAssertion, &$idAssertion, &$nameAssertion) {
284+
$requestAssertion = $request;
285+
$idAssertion = $parameters['id'];
286+
$nameAssertion = $parameters['name'];
287+
});
288+
289+
$server->listen($this->socket, null, new CurlyBrackets());
290+
291+
$this->socket->emit('connection', array($this->connection));
292+
$this->connection->emit('data', array("PUT http://example.com/user/add/10/group/reactphp HTTP/1.0\r\n\r\n"));
293+
294+
$this->assertInstanceOf('RingCentral\Psr7\Request', $requestAssertion);
295+
$this->assertSame('PUT', $requestAssertion->getMethod());
296+
$this->assertSame('http://example.com/user/add/10/group/reactphp', $requestAssertion->getRequestTarget());
297+
$this->assertEquals('http://example.com/user/add/10/group/reactphp', $requestAssertion->getUri());
298+
$this->assertSame('/user/add/10/group/reactphp', $requestAssertion->getUri()->getPath());
299+
$this->assertSame('example.com', $requestAssertion->getHeaderLine('Host'));
300+
301+
$this->assertEquals('10', $idAssertion);
302+
$this->assertEquals('reactphp', $nameAssertion);
303+
}
273304
}

tests/SquareBracketsStrategyTest.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Tests\Legionth\React\Http\Rest;
4+
5+
6+
use Legionth\React\Http\Rest\Paramaters\Label\SquareBrackets;
7+
8+
class SquareBracketsStrategyTest extends TestCase
9+
{
10+
public function testColonWillBeExtracted()
11+
{
12+
$colonStrategy = new SquareBrackets();
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 SquareBrackets();
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+
32+
public function testNothingWillBeExtractedBecauseBracketIsNotComplete()
33+
{
34+
$colonStrategy = new CurlyBrackets();
35+
$pathArray = array('say', '[word');
36+
$requestArray = array('say', 'hello');
37+
38+
$result = $colonStrategy->extractParametersFromPath($pathArray, $requestArray);
39+
40+
$this->assertEquals(array(), $result);
41+
}
42+
}

0 commit comments

Comments
 (0)