Distinct.php 5.99 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 20

namespace MongoDB\Operation;

use MongoDB\Driver\Command;
21
use MongoDB\Driver\ReadConcern;
22
use MongoDB\Driver\ReadPreference;
23
use MongoDB\Driver\Server;
24
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
25 26
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
27
use MongoDB\Exception\UnsupportedException;
28 29 30 31 32

/**
 * Operation for the distinct command.
 *
 * @api
33
 * @see \MongoDB\Collection::distinct()
34 35 36 37
 * @see http://docs.mongodb.org/manual/reference/command/distinct/
 */
class Distinct implements Executable
{
38
    private static $wireVersionForCollation = 5;
39 40
    private static $wireVersionForReadConcern = 4;

41 42 43 44 45 46 47 48 49 50 51
    private $databaseName;
    private $collectionName;
    private $fieldName;
    private $filter;
    private $options;

    /**
     * Constructs a distinct command.
     *
     * Supported options:
     *
52 53 54 55 56
     *  * collation (document): Collation specification.
     *
     *    This is not supported for server versions < 3.4 and will result in an
     *    exception at execution time if used.
     *
57 58 59
     *  * maxTimeMS (integer): The maximum amount of time to allow the query to
     *    run.
     *
60 61
     *  * readConcern (MongoDB\Driver\ReadConcern): Read concern.
     *
62 63
     *    This is not supported for server versions < 3.2 and will result in an
     *    exception at execution time if used.
64
     *
65 66
     *  * readPreference (MongoDB\Driver\ReadPreference): Read preference.
     *
67 68 69 70 71
     * @param string       $databaseName   Database name
     * @param string       $collectionName Collection name
     * @param string       $fieldName      Field for which to return distinct values
     * @param array|object $filter         Query by which to filter documents
     * @param array        $options        Command options
72
     * @throws InvalidArgumentException for parameter/option parsing errors
73
     */
Jeremy Mikola's avatar
Jeremy Mikola committed
74
    public function __construct($databaseName, $collectionName, $fieldName, $filter = [], array $options = [])
75
    {
76
        if ( ! is_array($filter) && ! is_object($filter)) {
77
            throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
78 79
        }

80 81 82 83
        if (isset($options['collation']) && ! is_array($options['collation']) && ! is_object($options['collation'])) {
            throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'array or object');
        }

84
        if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
85
            throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
86 87
        }

88
        if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
89
            throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], 'MongoDB\Driver\ReadConcern');
90 91
        }

92
        if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
93
            throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
94 95
        }

96 97 98 99 100 101 102 103 104 105 106 107 108
        $this->databaseName = (string) $databaseName;
        $this->collectionName = (string) $collectionName;
        $this->fieldName = (string) $fieldName;
        $this->filter = $filter;
        $this->options = $options;
    }

    /**
     * Execute the operation.
     *
     * @see Executable::execute()
     * @param Server $server
     * @return mixed[]
109
     * @throws UnexpectedValueException if the command response was malformed
110
     * @throws UnsupportedException if collation or read concern is used and unsupported
111
     * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
112 113 114
     */
    public function execute(Server $server)
    {
115 116 117 118
        if (isset($this->options['collation']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) {
            throw UnsupportedException::collationNotSupported();
        }

119 120 121 122
        if (isset($this->options['readConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
            throw UnsupportedException::readConcernNotSupported();
        }

123 124
        $readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;

125
        $cursor = $server->executeCommand($this->databaseName, $this->createCommand(), $readPreference);
126 127
        $result = current($cursor->toArray());

128
        if ( ! isset($result->values) || ! is_array($result->values)) {
129 130 131
            throw new UnexpectedValueException('distinct command did not return a "values" array');
        }

132
        return $result->values;
133 134 135 136 137 138 139
    }

    /**
     * Create the distinct command.
     *
     * @return Command
     */
140
    private function createCommand()
141
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
142
        $cmd = [
143 144
            'distinct' => $this->collectionName,
            'key' => $this->fieldName,
Jeremy Mikola's avatar
Jeremy Mikola committed
145
        ];
146 147 148 149 150

        if ( ! empty($this->filter)) {
            $cmd['query'] = (object) $this->filter;
        }

151 152 153 154
        if (isset($this->options['collation'])) {
            $cmd['collation'] = (object) $this->options['collation'];
        }

155
        if (isset($this->options['maxTimeMS'])) {
156
            $cmd['maxTimeMS'] = $this->options['maxTimeMS'];
157 158
        }

159
        if (isset($this->options['readConcern'])) {
160 161 162
            $cmd['readConcern'] = \MongoDB\read_concern_as_document($this->options['readConcern']);
        }

163 164 165
        return new Command($cmd);
    }
}