コード例 #1
0
 /**
  * Constructor.
  *
  * @param array $index Index specification
  */
 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 new InvalidArgumentTypeException('"key" option', $index['key'], 'array or object');
     }
     foreach ($index['key'] as $fieldName => $order) {
         if (!is_int($order) && !is_float($order) && !is_string($order)) {
             throw new InvalidArgumentTypeException(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 new InvalidArgumentTypeException('"ns" option', $index['ns'], 'string');
     }
     if (!isset($index['name'])) {
         $index['name'] = \MongoDB\generate_index_name($index['key']);
     }
     if (!is_string($index['name'])) {
         throw new InvalidArgumentTypeException('"name" option', $index['name'], 'string');
     }
     $this->index = $index;
 }
コード例 #2
0
 /**
  * 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.
  *
  *  * 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(), array $options = array())
 {
     if (!is_array($filter) && !is_object($filter)) {
         throw new InvalidArgumentTypeException('$filter', $filter, 'array or object');
     }
     if (isset($options['hint'])) {
         if (is_array($options['hint']) || is_object($options['hint'])) {
             $options['hint'] = \MongoDB\generate_index_name($options['hint']);
         }
         if (!is_string($options['hint'])) {
             throw new InvalidArgumentTypeException('"hint" option', $options['hint'], 'string or array or object');
         }
     }
     if (isset($options['limit']) && !is_integer($options['limit'])) {
         throw new InvalidArgumentTypeException('"limit" option', $options['limit'], 'integer');
     }
     if (isset($options['maxTimeMS']) && !is_integer($options['maxTimeMS'])) {
         throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
     }
     if (isset($options['skip']) && !is_integer($options['skip'])) {
         throw new InvalidArgumentTypeException('"skip" option', $options['skip'], 'integer');
     }
     $this->databaseName = (string) $databaseName;
     $this->collectionName = (string) $collectionName;
     $this->filter = $filter;
     $this->options = $options;
 }
コード例 #3
0
ファイル: Count.php プロジェクト: tigerjump/mongo-php-library
 /**
  * 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 new InvalidArgumentTypeException('$filter', $filter, 'array or object');
     }
     if (isset($options['hint'])) {
         if (is_array($options['hint']) || is_object($options['hint'])) {
             $options['hint'] = \MongoDB\generate_index_name($options['hint']);
         }
         if (!is_string($options['hint'])) {
             throw new InvalidArgumentTypeException('"hint" option', $options['hint'], 'string or array or object');
         }
     }
     if (isset($options['limit']) && !is_integer($options['limit'])) {
         throw new InvalidArgumentTypeException('"limit" option', $options['limit'], 'integer');
     }
     if (isset($options['maxTimeMS']) && !is_integer($options['maxTimeMS'])) {
         throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
     }
     if (isset($options['readConcern']) && !$options['readConcern'] instanceof ReadConcern) {
         throw new InvalidArgumentTypeException('"readConcern" option', $options['readConcern'], 'MongoDB\\Driver\\ReadConcern');
     }
     if (isset($options['readPreference']) && !$options['readPreference'] instanceof ReadPreference) {
         throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\\Driver\\ReadPreference');
     }
     if (isset($options['skip']) && !is_integer($options['skip'])) {
         throw new InvalidArgumentTypeException('"skip" option', $options['skip'], 'integer');
     }
     $this->databaseName = (string) $databaseName;
     $this->collectionName = (string) $collectionName;
     $this->filter = $filter;
     $this->options = $options;
 }
コード例 #4
0
 /**
  * Deletes an index from this collection
  *
  * @link http://www.php.net/manual/en/mongocollection.deleteindex.php
  * @param string|array $keys Field or fields from which to delete the index.
  * @return array Returns the database response.
  */
 public function deleteIndex($keys)
 {
     if (is_string($keys)) {
         $indexName = $keys;
     } elseif (is_array($keys)) {
         $indexName = \MongoDB\generate_index_name($keys);
     } else {
         throw new \InvalidArgumentException();
     }
     return TypeConverter::toLegacy($this->collection->dropIndex($indexName));
 }
コード例 #5
0
 /**
  * Deletes an index from this collection
  *
  * @link http://www.php.net/manual/en/mongocollection.deleteindex.php
  * @param string|array $keys Field or fields from which to delete the index.
  * @return array Returns the database response.
  */
 public function deleteIndex($keys)
 {
     if (is_string($keys)) {
         $indexName = $keys;
         if (!preg_match('#_-?1$#', $indexName)) {
             $indexName .= '_1';
         }
     } elseif (is_array($keys)) {
         $indexName = \MongoDB\generate_index_name($keys);
     } else {
         throw new \InvalidArgumentException();
     }
     try {
         return TypeConverter::toLegacy($this->collection->dropIndex($indexName));
     } catch (\MongoDB\Driver\Exception\Exception $e) {
         return ExceptionConverter::toResultArray($e) + ['nIndexesWas' => count($this->getIndexInfo())];
     }
 }