Skip to content

Commit 3f7d58d

Browse files
committed
Create functions.php file for utility functions
1 parent 71b01ea commit 3f7d58d

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"ext-mongodb": ">=0.6.0"
1414
},
1515
"autoload": {
16-
"psr-4": { "MongoDB\\": "src/" }
16+
"psr-4": { "MongoDB\\": "src/" },
17+
"files": [ "src/functions.php" ]
1718
},
1819
"extra": {
1920
"branch-alias": {

src/functions.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace MongoDB;
4+
5+
use MongoDB\Exception\InvalidArgumentTypeException;
6+
7+
/**
8+
* Return whether the first key in the document starts with a "$" character.
9+
*
10+
* This is used for differentiating update and replacement documents.
11+
*
12+
* @internal
13+
* @param array|object $document Update or replacement document
14+
* @return boolean
15+
* @throws InvalidArgumentTypeException
16+
*/
17+
function is_first_key_operator($document)
18+
{
19+
if (is_object($document)) {
20+
$document = get_object_vars($document);
21+
}
22+
23+
if ( ! is_array($document)) {
24+
throw new InvalidArgumentTypeException('$document', $document, 'array or object');
25+
}
26+
27+
$firstKey = (string) key($document);
28+
29+
return (isset($firstKey[0]) && $firstKey[0] == '$');
30+
}
31+
32+
/**
33+
* Generate an index name from a key specification.
34+
*
35+
* @internal
36+
* @param array|object $document Document containing fields mapped to values,
37+
* which denote order or an index type
38+
* @return string
39+
* @throws InvalidArgumentTypeException
40+
*/
41+
function generate_index_name($document)
42+
{
43+
if (is_object($document)) {
44+
$document = get_object_vars($document);
45+
}
46+
47+
if ( ! is_array($document)) {
48+
throw new InvalidArgumentTypeException('$document', $document, 'array or object');
49+
}
50+
51+
$name = '';
52+
53+
foreach ($document as $field => $type) {
54+
$name .= ($name != '' ? '_' : '') . $field . '_' . $type;
55+
}
56+
57+
return $name;
58+
}
59+
60+
/**
61+
* Return whether the server supports a particular feature.
62+
*
63+
* @internal
64+
* @param Server $server Server to check
65+
* @param integer $feature Feature constant (i.e. wire protocol version)
66+
* @return boolean
67+
*/
68+
function server_supports_feature(Server $server, $feature)
69+
{
70+
$info = $server->getInfo();
71+
$maxWireVersion = isset($info['maxWireVersion']) ? (integer) $info['maxWireVersion'] : 0;
72+
$minWireVersion = isset($info['minWireVersion']) ? (integer) $info['minWireVersion'] : 0;
73+
74+
return ($minWireVersion <= $feature && $maxWireVersion >= $feature);
75+
}

tests/PedantryTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public function provideProjectClassNames()
6262
$files = new RegexIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($srcDir)), '/\.php$/i');
6363

6464
foreach ($files as $file) {
65+
if ($file->getFilename() === 'functions.php') {
66+
continue;
67+
}
68+
6569
$classNames[][] = 'MongoDB\\' . str_replace(DIRECTORY_SEPARATOR, '\\', substr($file->getRealPath(), strlen($srcDir) + 1, -4));
6670
}
6771

0 commit comments

Comments
 (0)