Skip to content

Commit 15d6568

Browse files
committed
Added class constants to use instead of strings
1 parent 1f06178 commit 15d6568

File tree

3 files changed

+38
-26
lines changed

3 files changed

+38
-26
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ Here's the tl;dr of how to begin. For more detailed examples, see the [wiki](ht
2424
require "vendor/autoload.php";
2525

2626
// connect to CouchDB (does make a call to check we can connect)
27-
$server = new \PHPCouchDB\Server(["url" => "http://localhost:5984"]);
27+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_URL => "http://localhost:5984"]);
2828

2929
// get a list of databases; each one is a \PHPCouchDB\Database object
3030
$databases = $server->getAllDbs();
3131

3232
// work with the "test" database (also a \PHPCouchDB\Database object)
33-
$test_db = $server->useDb(["name" => "test", "create_if_not_exists" => true]);
33+
$test_db = $server->useDb([\PHPCouchDB\Server::OPTION_NAME => "test", \PHPCouchDB\Server::OPTION_CREATE_IF_NOT_EXISTS => true]);
3434

3535
// add a document - you may specify the "id" here if you like
3636
$doc = $test_db->create(["name" => "Alice", "interests" => ["eating", "wondering"]]);

src/PHPCouchDB/Server.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ class Server
1818
{
1919
protected $client;
2020

21+
const OPTION_CLIENT = 'client';
22+
23+
const OPTION_URL = 'url';
24+
25+
const OPTION_NAME = 'name';
26+
27+
const OPTION_CREATE_IF_NOT_EXISTS = 'create_if_not_exists';
28+
2129
/**
2230
* Create the object to represent the server
2331
*
@@ -37,12 +45,12 @@ public function __construct(array $options)
3745
);
3846
}
3947

40-
if (isset($options['client']) && $options['client'] instanceof \GuzzleHttp\ClientInterface) {
41-
$client = $options['client'];
42-
} elseif (isset($options['url'])) {
48+
if (isset($options[self::OPTION_CLIENT]) && $options[self::OPTION_CLIENT] instanceof \GuzzleHttp\ClientInterface) {
49+
$client = $options[self::OPTION_CLIENT];
50+
} elseif (isset($options[self::OPTION_URL])) {
4351
// set a descriptive user agent
4452
$user_agent = \GuzzleHttp\default_user_agent();
45-
$client = new \GuzzleHttp\Client(["base_uri" => $options['url'],
53+
$client = new \GuzzleHttp\Client(["base_uri" => $options[self::OPTION_URL],
4654
"headers" => ["User-Agent" => "PHPCouchDB/" . VERSION . " " . $user_agent]]);
4755
} else {
4856
throw new Exception\ServerException(
@@ -117,15 +125,15 @@ public function getAllDbs() : array
117125
public function useDb($options) : Database
118126
{
119127
// check the $options array is sane
120-
if (!isset($options['name'])) {
128+
if (!isset($options[self::OPTION_NAME])) {
121129
throw new Exception\ServerException(
122130
'"name" is a required $options parameter'
123131
);
124132
} else {
125-
$db_name = $options['name'];
133+
$db_name = $options[self::OPTION_NAME];
126134
}
127135

128-
$create_if_not_exists = isset($options['create_if_not_exists']) ? $options['create_if_not_exists'] : false;
136+
$create_if_not_exists = isset($options[self::OPTION_CREATE_IF_NOT_EXISTS]) ? $options[self::OPTION_CREATE_IF_NOT_EXISTS] : false;
129137

130138
// does this database exist?
131139
$exists = false;

tests/PHPCouchDB/ServerTest.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public function testCreateWithClient() {
2525
$client = new Client(['handler' => $handler]);
2626

2727
// userland code starts
28-
$server = new \PHPCouchDB\Server(["client" => $client]);
28+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
2929

30-
$this->assertObjectHasAttribute('client', $server);
31-
$this->assertAttributeInstanceOf('\GuzzleHttp\ClientInterface', 'client', $server);
30+
$this->assertObjectHasAttribute(\PHPCouchDB\Server::OPTION_CLIENT, $server);
31+
$this->assertAttributeInstanceOf('\GuzzleHttp\ClientInterface', \PHPCouchDB\Server::OPTION_CLIENT, $server);
3232
}
3333

3434
public function testCreateWithUrl() {
@@ -37,10 +37,10 @@ public function testCreateWithUrl() {
3737
$handler = HandlerStack::create($mock);
3838

3939
// userland code starts
40-
$server = new \PHPCouchDB\Server(["url" => "http://localhost:5984"]);
40+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_URL => "http://localhost:5984"]);
4141

42-
$this->assertObjectHasAttribute('client', $server);
43-
$this->assertAttributeInstanceOf('\GuzzleHttp\ClientInterface', 'client', $server);
42+
$this->assertObjectHasAttribute(\PHPCouchDB\Server::OPTION_CLIENT, $server);
43+
$this->assertAttributeInstanceOf('\GuzzleHttp\ClientInterface', \PHPCouchDB\Server::OPTION_CLIENT, $server);
4444

4545
$config = $server->getClient()->getConfig();
4646
$this->assertArrayHasKey('User-Agent', $config['headers']);
@@ -54,7 +54,7 @@ public function testGetVersion() {
5454
$client = new Client(['handler' => $handler]);
5555

5656
// userland code starts
57-
$server = new \PHPCouchDB\Server(["client" => $client]);
57+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
5858
$this->assertEquals("2.0.0", $server->getVersion());
5959

6060
}
@@ -68,7 +68,7 @@ public function testGetAllDbs() {
6868
$client = new Client(['handler' => $handler]);
6969

7070
// userland code starts
71-
$server = new \PHPCouchDB\Server(["client" => $client]);
71+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
7272
$this->assertEquals($dbs, $server->getAllDbs());
7373
}
7474

@@ -81,8 +81,8 @@ public function testUseADbThatDoesExist() {
8181
$client = new Client(['handler' => $handler]);
8282

8383
// userland code starts
84-
$server = new \PHPCouchDB\Server(["client" => $client]);
85-
$this->assertInstanceOf("\PHPCouchDB\Database", $server->useDb(["name" => "egdb"]));
84+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
85+
$this->assertInstanceOf("\PHPCouchDB\Database", $server->useDb([\PHPCouchDB\Server::OPTION_NAME => "egdb"]));
8686
}
8787

8888
public function testUseADbWithCreateThatDoesExist() {
@@ -94,8 +94,10 @@ public function testUseADbWithCreateThatDoesExist() {
9494
$client = new Client(['handler' => $handler]);
9595

9696
// userland code starts
97-
$server = new \PHPCouchDB\Server(["client" => $client]);
98-
$this->assertInstanceOf("\PHPCouchDB\Database", $server->useDb(["name" => "egdb", "create_if_not_exists" => false]));
97+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
98+
$this->assertInstanceOf("\PHPCouchDB\Database", $server->useDb(
99+
[\PHPCouchDB\Server::OPTION_NAME => "egdb", \PHPCouchDB\Server::OPTION_CREATE_IF_NOT_EXISTS => false]
100+
));
99101
}
100102

101103
/**
@@ -110,8 +112,8 @@ public function testUseADbThatDoesNotExist() {
110112
$client = new Client(['handler' => $handler]);
111113

112114
// userland code starts
113-
$server = new \PHPCouchDB\Server(["client" => $client]);
114-
$server->useDb(["name" => "egdb"]);
115+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
116+
$server->useDb([\PHPCouchDB\Server::OPTION_NAME => "egdb"]);
115117
}
116118

117119
public function testUseADbWithCreateThatDoesNotExist() {
@@ -126,8 +128,10 @@ public function testUseADbWithCreateThatDoesNotExist() {
126128
$client = new Client(['handler' => $handler]);
127129

128130
// userland code starts
129-
$server = new \PHPCouchDB\Server(["client" => $client]);
130-
$this->assertInstanceOf("\PHPCouchDB\Database", $server->useDb(["name" => "egdb", "create_if_not_exists" => true]));
131+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
132+
$this->assertInstanceOf("\PHPCouchDB\Database", $server->useDb(
133+
[\PHPCouchDB\Server::OPTION_NAME => "egdb", \PHPCouchDB\Server::OPTION_CREATE_IF_NOT_EXISTS => true]
134+
));
131135
}
132136

133137
public function testGetClient() {
@@ -137,7 +141,7 @@ public function testGetClient() {
137141
$client = new Client(['handler' => $handler]);
138142

139143
// userland code starts
140-
$server = new \PHPCouchDB\Server(["client" => $client]);
144+
$server = new \PHPCouchDB\Server([\PHPCouchDB\Server::OPTION_CLIENT => $client]);
141145
$this->assertInstanceOf("\GuzzleHttp\ClientInterface", $server->getClient());
142146
}
143147
}

0 commit comments

Comments
 (0)