|
| 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 | +} |
0 commit comments