Skip to content

Commit 55cf25d

Browse files
committed
PHPLIB-60: Create result classes for CRUD methods
The bulkWrite() method still returns the driver's original WriteResult class. We may change this to use a library-specific wrapper class later.
1 parent a3f3e85 commit 55cf25d

File tree

6 files changed

+155
-36
lines changed

6 files changed

+155
-36
lines changed

src/Collection.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,10 @@ public function bulkWrite(array $bulk, array $options = array())
406406
* Inserts the provided document
407407
*
408408
* @see http://docs.mongodb.org/manual/reference/command/insert/
409-
* @see Collection::getWriteOptions() for supported $options
410409
*
411410
* @param array $document The document to insert
412411
* @param array $options Additional options
413-
* @return InsertResult
412+
* @return InsertOneResult
414413
*/
415414
public function insertOne(array $document)
416415
{
@@ -420,7 +419,7 @@ public function insertOne(array $document)
420419
$id = $bulk->insert($document);
421420
$wr = $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);
422421

423-
return new InsertResult($wr, $id);
422+
return new InsertOneResult($wr, $id);
424423
}
425424

426425
/**

src/DeleteResult.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,46 @@
44

55
use MongoDB\Driver\WriteResult;
66

7+
/**
8+
* Result class for a delete operation.
9+
*/
710
class DeleteResult
811
{
9-
protected $wr;
12+
private $writeResult;
1013

11-
public function __construct(WriteResult $wr)
14+
/**
15+
* Constructor.
16+
*
17+
* @param WriteResult $writeResult
18+
*/
19+
public function __construct(WriteResult $writeResult)
1220
{
13-
$this->wr = $wr;
21+
$this->writeResult = $writeResult;
1422
}
1523

24+
/**
25+
* Return the number of documents that were deleted.
26+
*
27+
* This value is undefined if the write was not acknowledged.
28+
*
29+
* @see UpdateResult::isAcknowledged()
30+
* @return integer
31+
*/
1632
public function getDeletedCount()
1733
{
18-
return $this->wr->getDeletedCount();
34+
return $this->writeResult->getDeletedCount();
35+
}
36+
37+
/**
38+
* Return whether this delete was acknowledged by the server.
39+
*
40+
* If the delete was not acknowledged, other fields from the WriteResult
41+
* (e.g. deletedCount) will be undefined.
42+
*
43+
* @return boolean
44+
*/
45+
public function isAcknowledged()
46+
{
47+
return $this->writeResult->isAcknowledged();
1948
}
2049
}

src/InsertOneResult.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace MongoDB;
4+
5+
use BSON\ObjectId;
6+
use MongoDB\Driver\WriteResult;
7+
8+
/**
9+
* Result class for a single-document insert operation.
10+
*/
11+
class InsertOneResult
12+
{
13+
private $writeResult;
14+
private $insertedId;
15+
16+
/**
17+
* Constructor.
18+
*
19+
* @param WriteResult $writeResult
20+
* @param ObjectId $insertedId
21+
*/
22+
public function __construct(WriteResult $writeResult, ObjectId $insertedId = null)
23+
{
24+
$this->writeResult = $writeResult;
25+
$this->insertedId = $insertedId;
26+
}
27+
28+
/**
29+
* Return the number of documents that were inserted.
30+
*
31+
* This value is undefined if the write was not acknowledged.
32+
*
33+
* @see InsertOneResult::isAcknowledged()
34+
* @return integer
35+
*/
36+
public function getInsertedCount()
37+
{
38+
return $this->writeResult->getInsertedCount();
39+
}
40+
41+
/**
42+
* Return the inserted ID that was generated by the driver.
43+
*
44+
* If the inserted document already had an ID (e.g. it was generated by the
45+
* application), this will be null.
46+
*
47+
* @return ObjectId|null
48+
*/
49+
public function getInsertedId()
50+
{
51+
return $this->insertedId;
52+
}
53+
54+
/**
55+
* Return whether this insert was acknowledged by the server.
56+
*
57+
* If the insert was not acknowledged, other fields from the WriteResult
58+
* (e.g. insertedCount) will be undefined.
59+
*
60+
* @return boolean
61+
*/
62+
public function isAcknowledged()
63+
{
64+
return $this->writeResult->isAcknowledged();
65+
}
66+
}

src/InsertResult.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/UpdateResult.php

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,76 @@
22

33
namespace MongoDB;
44

5+
use BSON\ObjectId;
56
use MongoDB\Driver\WriteResult;
67

8+
/**
9+
* Result class for an update operation.
10+
*/
711
class UpdateResult
812
{
9-
protected $wr;
13+
private $writeResult;
1014

11-
public function __construct(WriteResult $wr)
15+
/**
16+
* Constructor.
17+
*
18+
* @param WriteResult $writeResult
19+
*/
20+
public function __construct(WriteResult $writeResult)
1221
{
13-
$this->wr = $wr;
22+
$this->writeResult = $writeResult;
1423
}
1524

25+
/**
26+
* Return the number of documents that were matched by the filter.
27+
*
28+
* This value is undefined if the write was not acknowledged.
29+
*
30+
* @see UpdateResult::isAcknowledged()
31+
* @return integer
32+
*/
1633
public function getMatchedCount()
1734
{
18-
return $this->wr->getMatchedCount();
35+
return $this->writeResult->getMatchedCount();
1936
}
2037

38+
/**
39+
* Return the number of documents that were modified.
40+
*
41+
* This value is undefined if the write was not acknowledged.
42+
*
43+
* @see UpdateResult::isAcknowledged()
44+
* @return integer
45+
*/
2146
public function getModifiedCount()
2247
{
23-
return $this->wr->getModifiedCount();
48+
return $this->writeResult->getModifiedCount();
2449
}
2550

51+
/**
52+
* Return the ID of the document inserted by an upsert operation.
53+
*
54+
* This value is undefined if an upsert did not take place.
55+
*
56+
* @return ObjectId|null
57+
*/
2658
public function getUpsertedId()
2759
{
28-
return $this->wr->getUpsertedIds()[0];
60+
foreach ($this->writeResult->getUpsertedIds() as $id) {
61+
return $id;
62+
}
63+
}
64+
65+
/**
66+
* Return whether this update was acknowledged by the server.
67+
*
68+
* If the update was not acknowledged, other fields from the WriteResult
69+
* (e.g. matchedCount) will be undefined.
70+
*
71+
* @return boolean
72+
*/
73+
public function isAcknowledged()
74+
{
75+
return $this->writeResult->isAcknowledged();
2976
}
3077
}

tests/CollectionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function testInsertAndRetrieve() {
2121
for($i=0; $i<10;$i++) {
2222
$user = createUser($this->faker);
2323
$result = $collection->insertOne($user);
24-
$this->assertInstanceOf('MongoDB\InsertResult', $result);
24+
$this->assertInstanceOf('MongoDB\InsertOneResult', $result);
2525
$this->assertInstanceOf('BSON\ObjectId', $result->getInsertedId());
2626
$this->assertEquals(24, strlen($result->getInsertedId()));
2727

0 commit comments

Comments
 (0)