CollectionInfo.php 3.95 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 21 22
use MongoDB\Exception\BadMethodCallException;
use ArrayAccess;

23 24 25 26 27 28 29 30
/**
 * Collection information model class.
 *
 * This class models the collection information returned by the listCollections
 * command or, for legacy servers, queries on the "system.namespaces"
 * collection. It provides methods to access options for the collection.
 *
 * @api
31
 * @see \MongoDB\Database::listCollections()
32 33
 * @see https://github.com/mongodb/specifications/blob/master/source/enumerate-collections.rst
 */
34
class CollectionInfo implements ArrayAccess
35
{
36
    private $info;
37 38

    /**
Jeremy Mikola's avatar
Jeremy Mikola committed
39 40 41 42
     * Constructor.
     *
     * @param array $info Collection info
     */
43 44
    public function __construct(array $info)
    {
45
        $this->info = $info;
46 47
    }

48 49 50 51 52 53 54 55 56 57 58
    /**
     * Return the collection info as an array.
     *
     * @see http://php.net/oop5.magic#language.oop5.magic.debuginfo
     * @return array
     */
    public function __debugInfo()
    {
        return $this->info;
    }

59
    /**
60
     * Return the maximum number of documents to keep in the capped collection.
61
     *
62
     * @return integer|null
63
     */
64
    public function getCappedMax()
65
    {
66
        /* The MongoDB server might return this number as an integer or float */
67
        return isset($this->info['options']['max']) ? (integer) $this->info['options']['max'] : null;
68 69 70
    }

    /**
71
     * Return the maximum size (in bytes) of the capped collection.
72
     *
73
     * @return integer|null
74
     */
75
    public function getCappedSize()
76
    {
77
        /* The MongoDB server might return this number as an integer or float */
78
        return isset($this->info['options']['size']) ? (integer) $this->info['options']['size'] : null;
79 80 81
    }

    /**
82
     * Return the collection name.
83
     *
84
     * @return string
85
     */
86
    public function getName()
87
    {
88
        return (string) $this->info['name'];
89 90 91
    }

    /**
92
     * Return the collection options.
93
     *
94
     * @return array
95
     */
96
    public function getOptions()
97
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
98
        return isset($this->info['options']) ? (array) $this->info['options'] : [];
99 100 101
    }

    /**
102
     * Return whether the collection is a capped collection.
103
     *
104
     * @return boolean
105
     */
106
    public function isCapped()
107
    {
108
        return ! empty($this->info['options']['capped']);
109
    }
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

    /**
     * Check whether a field exists in the collection information.
     *
     * @see http://php.net/arrayaccess.offsetexists
     * @param mixed $key
     * @return boolean
     */
    public function offsetExists($key)
    {
        return array_key_exists($key, $this->info);
    }

    /**
     * Return the field's value from the collection information.
     *
     * @see http://php.net/arrayaccess.offsetget
     * @param mixed $key
     * @return mixed
     */
    public function offsetGet($key)
    {
        return $this->info[$key];
    }

    /**
     * Not supported.
     *
     * @see http://php.net/arrayaccess.offsetset
139 140
     * @param mixed $key
     * @param mixed $value
141 142 143 144 145 146 147 148 149 150 151
     * @throws BadMethodCallException
     */
    public function offsetSet($key, $value)
    {
        throw BadMethodCallException::classIsImmutable(__CLASS__);
    }

    /**
     * Not supported.
     *
     * @see http://php.net/arrayaccess.offsetunset
152
     * @param mixed $key
153 154 155 156 157 158
     * @throws BadMethodCallException
     */
    public function offsetUnset($key)
    {
        throw BadMethodCallException::classIsImmutable(__CLASS__);
    }
159
}