Distinct.php 3.51 KB
Newer Older
1 2 3 4 5
<?php

namespace MongoDB\Operation;

use MongoDB\Driver\Command;
6
use MongoDB\Driver\ReadPreference;
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\InvalidArgumentTypeException;
use MongoDB\Exception\RuntimeException;
use MongoDB\Exception\UnexpectedValueException;

/**
 * Operation for the distinct command.
 *
 * @api
 * @see MongoDB\Collection::distinct()
 * @see http://docs.mongodb.org/manual/reference/command/distinct/
 */
class Distinct implements Executable
{
    private $databaseName;
    private $collectionName;
    private $fieldName;
    private $filter;
    private $options;

    /**
     * Constructs a distinct command.
     *
     * Supported options:
     *
     *  * maxTimeMS (integer): The maximum amount of time to allow the query to
     *    run.
     *
36 37
     *  * readPreference (MongoDB\Driver\ReadPreference): Read preference.
     *
38 39 40 41 42
     * @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
43 44
     * @throws InvalidArgumentException
     */
Jeremy Mikola's avatar
Jeremy Mikola committed
45
    public function __construct($databaseName, $collectionName, $fieldName, $filter = [], array $options = [])
46
    {
47 48 49 50
        if ( ! is_array($filter) && ! is_object($filter)) {
            throw new InvalidArgumentTypeException('$filter', $filter, 'array or object');
        }

51 52 53 54
        if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
            throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
        }

55 56 57 58
        if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
            throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
        }

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        $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[]
     */
    public function execute(Server $server)
    {
75 76 77
        $readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;

        $cursor = $server->executeCommand($this->databaseName, $this->createCommand(), $readPreference);
78 79
        $result = current($cursor->toArray());

80 81
        if (empty($result->ok)) {
            throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
82 83
        }

84
        if ( ! isset($result->values) || ! is_array($result->values)) {
85 86 87
            throw new UnexpectedValueException('distinct command did not return a "values" array');
        }

88
        return $result->values;
89 90 91 92 93 94 95 96 97
    }

    /**
     * Create the distinct command.
     *
     * @return Command
     */
    private function createCommand()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
98
        $cmd = [
99 100
            'distinct' => $this->collectionName,
            'key' => $this->fieldName,
Jeremy Mikola's avatar
Jeremy Mikola committed
101
        ];
102 103 104 105 106 107 108 109 110 111 112 113

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

        if (isset($this->options['maxTimeMS'])) {
            $cmd[$option] = $this->options['maxTimeMS'];
        }

        return new Command($cmd);
    }
}