IndexInput.php 3.12 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\Model;

20
use MongoDB\BSON\Serializable;
21
use MongoDB\Exception\InvalidArgumentException;
22 23 24 25 26 27 28
use function is_array;
use function is_float;
use function is_int;
use function is_object;
use function is_string;
use function MongoDB\generate_index_name;
use function sprintf;
29 30 31 32 33 34 35

/**
 * Index input model class.
 *
 * This class is used to validate user input for index creation.
 *
 * @internal
36
 * @see \MongoDB\Collection::createIndexes()
37 38 39 40 41 42 43 44
 * @see https://github.com/mongodb/specifications/blob/master/source/enumerate-indexes.rst
 * @see http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/
 */
class IndexInput implements Serializable
{
    private $index;

    /**
Jeremy Mikola's avatar
Jeremy Mikola committed
45 46 47
     * @param array $index Index specification
     * @throws InvalidArgumentException
     */
48 49
    public function __construct(array $index)
    {
50
        if (! isset($index['key'])) {
51 52 53
            throw new InvalidArgumentException('Required "key" document is missing from index specification');
        }

54
        if (! is_array($index['key']) && ! is_object($index['key'])) {
55
            throw InvalidArgumentException::invalidType('"key" option', $index['key'], 'array or object');
56 57
        }

58
        foreach ($index['key'] as $fieldName => $order) {
59
            if (! is_int($order) && ! is_float($order) && ! is_string($order)) {
60
                throw InvalidArgumentException::invalidType(sprintf('order value for "%s" field within "key" option', $fieldName), $order, 'numeric or string');
61 62 63
            }
        }

64
        if (! isset($index['ns'])) {
65 66 67
            throw new InvalidArgumentException('Required "ns" option is missing from index specification');
        }

68
        if (! is_string($index['ns'])) {
69
            throw InvalidArgumentException::invalidType('"ns" option', $index['ns'], 'string');
70 71
        }

72 73
        if (! isset($index['name'])) {
            $index['name'] = generate_index_name($index['key']);
74 75
        }

76
        if (! is_string($index['name'])) {
77
            throw InvalidArgumentException::invalidType('"name" option', $index['name'], 'string');
78 79 80 81 82 83
        }

        $this->index = $index;
    }

    /**
84
     * Return the index name.
85
     *
86
     * @return string
87
     */
88
    public function __toString()
89
    {
90
        return $this->index['name'];
91 92 93
    }

    /**
94
     * Serialize the index information to BSON for index creation.
95
     *
96
     * @see \MongoDB\Collection::createIndexes()
97 98
     * @see http://php.net/mongodb-bson-serializable.bsonserialize
     * @return array
99
     */
100
    public function bsonSerialize()
101
    {
102
        return $this->index;
103 104
    }
}