CreateCollection.php 6.09 KB
Newer Older
1 2 3 4 5 6 7
<?php

namespace MongoDB\Operation;

use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
8
use MongoDB\Exception\InvalidArgumentTypeException;
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

/**
 * Operation for the create command.
 *
 * @api
 * @see MongoDB\Database::createCollection()
 * @see http://docs.mongodb.org/manual/reference/command/create/
 */
class CreateCollection implements Executable
{
    const USE_POWER_OF_2_SIZES = 1;
    const NO_PADDING = 2;

    private $databaseName;
    private $collectionName;
Jeremy Mikola's avatar
Jeremy Mikola committed
24
    private $options = [];
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

    /**
     * Constructs a create command.
     *
     * Supported options:
     *
     *  * autoIndexId (boolean): Specify false to disable the automatic creation
     *    of an index on the _id field. For replica sets, this option cannot be
     *    false. The default is true.
     *
     *  * capped (boolean): Specify true to create a capped collection. If set,
     *    the size option must also be specified. The default is false.
     *
     *  * flags (integer): Options for the MMAPv1 storage engine only. Must be a
     *    bitwise combination USE_POWER_OF_2_SIZES and NO_PADDING. The default
     *    is USE_POWER_OF_2_SIZES.
     *
42 43 44
     *  * indexOptionDefaults (document): Default configuration for indexes when
     *    creating the collection.
     *
45 46 47 48 49 50 51 52 53 54
     *  * max (integer): The maximum number of documents allowed in the capped
     *    collection. The size option takes precedence over this limit.
     *
     *  * maxTimeMS (integer): The maximum amount of time to allow the query to
     *    run.
     *
     *  * size (integer): The maximum number of bytes for a capped collection.
     *
     *  * storageEngine (document): Storage engine options.
     *
55 56 57 58 59 60
     *  * validationAction (string): Validation action.
     *
     *  * validationLevel (string): Validation level.
     *
     *  * validator (document): Validation rules or expressions.
     *
61
     * @see http://source.wiredtiger.com/2.4.1/struct_w_t___s_e_s_s_i_o_n.html#a358ca4141d59c345f401c58501276bbb
62
     * @see https://docs.mongodb.org/manual/core/document-validation/
63 64 65 66 67
     * @param string $databaseName   Database name
     * @param string $collectionName Collection name
     * @param array  $options        Command options
     * @throws InvalidArgumentException
     */
Jeremy Mikola's avatar
Jeremy Mikola committed
68
    public function __construct($databaseName, $collectionName, array $options = [])
69 70 71 72 73 74 75 76 77 78 79 80 81
    {
        if (isset($options['autoIndexId']) && ! is_bool($options['autoIndexId'])) {
            throw new InvalidArgumentTypeException('"autoIndexId" option', $options['autoIndexId'], 'boolean');
        }

        if (isset($options['capped']) && ! is_bool($options['capped'])) {
            throw new InvalidArgumentTypeException('"capped" option', $options['capped'], 'boolean');
        }

        if (isset($options['flags']) && ! is_integer($options['flags'])) {
            throw new InvalidArgumentTypeException('"flags" option', $options['flags'], 'integer');
        }

82 83 84 85
        if (isset($options['indexOptionDefaults']) && ! is_array($options['indexOptionDefaults']) && ! is_object($options['indexOptionDefaults'])) {
            throw new InvalidArgumentTypeException('"indexOptionDefaults" option', $options['indexOptionDefaults'], 'array or object');
        }

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        if (isset($options['max']) && ! is_integer($options['max'])) {
            throw new InvalidArgumentTypeException('"max" option', $options['max'], 'integer');
        }

        if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
            throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
        }

        if (isset($options['size']) && ! is_integer($options['size'])) {
            throw new InvalidArgumentTypeException('"size" option', $options['size'], 'integer');
        }

        if (isset($options['storageEngine']) && ! is_array($options['storageEngine']) && ! is_object($options['storageEngine'])) {
            throw new InvalidArgumentTypeException('"storageEngine" option', $options['storageEngine'], 'array or object');
        }

102 103 104 105 106 107 108 109 110 111 112 113
        if (isset($options['validationAction']) && ! is_string($options['validationAction'])) {
            throw new InvalidArgumentTypeException('"validationAction" option', $options['validationAction'], 'string');
        }

        if (isset($options['validationLevel']) && ! is_string($options['validationLevel'])) {
            throw new InvalidArgumentTypeException('"validationLevel" option', $options['validationLevel'], 'string');
        }

        if (isset($options['validator']) && ! is_array($options['validator']) && ! is_object($options['validator'])) {
            throw new InvalidArgumentTypeException('"validator" option', $options['validator'], 'array or object');
        }

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        $this->databaseName = (string) $databaseName;
        $this->collectionName = (string) $collectionName;
        $this->options = $options;
    }

    /**
     * Execute the operation.
     *
     * @see Executable::execute()
     * @param Server $server
     * @return object Command result document
     */
    public function execute(Server $server)
    {
        $cursor = $server->executeCommand($this->databaseName, $this->createCommand());

130
        return current($cursor->toArray());
131 132 133 134 135 136 137 138 139
    }

    /**
     * Create the create command.
     *
     * @return Command
     */
    private function createCommand()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
140
        $cmd = ['create' => $this->collectionName];
141

142
        foreach (['autoIndexId', 'capped', 'flags', 'max', 'maxTimeMS', 'size', 'validationAction', 'validationLevel'] as $option) {
143 144 145 146 147
            if (isset($this->options[$option])) {
                $cmd[$option] = $this->options[$option];
            }
        }

148
        if (isset($this->options['indexOptionDefaults'])) {
149 150 151
            $cmd['indexOptionDefaults'] = (object) $this->options['indexOptionDefaults'];
        }

152
        if (isset($this->options['storageEngine'])) {
153 154 155
            $cmd['storageEngine'] = (object) $this->options['storageEngine'];
        }

156 157 158 159
        if (isset($this->options['validator'])) {
            $cmd['validator'] = (object) $this->options['validator'];
        }

160 161 162
        return new Command($cmd);
    }
}