Skip to content

Commit d173636

Browse files
committed
Copy \MongoDB\generate_index_name into our code
This function has been relocated to a private method in mongodb/mongo-php-library=1.16.0. See: - mongodb/mongo-php-library@bf36331 - mongodb/mongo-php-library#1092 We're copying it inline in our code here for compatibility with 1.16.0.
1 parent 4586d50 commit d173636

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

lib/Mongo/MongoCollection.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,37 @@ public function findOne($query = [], array $fields = [], array $options = [])
571571
return $document;
572572
}
573573

574+
/**
575+
* Generate an index name from a key specification.
576+
*
577+
* @internal
578+
* @param array|object $document Document containing fields mapped to values,
579+
* which denote order or an index type
580+
* @throws \MongoDB\Exception\InvalidArgumentException
581+
*/
582+
static private function generate_index_name($document): string
583+
{
584+
if ($document instanceof Serializable) {
585+
$document = $document->bsonSerialize();
586+
}
587+
588+
if (is_object($document)) {
589+
$document = get_object_vars($document);
590+
}
591+
592+
if (! is_array($document)) {
593+
throw \MongoDB\Exception\InvalidArgumentException::invalidType('$document', $document, 'array or object');
594+
}
595+
596+
$name = '';
597+
598+
foreach ($document as $field => $type) {
599+
$name .= ($name != '' ? '_' : '') . $field . '_' . $type;
600+
}
601+
602+
return $name;
603+
}
604+
574605
/**
575606
* Creates an index on the given field(s), or does nothing if the index already exists
576607
*
@@ -597,7 +628,7 @@ public function createIndex($keys, array $options = [])
597628
}
598629

599630
if (! isset($options['name'])) {
600-
$options['name'] = \MongoDB\generate_index_name($keys);
631+
$options['name'] = self::generate_index_name($keys);
601632
}
602633

603634
$indexes = iterator_to_array($this->collection->listIndexes());

0 commit comments

Comments
 (0)