DropDatabase.php 1.59 KB
Newer Older
1 2 3 4 5 6
<?php

namespace MongoDB\Operation;

use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
7
use MongoDB\Exception\InvalidArgumentException;
8 9 10 11 12 13 14 15 16 17 18 19

/**
 * Operation for the dropDatabase command.
 *
 * @api
 * @see MongoDB\Client::dropDatabase()
 * @see MongoDB\Database::drop()
 * @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
 */
class DropDatabase implements Executable
{
    private $databaseName;
20
    private $options;
21 22 23 24

    /**
     * Constructs a dropDatabase command.
     *
25 26 27 28 29
     * Supported options:
     *
     *  * typeMap (array): Type map for BSON deserialization. This will be used
     *    for the returned command result document.
     *
30
     * @param string $databaseName Database name
31
     * @param array  $options      Command options
32
     */
33
    public function __construct($databaseName, array $options = [])
34
    {
35 36 37 38
        if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
            throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
        }

39
        $this->databaseName = (string) $databaseName;
40
        $this->options = $options;
41 42 43 44 45 46 47
    }

    /**
     * Execute the operation.
     *
     * @see Executable::execute()
     * @param Server $server
48
     * @return array|object Command result document
49 50 51
     */
    public function execute(Server $server)
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
52
        $cursor = $server->executeCommand($this->databaseName, new Command(['dropDatabase' => 1]));
53

54 55 56 57
        if (isset($this->options['typeMap'])) {
            $cursor->setTypeMap($this->options['typeMap']);
        }

58
        return current($cursor->toArray());
59 60
    }
}