|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Operation; |
| 4 | + |
| 5 | +use MongoDB\Driver\Command; |
| 6 | +use MongoDB\Driver\Server; |
| 7 | +use MongoDB\Exception\InvalidArgumentException; |
| 8 | +use MongoDB\Exception\InvalidArgumentTypeException; |
| 9 | +use MongoDB\Exception\RuntimeException; |
| 10 | +use MongoDB\Exception\UnexpectedValueException; |
| 11 | + |
| 12 | +/** |
| 13 | + * Operation for the count command. |
| 14 | + * |
| 15 | + * @api |
| 16 | + * @see MongoDB\Collection::count() |
| 17 | + * @see http://docs.mongodb.org/manual/reference/command/count/ |
| 18 | + */ |
| 19 | +class Count implements Executable |
| 20 | +{ |
| 21 | + private $databaseName; |
| 22 | + private $collectionName; |
| 23 | + private $filter; |
| 24 | + private $options; |
| 25 | + |
| 26 | + /** |
| 27 | + * Constructs a count command. |
| 28 | + * |
| 29 | + * Supported options: |
| 30 | + * |
| 31 | + * * hint (string|document): The index to use. If a document, it will be |
| 32 | + * interpretted as an index specification and a name will be generated. |
| 33 | + * |
| 34 | + * * limit (integer): The maximum number of documents to count. |
| 35 | + * |
| 36 | + * * maxTimeMS (integer): The maximum amount of time to allow the query to |
| 37 | + * run. |
| 38 | + * |
| 39 | + * * skip (integer): The number of documents to skip before returning the |
| 40 | + * documents. |
| 41 | + * |
| 42 | + * @param string $databaseName Database name |
| 43 | + * @param string $collectionName Collection name |
| 44 | + * @param array $filter Query by which to filter documents |
| 45 | + * @param array $options Command options |
| 46 | + * @throws InvalidArgumentException |
| 47 | + */ |
| 48 | + public function __construct($databaseName, $collectionName, array $filter = array(), array $options = array()) |
| 49 | + { |
| 50 | + if (isset($options['hint'])) { |
| 51 | + if (is_array($options['hint']) || is_object($options['hint'])) { |
| 52 | + $options['hint'] = $this->generateIndexName($options['hint']); |
| 53 | + } |
| 54 | + |
| 55 | + if ( ! is_string($options['hint'])) { |
| 56 | + throw new InvalidArgumentTypeException('hint option', $options['hint'], 'string or array or object'); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if (isset($options['limit']) && ! is_integer($options['limit'])) { |
| 61 | + throw new InvalidArgumentTypeException('limit option', $options['limit'], 'integer'); |
| 62 | + } |
| 63 | + |
| 64 | + if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) { |
| 65 | + throw new InvalidArgumentTypeException('maxTimeMS option', $options['maxTimeMS'], 'integer'); |
| 66 | + } |
| 67 | + |
| 68 | + if (isset($options['skip']) && ! is_integer($options['skip'])) { |
| 69 | + throw new InvalidArgumentTypeException('skip option', $options['skip'], 'integer'); |
| 70 | + } |
| 71 | + |
| 72 | + $this->databaseName = (string) $databaseName; |
| 73 | + $this->collectionName = (string) $collectionName; |
| 74 | + $this->filter = $filter; |
| 75 | + $this->options = $options; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Execute the operation. |
| 80 | + * |
| 81 | + * @see Executable::execute() |
| 82 | + * @param Server $server |
| 83 | + * @return integer |
| 84 | + */ |
| 85 | + public function execute(Server $server) |
| 86 | + { |
| 87 | + $cursor = $server->executeCommand($this->databaseName, $this->createCommand()); |
| 88 | + $result = current($cursor->toArray()); |
| 89 | + |
| 90 | + if (empty($result['ok'])) { |
| 91 | + throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); |
| 92 | + } |
| 93 | + |
| 94 | + if ( ! isset($result['n']) || ! is_integer($result['n'])) { |
| 95 | + throw new UnexpectedValueException('count command did not return an "n" integer'); |
| 96 | + } |
| 97 | + |
| 98 | + return $result['n']; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Create the count command. |
| 103 | + * |
| 104 | + * @return Command |
| 105 | + */ |
| 106 | + private function createCommand() |
| 107 | + { |
| 108 | + $cmd = array( |
| 109 | + 'count' => $this->collectionName, |
| 110 | + ); |
| 111 | + |
| 112 | + if ( ! empty($this->filter)) { |
| 113 | + $cmd['query'] = (object) $this->filter; |
| 114 | + } |
| 115 | + |
| 116 | + foreach (array('hint', 'limit', 'maxTimeMS', 'skip') as $option) { |
| 117 | + if (isset($this->options[$option])) { |
| 118 | + $cmd[$option] = $this->options[$option]; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return new Command($cmd); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Generates an index name from its key specification. |
| 127 | + * |
| 128 | + * @param array|object $key Document containing fields mapped to values, |
| 129 | + * which denote order or an index type |
| 130 | + * @return string |
| 131 | + */ |
| 132 | + private function generateIndexName($key) |
| 133 | + { |
| 134 | + $name = ''; |
| 135 | + |
| 136 | + foreach ($key as $field => $type) { |
| 137 | + $name .= ($name != '' ? '_' : '') . $field . '_' . $type; |
| 138 | + } |
| 139 | + |
| 140 | + return $name; |
| 141 | + } |
| 142 | +} |
0 commit comments