1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/*
* 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.
*/
namespace MongoDB\Model;
use FilterIterator;
use Iterator;
use IteratorIterator;
use Traversable;
/**
* CollectionInfoIterator for legacy "system.namespaces" query results.
*
* This iterator may be used to wrap a Cursor returned for queries on the
* "system.namespaces" collection. It includes logic to filter out internal
* collections and modify the collection name to be consistent with results from
* the listCollections command.
*
* @internal
* @see \MongoDB\Database::listCollections()
* @see https://github.com/mongodb/specifications/blob/master/source/enumerate-collections.rst
* @see http://docs.mongodb.org/manual/reference/command/listCollections/
* @see http://docs.mongodb.org/manual/reference/system-collections/
*/
class CollectionInfoLegacyIterator extends FilterIterator implements CollectionInfoIterator
{
/**
* Constructor.
*
* @param Traversable $iterator
*/
public function __construct(Traversable $iterator)
{
/* FilterIterator requires an Iterator, so wrap all other Traversables
* with an IteratorIterator as a convenience.
*/
if ( ! $iterator instanceof Iterator) {
$iterator = new IteratorIterator($iterator);
}
parent::__construct($iterator);
}
/**
* Filter out internal or invalid collections.
*
* @see http://php.net/filteriterator.accept
* @return boolean
*/
public function accept()
{
$info = parent::current();
if ( ! isset($info['name']) || ! is_string($info['name'])) {
return false;
}
// Reject names with "$" characters (e.g. indexes, oplog)
if (strpos($info['name'], '$') !== false) {
return false;
}
$firstDot = strpos($info['name'], '.');
/* Legacy collection names are a namespace and should be prefixed with
* the database name and a dot. Reject values that omit this prefix or
* are empty beyond it.
*/
if ($firstDot === false || $firstDot + 1 == strlen($info['name'])) {
return false;
}
return true;
}
/**
* Return the current element as a CollectionInfo instance.
*
* @see CollectionInfoIterator::current()
* @see http://php.net/iterator.current
* @return CollectionInfo
*/
public function current()
{
$info = parent::current();
// Trim the database prefix up to and including the first dot
$firstDot = strpos($info['name'], '.');
if ($firstDot !== false) {
$info['name'] = (string) substr($info['name'], $firstDot + 1);
}
return new CollectionInfo($info);
}
}