Skip to content

Commit bf80641

Browse files
committed
Document also needs a client and the db info
1 parent dadce73 commit bf80641

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/PHPCouchDB/Database.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function getAllDocs($options = []) : array
5050
// we have some data - extract the docs to return
5151
$docs = [];
5252
foreach ($json_data["rows"] as $document) {
53-
$docs[] = new Document($document["doc"]);
53+
$docs[] = new Document($this->client, $this->db_name, $document["doc"]);
5454
}
5555
return $docs;
5656
} else {
@@ -117,8 +117,8 @@ public function getDocById($id) : \PHPCouchDB\Document
117117
$endpoint = "/" . $this->db_name . "/" . $id;
118118
$response = $this->client->request("GET", $endpoint);
119119
if ($response->getStatusCode() == 200) {
120-
if ($json_data = json_decode($response->getBody(), true)) {
121-
$doc = new Document($json_data);
120+
if ($data = json_decode($response->getBody(), true)) {
121+
$doc = new Document($this->client, $this->db_name, $data);
122122
return $doc;
123123
} else {
124124
throw new \PHPCouchDB\Exception\ServerException('JSON response not received or not understood');

src/PHPCouchDB/Document.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,29 @@
88

99
/**
1010
* Documents are the "rows" in a document database. The object has keys and
11-
* values to represent the keys and values of the document.
11+
* values to represent the keys and values of the document. Operations on
12+
* an existing document, such as update and delete, are done by this object
1213
*/
1314

1415

1516
class Document
1617
{
18+
protected $client;
19+
protected $db_name;
20+
1721
/**
1822
* Usually constructed by the Database object as it gets documents for us
1923
*
24+
* @param \GuzzleHTTP\ClientInterface $client Our HTTP client
25+
* @param string $db_name The database this document is in
2026
* @param array $data Representation of the document
2127
*/
22-
public function __construct($data)
28+
*/
29+
30+
public function __construct(\GuzzleHttp\ClientInterface $client, string $db_name, array $data)
2331
{
32+
$this->client = $client;
33+
$this->db_name = $db_name;
2434
// possibly overly simple!
2535
// Add all array elements as properties on the new object
2636
foreach ($data as $field => $value) {

0 commit comments

Comments
 (0)