Skip to content

Commit

Permalink
Refined Paginator interface, setting optional arguments is now
Browse files Browse the repository at this point in the history
done via setter methods.

Add options definition to README.
  • Loading branch information
janl committed Jul 6, 2011
1 parent 8566af8 commit 47e078f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 37 deletions.
4 changes: 2 additions & 2 deletions Couchbase/View.php
Expand Up @@ -55,9 +55,9 @@ function getResultByRange($start, $end = null, $options = array())
return $this->getResult(array_merge($options, $key_options));
}

function getResultPaginator($rowsPerPage = 10, $pageKey = null, $options = array())
function getResultPaginator()
{
return new Couchbase_ViewResultPaginator($this, $rowsPerPage, $pageKey, $options);
return new Couchbase_ViewResultPaginator($this);
}

function setMapFunction($code)
Expand Down
11 changes: 10 additions & 1 deletion Couchbase/ViewResultPaginator.php
Expand Up @@ -13,11 +13,20 @@ class Couchbase_ViewResultPaginator implements Iterator
var $page_key = null;
var $options = array();

function __construct($view, $rowsPerPage = 10, $pageKey = null, $options = array())
function __construct($view)
{
$this->view = $view;
}

function setRowsPerPage($rowsPerPage) {
$this->rowsPerPage = $rowsPerPage;
}

function setPageKey($pageKey) {
$this->page_key = $pageKey;
}

function setOptions($options) {
$this->options = $options;
}

Expand Down
24 changes: 20 additions & 4 deletions README.md
Expand Up @@ -104,9 +104,8 @@ access your view result in pages, or spread over multiple pages.

<?php
// setup skipped
define("ROWS_PER_PAGE", 10);
$view = $cb->getView("designdoc_Name", "by_name");
$resultPages = $view->getResultPaginator(ROWS_PER_PAGE);
$resultPages = $view->getResultPaginator();
foreach($resultPages AS $page) {
// $page is a Couchbase_ViewResult instance
}
Expand All @@ -116,11 +115,12 @@ keep a `$page_key` around:

<?php
// setup skipped
define("ROWS_PER_PAGE", 10);
// safer version of $_GET["page_key"];
$page_key = filter_input(INPUT_GET, "page_key", FILTER_SANITIZE_URL);

$view = $cb->getView("designdoc_Name", "by_name");
$resultPages = $view->getResultPaginato(ROWS_PER_PAGE, $pageKey);
$resultPages = $view->getResultPaginator();
$resultPages->setPageKey($page_key);
$currentPage = $resultPages->current();


Expand Down Expand Up @@ -224,3 +224,19 @@ definition exists just for documentation purposes.
class Couchbase_ViewResultPaginator implements Iterator
{
}

### `Couchbase_ViewQueryOptions`

Note that this isn't an actual class, this just declares what query options
can be used:

Couchbase_QueryOptions $options = array(
"limit" => false,
"skip" => 0,
"descending" => false,
"stale" => false,
"group" => false,
"group_level" => 0,
"reduce" => true,
"inclusive_end" => false // only valid in getResultByRange
);
44 changes: 14 additions & 30 deletions test/CouchbaseTest.php
Expand Up @@ -80,29 +80,6 @@ function test_basic_query()
$view = $this->cb->getView("default", "name");
$this->assertInstanceOf("Couchbase_View", $view);

// Couchbase_QueryOptions $options = array(
// "limit" => false,
// "skip" => 0,
// "descending" => false,
// "stale" => false,
// "group" => false,
// "group_level" => 0,
// "reduce" => true,
// "inclusive_end" => false // only valid in getResultByRange
// );

// key options
// $start = "key";
// $start = array("key", "docid");
// $end = "key";
// $end = array("key", "docid");

// $result = $view->getResult([$options]);
// $result = $view->getResultByKey($key, [$options]);
// $result = $view->getResultRange($start, $end, [$options]);
// $result = $query->getResultPage([$pagekey = null]);


$result = $view->getResult();
$this->assertInstanceOf("Couchbase_ViewResult", $result);
$this->assertEquals(3, count($result->rows));
Expand Down Expand Up @@ -259,14 +236,15 @@ function test_result_page()
$this->prepare_ddoc();

$view = $this->cb->getView("default", "name");
$rowsPerPage = 2;
$resultPages = $view->getResultPaginator($rowsPerPage);
$resultPages = $view->getResultPaginator();
$resultPages->setRowsPerPage(2);
$this->assertInstanceOf("Couchbase_ViewResultPaginator", $resultPages);
foreach($resultPages AS $resultPage) {
$this->assertInstanceOf("Couchbase_ViewResult", $resultPage);
}

$resultPages = $view->getResultPaginator($rowsPerPage);
$resultPages = $view->getResultPaginator();
$resultPages->setRowsPerPage(2);
$firstPage = $resultPages->next();
$this->assertEquals(2, count($firstPage->rows));
$this->assertEquals("Ben", $firstPage->rows[0]->key);
Expand All @@ -283,12 +261,15 @@ function test_result_page_explicit_pages()
$this->prepare_ddoc();

$view = $this->cb->getView("default", "name");
$rowsPerPage = 2;
$resultPages = $view->getResultPaginator($rowsPerPage);
$resultPages = $view->getResultPaginator();
$resultPages->setRowsPerPage(2);
$resultPages->next();
$pageKey = $resultPages->key();

$resultPages = $view->getResultPaginator($rowsPerPage, $pageKey);
$resultPages = $view->getResultPaginator();
$resultPages->setRowsPerPage(2);
$resultPages->setPageKey($pageKey);

$secondPage = $resultPages->current();
$this->assertEquals(1, count($secondPage->rows));
$this->assertEquals("Simon", $secondPage->rows[0]->key);
Expand All @@ -302,7 +283,10 @@ function test_result_page_with_descending_option()
$view = $this->cb->getView("default", "name");
$rowsPerPage = 2;

$resultPages = $view->getResultPaginator($rowsPerPage, null, array("descending" => true));
$resultPages = $view->getResultPaginator();
$resultPages->setRowsPerPage(2);
$resultPages->setOptions(array("descending" => true));

$firstPage = $resultPages->next();
$this->assertEquals(2, count($firstPage->rows));
$this->assertEquals("Simon", $firstPage->rows[0]->key);
Expand Down

0 comments on commit 47e078f

Please sign in to comment.