|
2 | 2 |
|
3 | 3 | namespace MongoDB; |
4 | 4 |
|
| 5 | +use MongoDB\BSON\Serializable; |
5 | 6 | use MongoDB\Driver\ReadConcern; |
6 | 7 | use MongoDB\Driver\Server; |
7 | 8 | use MongoDB\Exception\InvalidArgumentTypeException; |
8 | 9 | use stdClass; |
9 | 10 |
|
| 11 | +/** |
| 12 | + * Extracts an ID from an inserted document. |
| 13 | + * |
| 14 | + * This function is used when BulkWrite::insert() does not return a generated |
| 15 | + * ID, which means that the ID should be fetched from an array offset, public |
| 16 | + * property, or in the data returned by bsonSerialize(). |
| 17 | + * |
| 18 | + * @internal |
| 19 | + * @see https://jira.mongodb.org/browse/PHPC-382 |
| 20 | + * @param array|object $document Inserted document |
| 21 | + * @return mixed |
| 22 | + */ |
| 23 | +function extract_id_from_inserted_document($document) |
| 24 | +{ |
| 25 | + if ($document instanceof Serializable) { |
| 26 | + return extract_id_from_inserted_document($document->bsonSerialize()); |
| 27 | + } |
| 28 | + |
| 29 | + return is_array($document) ? $document['_id'] : $document->_id; |
| 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 | + |
10 | 60 | /** |
11 | 61 | * Return whether the first key in the document starts with a "$" character. |
12 | 62 | * |
@@ -55,34 +105,6 @@ function is_last_pipeline_operator_out(array $pipeline) |
55 | 105 | return key($lastOp) === '$out'; |
56 | 106 | } |
57 | 107 |
|
58 | | -/** |
59 | | - * Generate an index name from a key specification. |
60 | | - * |
61 | | - * @internal |
62 | | - * @param array|object $document Document containing fields mapped to values, |
63 | | - * which denote order or an index type |
64 | | - * @return string |
65 | | - * @throws InvalidArgumentTypeException |
66 | | - */ |
67 | | -function generate_index_name($document) |
68 | | -{ |
69 | | - if (is_object($document)) { |
70 | | - $document = get_object_vars($document); |
71 | | - } |
72 | | - |
73 | | - if ( ! is_array($document)) { |
74 | | - throw new InvalidArgumentTypeException('$document', $document, 'array or object'); |
75 | | - } |
76 | | - |
77 | | - $name = ''; |
78 | | - |
79 | | - foreach ($document as $field => $type) { |
80 | | - $name .= ($name != '' ? '_' : '') . $field . '_' . $type; |
81 | | - } |
82 | | - |
83 | | - return $name; |
84 | | -} |
85 | | - |
86 | 108 | /** |
87 | 109 | * Converts a ReadConcern instance to a stdClass for use in a BSON document. |
88 | 110 | * |
|
0 commit comments