Commit 39cf9182 authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-63: Use model class to validate index creation args

parent 3108162e
...@@ -14,7 +14,7 @@ use MongoDB\Exception\InvalidArgumentException; ...@@ -14,7 +14,7 @@ use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedTypeException; use MongoDB\Exception\UnexpectedTypeException;
use MongoDB\Model\IndexInfoIterator; use MongoDB\Model\IndexInfoIterator;
use MongoDB\Model\IndexInfoIteratorIterator; use MongoDB\Model\IndexInfoIteratorIterator;
use stdClass; use MongoDB\Model\IndexInput;
class Collection class Collection
{ {
...@@ -286,33 +286,20 @@ class Collection ...@@ -286,33 +286,20 @@ class Collection
* @see http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/ * @see http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/
* @param array $indexes List of index specifications * @param array $indexes List of index specifications
* @return string[] The names of the created indexes * @return string[] The names of the created indexes
* @throws InvalidArgumentException if an index specification does not * @throws InvalidArgumentException if an index specification is invalid
* contain a "key" document
* @throws UnexpectedTypeException if an index specification is not an array
* or a "key" document is not an array or
* object
*/ */
public function createIndexes(array $indexes) public function createIndexes(array $indexes)
{ {
foreach ($indexes as &$index) { foreach ($indexes as $i => $index) {
if ( ! is_array($index)) { if ( ! is_array($index)) {
throw new UnexpectedTypeException($index, 'array'); throw new UnexpectedTypeException($index, 'array');
} }
if ( ! isset($index['key'])) { if ( ! isset($index['ns'])) {
throw new InvalidArgumentException('Required "key" document is missing from index specification');
}
if ( ! is_array($index['key']) && ! is_object($index['key'])) {
throw new UnexpectedTypeException($index['key'], 'array or object');
}
$index['key'] = (object) $index['key'];
$index['ns'] = $this->ns; $index['ns'] = $this->ns;
if ( ! isset($index['name'])) {
$index['name'] = $this->generateIndexName($index['key']);
} }
$indexes[$i] = new IndexInput($index);
} }
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY); $readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
...@@ -327,7 +314,7 @@ class Collection ...@@ -327,7 +314,7 @@ class Collection
$this->createIndexesLegacy($server, $indexes); $this->createIndexesLegacy($server, $indexes);
} }
return array_map(function(array $index) { return $index['name']; }, $indexes); return array_map(function(IndexInput $index) { return (string) $index; }, $indexes);
} }
/** /**
...@@ -1248,23 +1235,6 @@ class Collection ...@@ -1248,23 +1235,6 @@ class Collection
$server->executeBulkWrite($this->dbname . '.system.indexes', $bulk); $server->executeBulkWrite($this->dbname . '.system.indexes', $bulk);
} }
/**
* Generates an index name from its key specification.
*
* @param object $key
* @return string
*/
private function generateIndexName(stdClass $key)
{
$name = '';
foreach ($key as $field => $type) {
$name .= ($name != '' ? '_' : '') . $field . '_' . $type;
}
return $name;
}
/** /**
* Returns information for all indexes for this collection using the * Returns information for all indexes for this collection using the
* listIndexes command. * listIndexes command.
......
<?php
namespace MongoDB\Model;
use BSON\Serializable;
/**
* Index input model class.
*
* This class is used to validate user input for index creation.
*
* @internal
* @see MongoDB\Collection::createIndexes()
* @see https://github.com/mongodb/specifications/blob/master/source/enumerate-indexes.rst
* @see http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/
*/
class IndexInput implements Serializable
{
private $index;
/**
* 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 UnexpectedTypeException($index['key'], 'array or object');
}
foreach ($index['key'] as $order) {
if ( ! is_int($order) && ! is_float($order) && ! is_string($order)) {
throw new UnexpectedTypeException($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 UnexpectedTypeException($index['ns'], 'string');
}
if ( ! isset($index['name'])) {
$index['name'] = $this->generateName($index['key']);
}
if ( ! is_string($index['name'])) {
throw new UnexpectedTypeException($index['name'], 'string');
}
$this->index = $index;
}
/**
* Serialize the index information to BSON for index creation.
*
* @see MongoDB\Collection::createIndexes()
* @see http://php.net/bson-serializable.bsonserialize
*/
public function bsonSerialize()
{
return $this->index;
}
/**
* Return the index name.
*
* @param string
*/
public function __toString()
{
return $this->index['name'];
}
/**
* Generates an index name from its key specification.
*
* @param array|object $key Document containing fields mapped to values,
* which denote order or an index type
* @return string
*/
private function generateName($key)
{
$name = '';
foreach ($key as $field => $type) {
$name .= ($name != '' ? '_' : '') . $field . '_' . $type;
}
return $name;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment