Skip to content

Commit d8e25ac

Browse files
committed
Move generate_index_name() to private method within IndexInput
1 parent d95e762 commit d8e25ac

File tree

4 files changed

+33
-42
lines changed

4 files changed

+33
-42
lines changed

src/Model/IndexInput.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
use function is_int;
2626
use function is_object;
2727
use function is_string;
28-
use function MongoDB\generate_index_name;
28+
use function MongoDB\document_to_array;
2929
use function sprintf;
3030

3131
/**
@@ -64,7 +64,7 @@ public function __construct(array $index)
6464
}
6565

6666
if (! isset($index['name'])) {
67-
$index['name'] = generate_index_name($index['key']);
67+
$index['name'] = $this->generateIndexName($index['key']);
6868
}
6969

7070
if (! is_string($index['name'])) {
@@ -92,4 +92,24 @@ public function bsonSerialize(): array
9292
{
9393
return $this->index;
9494
}
95+
96+
/**
97+
* Generate an index name from a key specification.
98+
*
99+
* @param array|object $document Document containing fields mapped to values,
100+
* which denote order or an index type
101+
* @throws InvalidArgumentException if $document is not an array or object
102+
*/
103+
private function generateIndexName($document): string
104+
{
105+
$document = document_to_array($document);
106+
107+
$name = '';
108+
109+
foreach ($document as $field => $type) {
110+
$name .= ($name !== '' ? '_' : '') . $field . '_' . $type;
111+
}
112+
113+
return $name;
114+
}
95115
}

src/functions.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,6 @@ function document_to_array($document): array
140140
return $document;
141141
}
142142

143-
/**
144-
* Generate an index name from a key specification.
145-
*
146-
* @internal
147-
* @param array|object $document Document containing fields mapped to values,
148-
* which denote order or an index type
149-
* @throws InvalidArgumentException if $document is not an array or object
150-
*/
151-
function generate_index_name($document): string
152-
{
153-
$document = document_to_array($document);
154-
155-
$name = '';
156-
157-
foreach ($document as $field => $type) {
158-
$name .= ($name != '' ? '_' : '') . $field . '_' . $type;
159-
}
160-
161-
return $name;
162-
}
163-
164143
/**
165144
* Return a collection's encryptedFields from the encryptedFieldsMap
166145
* autoEncryption driver option (if available).

tests/FunctionsTest.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use function MongoDB\apply_type_map_to_document;
1313
use function MongoDB\create_field_path_type_map;
1414
use function MongoDB\document_to_array;
15-
use function MongoDB\generate_index_name;
1615
use function MongoDB\is_first_key_operator;
1716
use function MongoDB\is_last_pipeline_operator_write;
1817
use function MongoDB\is_mapreduce_output_inline;
@@ -121,14 +120,6 @@ public function testDocumentToArrayArgumentTypeCheck($document): void
121120
document_to_array($document);
122121
}
123122

124-
/** @dataProvider provideDocumentCasts */
125-
public function testGenerateIndexName($cast): void
126-
{
127-
$this->assertSame('x_1', generate_index_name($cast(['x' => 1])));
128-
$this->assertSame('x_-1_y_1', generate_index_name($cast(['x' => -1, 'y' => 1])));
129-
$this->assertSame('x_2dsphere_y_1', generate_index_name($cast(['x' => '2dsphere', 'y' => 1])));
130-
}
131-
132123
public function provideDocumentCasts(): array
133124
{
134125
// phpcs:disable SlevomatCodingStandard.ControlStructures.JumpStatementsSpacing
@@ -143,13 +134,6 @@ public function provideDocumentCasts(): array
143134
// phpcs:enable
144135
}
145136

146-
/** @dataProvider provideInvalidDocumentValues */
147-
public function testGenerateIndexNameArgumentTypeCheck($document): void
148-
{
149-
$this->expectException(InvalidArgumentException::class);
150-
generate_index_name($document);
151-
}
152-
153137
/** @dataProvider provideDocumentCasts */
154138
public function testIsFirstKeyOperator(callable $cast): void
155139
{

tests/Model/IndexInputTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace MongoDB\Tests\Model;
44

5+
use MongoDB\BSON\Document;
56
use MongoDB\BSON\Serializable;
67
use MongoDB\Exception\InvalidArgumentException;
8+
use MongoDB\Model\BSONDocument;
79
use MongoDB\Model\IndexInput;
810
use MongoDB\Tests\TestCase;
911
use stdClass;
@@ -40,16 +42,22 @@ public function testConstructorShouldRequireNameToBeString(): void
4042
new IndexInput(['key' => ['x' => 1], 'name' => 1]);
4143
}
4244

43-
/** @dataProvider provideExpectedNameAndKey */
44-
public function testNameGeneration($expectedName, array $key): void
45+
/**
46+
* @dataProvider provideExpectedNameAndKey
47+
* @param array|object $key
48+
*/
49+
public function testNameGeneration($expectedName, $key): void
4550
{
4651
$this->assertSame($expectedName, (string) new IndexInput(['key' => $key]));
4752
}
4853

49-
public function provideExpectedNameAndKey()
54+
public function provideExpectedNameAndKey(): array
5055
{
5156
return [
5257
['x_1', ['x' => 1]],
58+
['x_1', (object) ['x' => 1]],
59+
['x_1', new BSONDocument(['x' => 1])],
60+
['x_1', Document::fromPHP(['x' => 1])],
5361
['x_1_y_-1', ['x' => 1, 'y' => -1]],
5462
['loc_2dsphere', ['loc' => '2dsphere']],
5563
['loc_2dsphere_x_1', ['loc' => '2dsphere', 'x' => 1]],

0 commit comments

Comments
 (0)