<?php
 namespace MongoDB\Operation;
 use MongoDB\DeleteResult;
 use MongoDB\Driver\BulkWrite as Bulk;
 use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
 use MongoDB\Driver\Server;
 use MongoDB\Driver\Session;
 use MongoDB\Driver\WriteConcern;
 use MongoDB\Exception\InvalidArgumentException;
 use MongoDB\Exception\UnsupportedException;
 use function is_array;
 use function is_object;
 use function MongoDB\server_supports_feature;
 class Delete implements Executable, Explainable {
 private static $wireVersionForCollation = 5;
 private $databaseName;
 private $collectionName;
 private $filter;
 private $limit;
 private $options;
 public function __construct($databaseName, $collectionName, $filter, $limit, array $options = []) {
 if (! is_array($filter) && ! is_object($filter)) {
 throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
 }
 if ($limit !== 0 && $limit !== 1) {
 throw new InvalidArgumentException('$limit must be 0 or 1');
 }
 if (isset($options['collation']) && ! is_array($options['collation']) && ! is_object($options['collation'])) {
 throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'array or object');
 }
 if (isset($options['session']) && ! $options['session'] instanceof Session) {
 throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
 }
 if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
 throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class);
 }
 if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
 unset($options['writeConcern']);
 }
 $this->databaseName = (string) $databaseName;
 $this->collectionName = (string) $collectionName;
 $this->filter = $filter;
 $this->limit = $limit;
 $this->options = $options;
 }
 public function execute(Server $server) {
 if (isset($this->options['collation']) && ! server_supports_feature($server, self::$wireVersionForCollation)) {
 throw UnsupportedException::collationNotSupported();
 }
 $inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction();
 if ($inTransaction && isset($this->options['writeConcern'])) {
 throw UnsupportedException::writeConcernNotSupportedInTransaction();
 }
 $bulk = new Bulk();
 $bulk->delete($this->filter, $this->createDeleteOptions());
 $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $this->createExecuteOptions());
 return new DeleteResult($writeResult);
 }
 public function getCommandDocument(Server $server) {
 $cmd = ['delete' => $this->collectionName, 'deletes' => [['q' => $this->filter] + $this->createDeleteOptions()]];
 if (isset($this->options['writeConcern'])) {
 $cmd['writeConcern'] = $this->options['writeConcern'];
 }
 return $cmd;
 }
 private function createDeleteOptions() {
 $deleteOptions = ['limit' => $this->limit];
 if (isset($this->options['collation'])) {
 $deleteOptions['collation'] = (object) $this->options['collation'];
 }
 return $deleteOptions;
 }
 private function createExecuteOptions() {
 $options = [];
 if (isset($this->options['session'])) {
 $options['session'] = $this->options['session'];
 }
 if (isset($this->options['writeConcern'])) {
 $options['writeConcern'] = $this->options['writeConcern'];
 }
 return $options;
 }
 }
 