Skip to content

Commit

Permalink
Added fragment method to paginator.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 26, 2013
1 parent e99504e commit ea9a924
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Illuminate/Foundation/changes.json
Expand Up @@ -62,7 +62,8 @@
{"message": "Added automatic separation of read / write connections into database layer.", "backport": null},
{"message": "Added automatic failed job handling for all queue drivers. New --tries switch for queue:listen and queue:work.", "backport": null},
{"message": "Cache:add now returns true when the value is actually added. False is returned otherwise.", "backport": null},
{"message": "Added merge, diff, and intersect to the Collection class.", "backport": null}
{"message": "Added merge, diff, and intersect to the Collection class.", "backport": null},
{"message": "Added fragment method to paginator.", "backport": null}
],
"4.0.x": [
{"message": "Added implode method to query builder and Collection class.", "backport": null},
Expand Down
34 changes: 33 additions & 1 deletion src/Illuminate/Pagination/Paginator.php
Expand Up @@ -73,6 +73,13 @@ class Paginator implements ArrayableInterface, ArrayAccess, Countable, IteratorA
*/
protected $query = array();

/**
* The fragment to be appended to all URLs.
*
* @var string
*/
protected $fragment;

/**
* Create a new Paginator instance.
*
Expand Down Expand Up @@ -191,7 +198,32 @@ public function getUrl($page)
$parameters = array_merge($parameters, $this->query);
}

return $this->env->getCurrentUrl().'?'.http_build_query($parameters, null, '&');
$fragment = $this->buildFragment();

return $this->env->getCurrentUrl().'?'.http_build_query($parameters, null, '&').$fragment;
}

/**
* Get / set the URL fragment to be appended to URLs.
*
* @param string|null $fragment
* @return \Illuminate\Pagination\Paginator|string
*/
public function fragment($fragment = null)
{
if (is_null($fragment)) return $this->fragment;

$this->fragment = $fragment; return $this;
}

/**
* Build the full fragment portion of a URL.
*
* @return string
*/
protected function buildFragment()
{
return $this->fragment ? '#'.$this->fragment : '';
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/Pagination/PaginationPaginatorTest.php
Expand Up @@ -132,4 +132,18 @@ public function testPaginatorIsIterable()
$this->assertEquals(array('foo', 'bar', 'baz'), $p->getIterator()->getArrayCopy());
}


public function testGetUrlAddsFragment()
{
$p = new Paginator($env = m::mock('Illuminate\Pagination\Environment'), array('foo', 'bar', 'baz'), 3, 2);
$env->shouldReceive('getCurrentUrl')->twice()->andReturn('http://foo.com');
$env->shouldReceive('getPageName')->twice()->andReturn('page');

$p->fragment("a-fragment");

$this->assertEquals('http://foo.com?page=1#a-fragment', $p->getUrl(1));
$p->addQuery('foo', 'bar');
$this->assertEquals('http://foo.com?page=1&foo=bar#a-fragment', $p->getUrl(1));
}

}

0 comments on commit ea9a924

Please sign in to comment.