FindOneAndDelete.php 2.39 KB
Newer Older
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
<?php

namespace MongoDB\Operation;

use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;

/**
 * Operation for deleting a document with the findAndModify command.
 *
 * @api
 * @see MongoDB\Collection::findOneAndDelete()
 * @see http://docs.mongodb.org/manual/reference/command/findAndModify/
 */
class FindOneAndDelete implements Executable
{
    private $findAndModify;

    /**
     * Constructs a findAndModify command for deleting a document.
     *
     * Supported options:
     *
     *  * maxTimeMS (integer): The maximum amount of time to allow the query to
     *    run.
     *
     *  * projection (document): Limits the fields to return for the matching
     *    document.
     *
     *  * sort (document): Determines which document the operation modifies if
     *    the query selects multiple documents.
     *
34 35 36
     *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern. This option
     *    is only supported for server versions >= 3.2.
     *
37 38 39 40 41 42
     * @param string       $databaseName   Database name
     * @param string       $collectionName Collection name
     * @param array|object $filter         Query by which to filter documents
     * @param array        $options        Command options
     * @throws InvalidArgumentException
     */
Jeremy Mikola's avatar
Jeremy Mikola committed
43
    public function __construct($databaseName, $collectionName, $filter, array $options = [])
44 45
    {
        if ( ! is_array($filter) && ! is_object($filter)) {
46
            throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
47 48 49
        }

        if (isset($options['projection']) && ! is_array($options['projection']) && ! is_object($options['projection'])) {
50
            throw InvalidArgumentException::invalidType('"projection" option', $options['projection'], 'array or object');
51 52
        }

53 54
        if (isset($options['projection'])) {
            $options['fields'] = $options['projection'];
55 56
        }

57 58
        unset($options['projection']);

59 60 61
        $this->findAndModify = new FindAndModify(
            $databaseName,
            $collectionName,
Jeremy Mikola's avatar
Jeremy Mikola committed
62
            ['query' => $filter, 'remove' => true] + $options
63 64 65 66 67 68 69 70
        );
    }

    /**
     * Execute the operation.
     *
     * @see Executable::execute()
     * @param Server $server
71
     * @return object|null
72 73 74 75 76 77
     */
    public function execute(Server $server)
    {
        return $this->findAndModify->execute($server);
    }
}