UpdateResult.php 4.05 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 an update operation.
 */
26 27
class UpdateResult
{
28
    /** @var WriteResult */
29
    private $writeResult;
30 31

    /** @var boolean */
32
    private $isAcknowledged;
33

34
    public function __construct(WriteResult $writeResult)
35
    {
36
        $this->writeResult = $writeResult;
37
        $this->isAcknowledged = $writeResult->isAcknowledged();
38 39
    }

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

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
56 57
    }

58 59 60
    /**
     * Return the number of documents that were modified.
     *
61 62 63 64
     * This value is undefined (i.e. null) if the write executed as a legacy
     * operation instead of command.
     *
     * This method should only be called if the write was acknowledged.
65 66
     *
     * @see UpdateResult::isAcknowledged()
67 68
     * @return integer|null
     * @throws BadMethodCallException is the write result is unacknowledged
69
     */
70 71
    public function getModifiedCount()
    {
72 73 74 75 76
        if ($this->isAcknowledged) {
            return $this->writeResult->getModifiedCount();
        }

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
77 78
    }

79 80 81
    /**
     * Return the number of documents that were upserted.
     *
82
     * This method should only be called if the write was acknowledged.
83 84 85
     *
     * @see UpdateResult::isAcknowledged()
     * @return integer
86
     * @throws BadMethodCallException is the write result is unacknowledged
87 88 89
     */
    public function getUpsertedCount()
    {
90 91 92 93 94
        if ($this->isAcknowledged) {
            return $this->writeResult->getUpsertedCount();
        }

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
95 96
    }

97 98
    /**
     * Return the ID of the document inserted by an upsert operation.
99 100 101
     *
     * If the document had an ID prior to upserting (i.e. the server did not
     * need to generate an ID), this will contain its "_id". Any
102
     * server-generated ID will be a MongoDB\BSON\ObjectId instance.
103
     *
104 105 106
     * This value is undefined (i.e. null) if an upsert did not take place.
     *
     * This method should only be called if the write was acknowledged.
107
     *
108
     * @see UpdateResult::isAcknowledged()
109
     * @return mixed|null
110
     * @throws BadMethodCallException is the write result is unacknowledged
111
     */
112 113
    public function getUpsertedId()
    {
114 115
        if ($this->isAcknowledged) {
            foreach ($this->writeResult->getUpsertedIds() as $id) {
Jeremy Mikola's avatar
Jeremy Mikola committed
116
                return $id;
117 118 119
            }

            return null;
120
        }
121 122

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
123 124 125 126 127 128
    }

    /**
     * Return whether this update was acknowledged by the server.
     *
     * If the update was not acknowledged, other fields from the WriteResult
129 130
     * (e.g. matchedCount) will be undefined and their getter methods should not
     * be invoked.
131 132 133 134 135
     *
     * @return boolean
     */
    public function isAcknowledged()
    {
136
        return $this->isAcknowledged;
137 138
    }
}