DeleteResult.php 1.92 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2015-2017 MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
17

18 19
namespace MongoDB;

20
use MongoDB\Driver\WriteResult;
21
use MongoDB\Exception\BadMethodCallException;
22

23 24 25
/**
 * Result class for a delete operation.
 */
26 27
class DeleteResult
{
28
    private $writeResult;
29
    private $isAcknowledged;
30

31 32 33 34 35 36
    /**
     * Constructor.
     *
     * @param WriteResult $writeResult
     */
    public function __construct(WriteResult $writeResult)
37
    {
38
        $this->writeResult = $writeResult;
39
        $this->isAcknowledged = $writeResult->isAcknowledged();
40 41
    }

42 43 44
    /**
     * Return the number of documents that were deleted.
     *
45
     * This method should only be called if the write was acknowledged.
46
     *
47
     * @see DeleteResult::isAcknowledged()
48
     * @return integer
49
     * @throws BadMethodCallException is the write result is unacknowledged
50
     */
51 52
    public function getDeletedCount()
    {
53 54 55 56 57
        if ($this->isAcknowledged) {
            return $this->writeResult->getDeletedCount();
        }

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
58 59 60 61 62 63 64 65 66 67 68 69
    }

    /**
     * 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()
    {
70
        return $this->isAcknowledged;
71 72
    }
}