Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
39cf9182
Commit
39cf9182
authored
Apr 24, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-63: Use model class to validate index creation args
parent
3108162e
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
106 additions
and
37 deletions
+106
-37
Collection.php
src/Collection.php
+7
-37
IndexInput.php
src/Model/IndexInput.php
+99
-0
No files found.
src/Collection.php
View file @
39cf9182
...
@@ -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.
...
...
src/Model/IndexInput.php
0 → 100644
View file @
39cf9182
<?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
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment