Skip to content

Commit c0183a7

Browse files
committed
Add whole Database object as constructor argument to Document
1 parent 5eab5e9 commit c0183a7

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
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($this->client, $this->db_name, $document["doc"]);
53+
$docs[] = new Document($this, $document["doc"]);
5454
}
5555
return $docs;
5656
} else {
@@ -93,7 +93,7 @@ public function create($doc)
9393
$id = $response_data['id'];
9494
// all good. Let's fetch the doc and return it
9595
$fetched_data = json_decode($this->client->get('/' . $this->db_name . '/' . $id)->getBody(), true);
96-
return new Document($this->client, $this->db_name, $fetched_data);
96+
return new Document($this, $fetched_data);
9797
}
9898
} catch (\GuzzleHttp\Exception\ConnectException $e) {
9999
throw new Exception\ServerException(
@@ -118,7 +118,7 @@ public function getDocById($id) : Document
118118
$response = $this->client->request("GET", $endpoint);
119119
if ($response->getStatusCode() == 200) {
120120
if ($data = json_decode($response->getBody(), true)) {
121-
$doc = new Document($this->client, $this->db_name, $data);
121+
$doc = new Document($this, $data);
122122
return $doc;
123123
} else {
124124
throw new Exception\ServerException('JSON response not received or not understood');

src/PHPCouchDB/Document.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,19 @@
1616
class Document
1717
{
1818
protected $client;
19-
protected $db_name;
19+
protected $database;
2020

2121
/**
2222
* Usually constructed by the Database object as it gets documents for us
2323
*
24-
* @param \GuzzleHTTP\ClientInterface $client Our HTTP client
25-
* @param string $db_name The database this document is in
24+
* @param \PHPCouchDB\Database $database The database this document is in
2625
* @param array $data Representation of the document
2726
*/
2827

29-
public function __construct(\GuzzleHttp\ClientInterface $client, string $db_name, array $data)
28+
public function __construct(Database $database, array $data)
3029
{
31-
$this->client = $client;
32-
$this->db_name = $db_name;
30+
$this->database = $database;
31+
$this->client = $database->getClient();
3332
// possibly overly simple!
3433
// Add all array elements as properties on the new object
3534
foreach ($data as $field => $value) {
@@ -44,13 +43,14 @@ public function __construct(\GuzzleHttp\ClientInterface $client, string $db_name
4443
}
4544

4645
/**
47-
* Format object for var_dump() - removes the $client property
46+
* Format object for var_dump(), removing large properties
4847
*/
4948
public function __debugInfo()
5049
{
51-
// remove the $client object because the output is HUGE
50+
// remove the $database property because the output is HUGE
5251
$result = get_object_vars($this);
5352
unset($result['client']);
53+
unset($result['database']);
5454
return $result;
5555
}
5656
}

0 commit comments

Comments
 (0)