DeleteResult.php 1.33 KB
Newer Older
1
<?php
2

3 4
namespace MongoDB;

5
use MongoDB\Driver\WriteResult;
6
use MongoDB\Exception\BadMethodCallException;
7

8 9 10
/**
 * Result class for a delete operation.
 */
11 12
class DeleteResult
{
13
    private $writeResult;
14
    private $isAcknowledged;
15

16 17 18 19 20 21
    /**
     * Constructor.
     *
     * @param WriteResult $writeResult
     */
    public function __construct(WriteResult $writeResult)
22
    {
23
        $this->writeResult = $writeResult;
24
        $this->isAcknowledged = $writeResult->isAcknowledged();
25 26
    }

27 28 29
    /**
     * Return the number of documents that were deleted.
     *
30
     * This method should only be called if the write was acknowledged.
31
     *
32
     * @see DeleteResult::isAcknowledged()
33
     * @return integer
34
     * @throws BadMethodCallException is the write result is unacknowledged
35
     */
36 37
    public function getDeletedCount()
    {
38 39 40 41 42
        if ($this->isAcknowledged) {
            return $this->writeResult->getDeletedCount();
        }

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
43 44 45 46 47 48 49 50 51 52 53 54
    }

    /**
     * Return whether this delete was acknowledged by the server.
     *
     * If the delete was not acknowledged, other fields from the WriteResult
     * (e.g. deletedCount) will be undefined.
     *
     * @return boolean
     */
    public function isAcknowledged()
    {
55
        return $this->isAcknowledged;
56 57
    }
}