Skip to content

Commit

Permalink
Change urls params interpolation syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
evaisse committed Jul 21, 2015
1 parent 36465c5 commit ccfc9bf
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@ Get the simple API
// handle errors
}

Easy routing
Easy routing with replacement syntax (similar to php frameworks like symfony2/laravel)

$data = $http->POST('http://httpbin.org/status/:code', array(
$data = $http->GET('http://httpbin.org/status/{code}', array(
"code" => 200
));
// will call http://httpbin.org/status/200

$data = $http->POST('http://httpbin.org/status/:code', array(
$data = $http->GET('http://httpbin.org/status/{code}', array(
"code" => 200,
"foo" => "bar"
));
// will call http://httpbin.org/status/200?foo=bar





Complete API
Expand Down
5 changes: 4 additions & 1 deletion Service/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,15 @@ public function prepare($method, $url, $parameters = array(), $cookies = array()
*/
public function transformUrl($urlPattern, array $parameters = array())
{
if (empty($parameters) || !preg_match('/:[a-z]/', $urlPattern)) {
if (empty($parameters)
|| !preg_match('/\{[a-z+][a-z0-9_]+\}/i', $urlPattern)
) {
return array($urlPattern, $parameters); // no need to transform plain uri
}

foreach ($parameters as $key => $value) {
$urlPattern = str_replace(":$key", $value, $urlPattern);
$urlPattern = str_replace('{' . $key . '}', $value, $urlPattern);
unset($parameters[$key]);
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/Unit/ExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function testResultWithClientErrorException($code)
{
list($helper, $httpKernel, $container) = $this->createContext();

$stmt = $helper->prepare('GET', 'http://httpbin.org/status/:code', array(
$stmt = $helper->prepare('GET', 'http://httpbin.org/status/{code}', array(
'code' => $code,
));

Expand All @@ -111,7 +111,7 @@ public function testResultWithServerErrorException($code)
{
list($helper, $httpKernel, $container) = $this->createContext();

$stmt = $helper->prepare('GET', 'http://httpbin.org/status/:code', array(
$stmt = $helper->prepare('GET', 'http://httpbin.org/status/{code}', array(
'code' => $code,
));

Expand All @@ -133,7 +133,7 @@ public function testExpectedInstancesOfExceptions($code, $cls)
{
list($helper, $httpKernel, $container) = $this->createContext();

$stmt = $helper->prepare('GET', 'http://httpbin.org/status/:code', array(
$stmt = $helper->prepare('GET', 'http://httpbin.org/status/{code}', array(
'code' => $code,
));

Expand Down
10 changes: 5 additions & 5 deletions Tests/Unit/PromisesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testPromises($code, $expectedResults)
{
list($helper, $httpKernel, $container) = $this->createContext();

$stmt = $helper->prepare("GET", 'https://httpbin.org/status/:code', array(
$stmt = $helper->prepare("GET", 'https://httpbin.org/status/{code}', array(
'code' => $code
));

Expand Down Expand Up @@ -67,7 +67,7 @@ public function testProxiedPromises($code, $expectedResults)
{
list($helper, $httpKernel, $container) = $this->createContext();

$stmt = $helper->prepare("GET", 'https://httpbin.org/status/:code', array(
$stmt = $helper->prepare("GET", 'https://httpbin.org/status/{code}', array(
'code' => $code
));

Expand Down Expand Up @@ -136,7 +136,7 @@ public function testPromisesValues($code, $events)
{
list($helper, $httpKernel, $container) = $this->createContext();

$stmt = $helper->prepare("GET", 'https://httpbin.org/status/:code', [
$stmt = $helper->prepare("GET", 'https://httpbin.org/status/{code}', [
'code' => $code,
]);

Expand Down Expand Up @@ -174,7 +174,7 @@ public function testPromisesAfterwards($code, $events)
{
list($helper, $httpKernel, $container) = $this->createContext();

$stmt = $helper->prepare("GET", 'https://httpbin.org/status/:code', [
$stmt = $helper->prepare("GET", 'https://httpbin.org/status/{code}', [
'code' => $code,
]);

Expand Down Expand Up @@ -214,7 +214,7 @@ public function testAllPromisesFulfillsMultipleTimesAfterwards($code, $events)
{
list($helper, $httpKernel, $container) = $this->createContext();

$stmt = $helper->prepare("GET", 'https://httpbin.org/status/:code', [
$stmt = $helper->prepare("GET", 'https://httpbin.org/status/{code}', [
'code' => $code,
]);

Expand Down
3 changes: 2 additions & 1 deletion Tests/Unit/SimpleRoutingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testCallsWithRouteArguments($code)
"another" => __FILE__
];

$statement = $helper->prepare("GET", 'http://httpbin.org/status/:code', $args);
$statement = $helper->prepare("GET", 'http://httpbin.org/status/{code}', $args);
$request = $statement->getRequest();

$statement->execute($httpKernel);
Expand All @@ -45,4 +45,5 @@ public function testCallsWithRouteArguments($code)

}


}

0 comments on commit ccfc9bf

Please sign in to comment.