CollectionInfo.php 3.71 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
        return isset($this->info['options']['max']) ? (integer) $this->info['options']['max'] : null;
67 68 69
    }

    /**
70
     * Return the maximum size (in bytes) of the capped collection.
71
     *
72
     * @return integer|null
73
     */
74
    public function getCappedSize()
75
    {
76
        return isset($this->info['options']['size']) ? (integer) $this->info['options']['size'] : null;
77 78 79
    }

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

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

    /**
100
     * Return whether the collection is a capped collection.
101
     *
102
     * @return boolean
103
     */
104
    public function isCapped()
105
    {
106
        return ! empty($this->info['options']['capped']);
107
    }
108 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

    /**
     * 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
     * @throws BadMethodCallException
     */
    public function offsetSet($key, $value)
    {
        throw BadMethodCallException::classIsImmutable(__CLASS__);
    }

    /**
     * Not supported.
     *
     * @see http://php.net/arrayaccess.offsetunset
     * @throws BadMethodCallException
     */
    public function offsetUnset($key)
    {
        throw BadMethodCallException::classIsImmutable(__CLASS__);
    }
154
}