Permalink
Browse files

Added fragment method to paginator.

  • Loading branch information...
taylorotwell committed Nov 26, 2013
1 parent e99504e commit ea9a92483479155b393462f2a2bcbb4a2c246fff
@@ -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},
@@ -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.
*
@@ -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 : '';
}
/**
@@ -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.