Skip to content

Commit

Permalink
Improved offsetPath support to handle negative offsets and limits
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Mar 2, 2015
1 parent c1e8df8 commit bf566b7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
9 changes: 2 additions & 7 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,8 @@ public function path($depth = null)
*/
public function offsetPath($offset = 0, $limit = null)
{
$path = $this->path($limit === null ? null : $offset + $limit);
$parts = explode("/", $path);
for($i = 0; $i <= $offset; $i++)
{
array_shift($parts);
}
return '/' . implode('/', $parts);
$parts = explode("/", substr($this->path(), 1));
return '/' . implode('/', array_slice($parts, $offset, $limit));
}

/**
Expand Down
37 changes: 34 additions & 3 deletions tests/Cubex/Http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,40 @@ public function testPaths($path, $expect, $depth = 1, $offset = -1)
public function pathProvider()
{
return [
['/hello/world', '/hello', 1],
['/hello/world', '/hello/world', 2],
['/hello/world', '/world', 1, 1],
['/hello/world/hi', '/hello', 1],
['/hello/world/hi', '/hello/world', 2],
['/hello/world/hi', '/hello/world/hi', 3],
];
}

/**
* @dataProvider offsetPathProvider
*
* @param $path
* @param $expect
* @param int $offset
* @param int $limit
*/
public function testOffsetPaths($path, $expect, $offset = 0, $limit = null)
{
$request = Request::createFromGlobals();
/**
* @var \Cubex\Http\Request $request
*/
$request->server->set('REQUEST_URI', $path);
$this->assertEquals($expect, $request->offsetPath($offset, $limit));
}

public function offsetPathProvider()
{
$path = '/hello/world/how/are/you';
return [
[$path, '/hello', 0, 1],
[$path, '/world', 1, 1],
[$path, '/you', -1],
[$path, '/are/you', -2],
[$path, '/hello/world/how', 0, -2],
[$path, '/world/how', 1, -2],
];
}

Expand Down

0 comments on commit bf566b7

Please sign in to comment.