Skip to content

Commit 4fa91f6

Browse files
committed
Merge branch 'class-constants'
2 parents 1f06178 + f6a3a5a commit 4fa91f6

File tree

5 files changed

+106
-70
lines changed

5 files changed

+106
-70
lines changed

src/PHPCouchDB/Database.php

+25-15
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class Database
1717
protected $client;
1818
protected $db_name;
1919

20+
const OPTION_INCLUDE_DOCS = 'include_docs';
21+
const OPTION_REDUCE = 'reduce';
22+
const OPTION_DDOC = 'ddoc';
23+
const OPTION_VIEW = 'view';
2024

2125
/**
2226
* Constructor for the Database object - this is usually called by
@@ -77,11 +81,13 @@ public function getAllDocs($options = []) : array
7781
$query = $options;
7882

7983
// convert data and set some defaults
80-
if (isset($query['include_docs'])) {
81-
$query['include_docs'] = $this->boolToString($query['include_docs']);
84+
if (isset($query[self::OPTION_INCLUDE_DOCS])) {
85+
$query[self::OPTION_INCLUDE_DOCS] = $this->boolToString(
86+
$query[self::OPTION_INCLUDE_DOCS]
87+
);
8288
} else {
8389
// needs to be a string and this is our chosen default value
84-
$query['include_docs'] = "true";
90+
$query[self::OPTION_INCLUDE_DOCS] = "true";
8591
}
8692

8793
$response = $this->client->request("GET", $endpoint, ["query" => $query]);
@@ -167,50 +173,54 @@ public function getDocById($id) : Document
167173
/**
168174
* Get data from a view, either docs or grouped data
169175
*
170-
* @param array $options Must include `ddoc` and `view`, also suppoerts any
176+
* @param array $options Must include `ddoc` and `view`, also supports any
171177
* other query parameters that should be passed to the view (e.g. limit)
172178
* @return array If there are documents, an array of \PHPCouchDB\Document
173179
* objects, otherwise an array as appropriate
174180
*/
175181
public function getView($options = []) : array
176182
{
177183
// check we have ddoc and view name
178-
if (!isset($options['ddoc'])) {
184+
if (!isset($options[self::OPTION_DDOC])) {
179185
throw new Exception\ServerException(
180186
'ddoc is a required parameter for getView'
181187
);
182188
}
183-
if (!isset($options['view'])) {
189+
if (!isset($options[self::OPTION_VIEW])) {
184190
throw new Exception\ServerException(
185191
'view is a required parameter for getView'
186192
);
187193
}
188194

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

192198
// grab extra params
193199
$query = [];
194200
foreach ($options as $key => $value) {
195201
// skip the values we need for the URL, pass the rest through
196-
if (!in_array($key, ["ddoc", "view"])) {
202+
if (!in_array($key, [self::OPTION_DDOC, self::OPTION_VIEW])) {
197203
$query[$key] = $value;
198204
}
199205
}
200206

201207
// convert data and set some defaults
202-
if (isset($query['include_docs'])) {
203-
$query['include_docs'] = $this->boolToString($query['include_docs']);
208+
if (isset($query[self::OPTION_INCLUDE_DOCS])) {
209+
$query[self::OPTION_INCLUDE_DOCS] = $this->boolToString(
210+
$query[self::OPTION_INCLUDE_DOCS]
211+
);
204212
} else {
205213
// needs to be a string and this is our chosen default value
206-
$query['include_docs'] = "false";
214+
$query[self::OPTION_INCLUDE_DOCS] = "true";
207215
}
208216

209-
if (isset($query['reduce'])) {
210-
$query['reduce'] = $this->boolToString($query['reduce']);
217+
if (isset($query[self::OPTION_REDUCE])) {
218+
$query[self::OPTION_REDUCE] = $this->boolToString(
219+
$query[self::OPTION_REDUCE]
220+
);
211221
} else {
212222
// needs to be a string and this is our chosen default value
213-
$query['reduce'] = "true";
223+
$query[self::OPTION_REDUCE] = "true";
214224
}
215225

216226
$response = $this->client->request("GET", $endpoint, ["query" => $query]);

src/PHPCouchDB/Server.php

+19-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class Server
1818
{
1919
protected $client;
2020

21+
const OPTION_CLIENT = 'client';
22+
const OPTION_URL = 'url';
23+
const OPTION_NAME = 'name';
24+
const OPTION_CREATE_IF_NOT_EXISTS = 'create_if_not_exists';
25+
2126
/**
2227
* Create the object to represent the server
2328
*
@@ -37,12 +42,13 @@ public function __construct(array $options)
3742
);
3843
}
3944

40-
if (isset($options['client']) && $options['client'] instanceof \GuzzleHttp\ClientInterface) {
41-
$client = $options['client'];
42-
} elseif (isset($options['url'])) {
45+
if (isset($options[self::OPTION_CLIENT])
46+
&& $options[self::OPTION_CLIENT] instanceof \GuzzleHttp\ClientInterface) {
47+
$client = $options[self::OPTION_CLIENT];
48+
} elseif (isset($options[self::OPTION_URL])) {
4349
// set a descriptive user agent
4450
$user_agent = \GuzzleHttp\default_user_agent();
45-
$client = new \GuzzleHttp\Client(["base_uri" => $options['url'],
51+
$client = new \GuzzleHttp\Client(["base_uri" => $options[self::OPTION_URL],
4652
"headers" => ["User-Agent" => "PHPCouchDB/" . VERSION . " " . $user_agent]]);
4753
} else {
4854
throw new Exception\ServerException(
@@ -117,15 +123,21 @@ public function getAllDbs() : array
117123
public function useDb($options) : Database
118124
{
119125
// check the $options array is sane
120-
if (!isset($options['name'])) {
126+
if (!isset($options[self::OPTION_NAME])) {
121127
throw new Exception\ServerException(
122128
'"name" is a required $options parameter'
123129
);
124130
} else {
125-
$db_name = $options['name'];
131+
$db_name = $options[self::OPTION_NAME];
126132
}
127133

128-
$create_if_not_exists = isset($options['create_if_not_exists']) ? $options['create_if_not_exists'] : false;
134+
135+
if (isset($options[self::OPTION_CREATE_IF_NOT_EXISTS])) {
136+
$create_if_not_exists = $options[self::OPTION_CREATE_IF_NOT_EXISTS];
137+
} else {
138+
// default value
139+
$create_if_not_exists = false;
140+
}
129141

130142
// does this database exist?
131143
$exists = false;

tests/PHPCouchDB/DatabaseTest.php

+33-23
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public function testGetAllDocs() {
3030
$client = new Client(['handler' => $handler]);
3131

3232
// userland code starts
33-
$server = new \PHPCouchDB\Server(["client" => $client]);
34-
$database = $server->useDB(["name" => "egdb"]);
33+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
34+
$database = $server->useDB([\PHPCouchDB\Server::OPTION_NAME => "egdb"]);
3535
$docs = $database->getAllDocs();
3636

3737
$this->assertInternalType('array', $docs);
@@ -50,8 +50,8 @@ public function testGetAllDocsWithNoDocs() {
5050
$client = new Client(['handler' => $handler]);
5151

5252
// userland code starts
53-
$server = new \PHPCouchDB\Server(["client" => $client]);
54-
$database = $server->useDB(["name" => "egdb"]);
53+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
54+
$database = $server->useDB([\PHPCouchDB\Server::OPTION_NAME => "egdb"]);
5555
$docs = $database->getAllDocs();
5656

5757
$this->assertInternalType('array', $docs);
@@ -67,8 +67,8 @@ public function testCreateWithID() {
6767
$client = new Client(['handler' => $handler]);
6868

6969
// userland code starts
70-
$server = new \PHPCouchDB\Server(["client" => $client]);
71-
$database = $server->useDB(["name" => "egdb"]);
70+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
71+
$database = $server->useDB([\PHPCouchDB\Server::OPTION_NAME => "egdb"]);
7272
$doc = $database->create(["noise" => "howl", "id" => "abcde12345"]);
7373

7474
$this->assertInstanceOf('PHPCouchDB\Document', $doc);
@@ -85,8 +85,8 @@ public function testCreateWithoutID() {
8585
$client = new Client(['handler' => $handler]);
8686

8787
// userland code starts
88-
$server = new \PHPCouchDB\Server(["client" => $client]);
89-
$database = $server->useDB(["name" => "egdb"]);
88+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
89+
$database = $server->useDB([\PHPCouchDB\Server::OPTION_NAME => "egdb"]);
9090
$doc = $database->create(["noise" => "howl"]);
9191

9292
$this->assertInstanceOf('PHPCouchDB\Document', $doc);
@@ -105,8 +105,8 @@ public function testGetDocById() {
105105
$client = new Client(['handler' => $handler]);
106106

107107
// userland code starts
108-
$server = new \PHPCouchDB\Server(["client" => $client]);
109-
$database = $server->useDB(["name" => "egdb"]);
108+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
109+
$database = $server->useDB([\PHPCouchDB\Server::OPTION_NAME => "egdb"]);
110110
$doc = $database->create(["noise" => "crackle"]);
111111

112112
$fetched_doc = $database->getDocById($doc->id);
@@ -122,8 +122,8 @@ public function testGetName() {
122122
$client = new Client(['handler' => $handler]);
123123

124124
// userland code starts
125-
$server = new \PHPCouchDB\Server(["client" => $client]);
126-
$database = $server->useDB(["name" => "egdb"]);
125+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
126+
$database = $server->useDB([\PHPCouchDB\Server::OPTION_NAME => "egdb"]);
127127

128128
$this->assertInternalType('string', $database->getName());
129129
}
@@ -135,8 +135,8 @@ public function testGetClient() {
135135
$client = new Client(['handler' => $handler]);
136136

137137
// userland code starts
138-
$server = new \PHPCouchDB\Server(["client" => $client]);
139-
$database = $server->useDB(["name" => "egdb"]);
138+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
139+
$database = $server->useDB([\PHPCouchDB\Server::OPTION_NAME => "egdb"]);
140140

141141
$this->assertInstanceOf('\GuzzleHttp\ClientInterface', $database->getClient());
142142
}
@@ -154,9 +154,9 @@ public function testAllDocsWithoutIncludeDocs() {
154154
$client = new Client(['handler' => $handler]);
155155

156156
// userland code starts
157-
$server = new \PHPCouchDB\Server(["client" => $client]);
158-
$database = $server->useDB(["name" => "egdb"]);
159-
$docs = $database->getAllDocs(["include_docs" => false]);
157+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
158+
$database = $server->useDB([\PHPCouchDB\Server::OPTION_NAME => "egdb"]);
159+
$docs = $database->getAllDocs([\PHPCouchDB\Database::OPTION_INCLUDE_DOCS => false]);
160160

161161
$this->assertInternalType('array', $docs);
162162
$this->assertInternalType('array', $docs[0]);
@@ -178,9 +178,13 @@ public function testView() {
178178
$client = new Client(['handler' => $handler]);
179179

180180
// userland code starts
181-
$server = new \PHPCouchDB\Server(["client" => $client]);
182-
$database = $server->useDB(["name" => "egdb"]);
183-
$docs = $database->getView(["ddoc" => "myview", "view" => "year", "group" => true]);
181+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
182+
$database = $server->useDB([\PHPCouchDB\Server::OPTION_NAME => "egdb"]);
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));
@@ -201,9 +205,15 @@ public function testViewWithIncludeDocs() {
201205
$client = new Client(['handler' => $handler]);
202206

203207
// userland code starts
204-
$server = new \PHPCouchDB\Server(["client" => $client]);
205-
$database = $server->useDB(["name" => "egdb"]);
206-
$docs = $database->getView(["ddoc" => "myview", "view" => "year", "reduce" => false, "limit" => 3, "include_docs" => true]);
208+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
209+
$database = $server->useDB([\PHPCouchDB\Server::OPTION_NAME => "egdb"]);
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));

tests/PHPCouchDB/DocumentTest.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public function testUpdate() {
2929
$client = new Client(['handler' => $handler]);
3030

3131
// userland code starts
32-
$server = new \PHPCouchDB\Server(["client" => $client]);
33-
$database = $server->useDB(["name" => "egdb"]);
32+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
33+
$database = $server->useDB([\PHPCouchDB\Server::OPTION_NAME => "egdb"]);
3434
$doc = $database->create(["noise" => "howl", "id" => "abcde12345"]);
3535

3636
$doc->noise = "purr";
@@ -54,8 +54,8 @@ public function testUpdateConflict() {
5454
$client = new Client(['handler' => $handler]);
5555

5656
// userland code starts
57-
$server = new \PHPCouchDB\Server(["client" => $client]);
58-
$database = $server->useDB(["name" => "egdb"]);
57+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
58+
$database = $server->useDB([\PHPCouchDB\Server::OPTION_NAME => "egdb"]);
5959
$doc = $database->create(["noise" => "howl", "id" => "abcde12345"]);
6060

6161
$doc->noise = "purr";
@@ -74,8 +74,8 @@ public function testDelete() {
7474
$client = new Client(['handler' => $handler]);
7575

7676
// userland code starts
77-
$server = new \PHPCouchDB\Server(["client" => $client]);
78-
$database = $server->useDB(["name" => "egdb"]);
77+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
78+
$database = $server->useDB([\PHPCouchDB\Server::OPTION_NAME => "egdb"]);
7979
$doc = $database->create(["noise" => "howl", "id" => "abcde12345"]);
8080

8181
$result = $doc->delete();
@@ -99,8 +99,8 @@ public function testDeleteConflict() {
9999
$client = new Client(['handler' => $handler]);
100100

101101
// userland code starts
102-
$server = new \PHPCouchDB\Server(["client" => $client]);
103-
$database = $server->useDB(["name" => "egdb"]);
102+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
103+
$database = $server->useDB([\PHPCouchDB\Server::OPTION_NAME => "egdb"]);
104104
$doc = $database->create(["noise" => "howl", "id" => "abcde12345"]);
105105

106106
$result = $doc->delete();

0 commit comments

Comments
 (0)