/** * Constructs a count command. * * Supported options: * * * hint (string|document): The index to use. If a document, it will be * interpretted as an index specification and a name will be generated. * * * limit (integer): The maximum number of documents to count. * * * maxTimeMS (integer): The maximum amount of time to allow the query to * run. * * * readConcern (MongoDB\Driver\ReadConcern): Read concern. * * For servers < 3.2, this option is ignored as read concern is not * available. * * * readPreference (MongoDB\Driver\ReadPreference): Read preference. * * * skip (integer): The number of documents to skip before returning the * documents. * * @param string $databaseName Database name * @param string $collectionName Collection name * @param array|object $filter Query by which to filter documents * @param array $options Command options * * @throws InvalidArgumentException */ public function __construct($databaseName, $collectionName, $filter = [], array $options = []) { if (!is_array($filter) && !is_object($filter)) { throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object'); } if (isset($options['hint'])) { if (is_array($options['hint']) || is_object($options['hint'])) { $options['hint'] = Functions::generateIndexName($options['hint']); } if (!is_string($options['hint'])) { throw InvalidArgumentException::invalidType('"hint" option', $options['hint'], 'string or array or object'); } } if (isset($options['limit']) && !is_integer($options['limit'])) { throw InvalidArgumentException::invalidType('"limit" option', $options['limit'], 'integer'); } if (isset($options['maxTimeMS']) && !is_integer($options['maxTimeMS'])) { throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer'); } if (isset($options['readConcern']) && !$options['readConcern'] instanceof ReadConcern) { throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], 'MongoDB\\Driver\\ReadConcern'); } if (isset($options['readPreference']) && !$options['readPreference'] instanceof ReadPreference) { throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], 'MongoDB\\Driver\\ReadPreference'); } if (isset($options['skip']) && !is_integer($options['skip'])) { throw InvalidArgumentException::invalidType('"skip" option', $options['skip'], 'integer'); } $this->databaseName = (string) $databaseName; $this->collectionName = (string) $collectionName; $this->filter = $filter; $this->options = $options; }
/** * Constructor. * * @param array $index Index specification * * @throws InvalidArgumentException */ public function __construct(array $index) { if (!isset($index['key'])) { throw new InvalidArgumentException('Required "key" document is missing from index specification'); } if (!is_array($index['key']) && !is_object($index['key'])) { throw InvalidArgumentException::invalidType('"key" option', $index['key'], 'array or object'); } foreach ($index['key'] as $fieldName => $order) { if (!is_int($order) && !is_float($order) && !is_string($order)) { throw InvalidArgumentException::invalidType(sprintf('order value for "%s" field within "key" option', $fieldName), $order, 'numeric or string'); } } if (!isset($index['ns'])) { throw new InvalidArgumentException('Required "ns" option is missing from index specification'); } if (!is_string($index['ns'])) { throw InvalidArgumentException::invalidType('"ns" option', $index['ns'], 'string'); } if (!isset($index['name'])) { $index['name'] = Functions::generateIndexName($index['key']); } if (!is_string($index['name'])) { throw InvalidArgumentException::invalidType('"name" option', $index['name'], 'string'); } $this->index = $index; }