Skip to content

Commit f6a3a5a

Browse files
committed
Add class constants for required arguments for view operations
1 parent 3707804 commit f6a3a5a

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/PHPCouchDB/Database.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Database
1919

2020
const OPTION_INCLUDE_DOCS = 'include_docs';
2121
const OPTION_REDUCE = 'reduce';
22+
const OPTION_DDOC = 'ddoc';
23+
const OPTION_VIEW = 'view';
2224

2325
/**
2426
* Constructor for the Database object - this is usually called by
@@ -179,25 +181,25 @@ public function getDocById($id) : Document
179181
public function getView($options = []) : array
180182
{
181183
// check we have ddoc and view name
182-
if (!isset($options['ddoc'])) {
184+
if (!isset($options[self::OPTION_DDOC])) {
183185
throw new Exception\ServerException(
184186
'ddoc is a required parameter for getView'
185187
);
186188
}
187-
if (!isset($options['view'])) {
189+
if (!isset($options[self::OPTION_VIEW])) {
188190
throw new Exception\ServerException(
189191
'view is a required parameter for getView'
190192
);
191193
}
192194

193-
$endpoint = "/" . $this->db_name . "/_design/" . $options['ddoc']
194-
. "/_view/" . $options['view'];
195+
$endpoint = "/" . $this->db_name . "/_design/" . $options[self::OPTION_DDOC]
196+
. "/_view/" . $options[self::OPTION_VIEW];
195197

196198
// grab extra params
197199
$query = [];
198200
foreach ($options as $key => $value) {
199201
// skip the values we need for the URL, pass the rest through
200-
if (!in_array($key, ["ddoc", "view"])) {
202+
if (!in_array($key, [self::OPTION_DDOC, self::OPTION_VIEW])) {
201203
$query[$key] = $value;
202204
}
203205
}

tests/PHPCouchDB/DatabaseTest.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ public function testView() {
180180
// userland code starts
181181
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
182182
$database = $server->useDB([\PHPCouchDB\Server::OPTION_NAME => "egdb"]);
183-
$docs = $database->getView(["ddoc" => "myview", "view" => "year", "group" => true]);
183+
$docs = $database->getView([
184+
\PHPCouchDB\Database::OPTION_DDOC => "myview",
185+
\PHPCouchDB\Database::OPTION_VIEW => "year",
186+
"group" => true
187+
]);
184188

185189
$this->assertInternalType('array', $docs);
186190
$this->assertEquals(3, count($docs));
@@ -203,7 +207,13 @@ public function testViewWithIncludeDocs() {
203207
// userland code starts
204208
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
205209
$database = $server->useDB([\PHPCouchDB\Server::OPTION_NAME => "egdb"]);
206-
$docs = $database->getView(["ddoc" => "myview", "view" => "year", "reduce" => false, "limit" => 3, "include_docs" => true]);
210+
$docs = $database->getView([
211+
\PHPCouchDB\Database::OPTION_DDOC => "myview",
212+
\PHPCouchDB\Database::OPTION_VIEW => "year",
213+
"reduce" => false,
214+
"limit" => 3,
215+
\PHPCouchDB\Database::OPTION_INCLUDE_DOCS => true
216+
]);
207217

208218
$this->assertInternalType('array', $docs);
209219
$this->assertEquals(3, count($docs));

0 commit comments

Comments
 (0)