CollectionInfoLegacyIterator.php 2.67 KB
Newer Older
1 2 3 4 5
<?php

namespace MongoDB\Model;

use FilterIterator;
6 7 8
use Iterator;
use IteratorIterator;
use Traversable;
9

10 11 12 13 14 15 16 17 18
/**
 * 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
19
 * @see \MongoDB\Database::listCollections()
20 21 22 23
 * @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/
 */
24 25
class CollectionInfoLegacyIterator extends FilterIterator implements CollectionInfoIterator
{
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    /**
     * 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);
    }

43 44 45
    /**
     * Filter out internal or invalid collections.
     *
46
     * @see http://php.net/filteriterator.accept
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
     * @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;
    }
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

    /**
     * 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);
    }
95
}