UpdateResult.php 4.08 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
    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 matched by the filter.
     *
45
     * This method should only be called if the write was acknowledged.
46 47 48
     *
     * @see UpdateResult::isAcknowledged()
     * @return integer
49
     * @throws BadMethodCallException is the write result is unacknowledged
50
     */
51 52
    public function getMatchedCount()
    {
53 54 55 56 57
        if ($this->isAcknowledged) {
            return $this->writeResult->getMatchedCount();
        }

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
58 59
    }

60 61 62
    /**
     * Return the number of documents that were modified.
     *
63 64 65 66
     * 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.
67 68
     *
     * @see UpdateResult::isAcknowledged()
69 70
     * @return integer|null
     * @throws BadMethodCallException is the write result is unacknowledged
71
     */
72 73
    public function getModifiedCount()
    {
74 75 76 77 78
        if ($this->isAcknowledged) {
            return $this->writeResult->getModifiedCount();
        }

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
79 80
    }

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

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
97 98
    }

99 100
    /**
     * Return the ID of the document inserted by an upsert operation.
101 102 103
     *
     * 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
104
     * server-generated ID will be a MongoDB\BSON\ObjectId instance.
105
     *
106 107 108
     * 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.
109
     *
110
     * @see UpdateResult::isAcknowledged()
111
     * @return mixed|null
112
     * @throws BadMethodCallException is the write result is unacknowledged
113
     */
114 115
    public function getUpsertedId()
    {
116 117
        if ($this->isAcknowledged) {
            foreach ($this->writeResult->getUpsertedIds() as $id) {
Jeremy Mikola's avatar
Jeremy Mikola committed
118
                return $id;
119 120 121
            }

            return null;
122
        }
123 124

        throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
125 126 127 128 129 130
    }

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