functions.php 5.16 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2015-2017 MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
17 18 19

namespace MongoDB;

20
use MongoDB\BSON\Serializable;
21
use MongoDB\Driver\ReadConcern;
22
use MongoDB\Driver\Server;
23
use MongoDB\Driver\WriteConcern;
24
use MongoDB\Exception\InvalidArgumentException;
25
use stdClass;
26

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/**
 * Applies a type map to a document.
 *
 * This function is used by operations where it is not possible to apply a type
 * map to the cursor directly because the root document is a command response
 * (e.g. findAndModify).
 *
 * @internal
 * @param array|object $document Document to which the type map will be applied
 * @param array        $typeMap  Type map for BSON deserialization.
 * @return array|object
 * @throws InvalidArgumentException
 */
function apply_type_map_to_document($document, array $typeMap)
{
    if ( ! is_array($document) && ! is_object($document)) {
        throw InvalidArgumentException::invalidType('$document', $document, 'array or object');
    }

    return \MongoDB\BSON\toPHP(\MongoDB\BSON\fromPHP($document), $typeMap);
}

49 50 51 52 53 54 55
/**
 * Generate an index name from a key specification.
 *
 * @internal
 * @param array|object $document Document containing fields mapped to values,
 *                               which denote order or an index type
 * @return string
56
 * @throws InvalidArgumentException
57 58 59
 */
function generate_index_name($document)
{
60 61 62 63
    if ($document instanceof Serializable) {
        $document = $document->bsonSerialize();
    }

64 65 66 67 68
    if (is_object($document)) {
        $document = get_object_vars($document);
    }

    if ( ! is_array($document)) {
69
        throw InvalidArgumentException::invalidType('$document', $document, 'array or object');
70 71 72 73 74 75 76 77 78 79 80
    }

    $name = '';

    foreach ($document as $field => $type) {
        $name .= ($name != '' ? '_' : '') . $field . '_' . $type;
    }

    return $name;
}

81 82 83 84 85 86 87 88
/**
 * Return whether the first key in the document starts with a "$" character.
 *
 * This is used for differentiating update and replacement documents.
 *
 * @internal
 * @param array|object $document Update or replacement document
 * @return boolean
89
 * @throws InvalidArgumentException
90 91 92
 */
function is_first_key_operator($document)
{
93 94 95 96
    if ($document instanceof Serializable) {
        $document = $document->bsonSerialize();
    }

97 98 99 100 101
    if (is_object($document)) {
        $document = get_object_vars($document);
    }

    if ( ! is_array($document)) {
102
        throw InvalidArgumentException::invalidType('$document', $document, 'array or object');
103 104
    }

105
    reset($document);
106 107
    $firstKey = (string) key($document);

108
    return (isset($firstKey[0]) && $firstKey[0] === '$');
109 110
}

111 112 113
/**
 * Return whether the aggregation pipeline ends with an $out operator.
 *
Jeremy Mikola's avatar
Jeremy Mikola committed
114
 * This is used for determining whether the aggregation pipeline must be
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
 * executed against a primary server.
 *
 * @internal
 * @param array $pipeline List of pipeline operations
 * @return boolean
 */
function is_last_pipeline_operator_out(array $pipeline)
{
    $lastOp = end($pipeline);

    if ($lastOp === false) {
        return false;
    }

    $lastOp = (array) $lastOp;

    return key($lastOp) === '$out';
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
/**
 * Return whether the "out" option for a mapReduce operation is "inline".
 *
 * This is used to determine if a mapReduce command requires a primary.
 *
 * @internal
 * @see https://docs.mongodb.com/manual/reference/command/mapReduce/#output-inline
 * @param string|array|object $out Output specification
 * @return boolean
 * @throws InvalidArgumentException
 */
function is_mapreduce_output_inline($out)
{
    if ( ! is_array($out) && ! is_object($out)) {
        return false;
    }

    if ($out instanceof Serializable) {
        $out = $out->bsonSerialize();
    }

    if (is_object($out)) {
        $out = get_object_vars($out);
    }

    if ( ! is_array($out)) {
        throw InvalidArgumentException::invalidType('$out', $out, 'array or object');
    }

    reset($out);

    return key($out) === 'inline';
}

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
/**
 * Return whether the server supports a particular feature.
 *
 * @internal
 * @param Server  $server  Server to check
 * @param integer $feature Feature constant (i.e. wire protocol version)
 * @return boolean
 */
function server_supports_feature(Server $server, $feature)
{
    $info = $server->getInfo();
    $maxWireVersion = isset($info['maxWireVersion']) ? (integer) $info['maxWireVersion'] : 0;
    $minWireVersion = isset($info['minWireVersion']) ? (integer) $info['minWireVersion'] : 0;

    return ($minWireVersion <= $feature && $maxWireVersion >= $feature);
}
184 185 186 187 188 189 190 191 192 193 194 195 196

function is_string_array($input) {
    if (!is_array($input)){
        return false;
    }
    foreach($input as $item) {
        if (!is_string($item)) {
            return false;
        }
    }
    return true;
}