<?php
 namespace MongoDB\Operation;
 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 MongoDB\InsertOneResult;
 use function is_array;
 use function is_bool;
 use function is_object;
 use function MongoDB\server_supports_feature;
 class InsertOne implements Executable {
 private static $wireVersionForDocumentLevelValidation = 4;
 private $databaseName;
 private $collectionName;
 private $document;
 private $options;
 public function __construct($databaseName, $collectionName, $document, array $options = []) {
 if (! is_array($document) && ! is_object($document)) {
 throw InvalidArgumentException::invalidType('$document', $document, 'array or object');
 }
 if (isset($options['bypassDocumentValidation']) && ! is_bool($options['bypassDocumentValidation'])) {
 throw InvalidArgumentException::invalidType('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
 }
 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->document = $document;
 $this->options = $options;
 }
 public function execute(Server $server) {
 $options = [];
 $inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction();
 if (isset($this->options['writeConcern']) && $inTransaction) {
 throw UnsupportedException::writeConcernNotSupportedInTransaction();
 }
 if (! empty($this->options['bypassDocumentValidation']) && server_supports_feature($server, self::$wireVersionForDocumentLevelValidation) ) {
 $options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
 }
 $bulk = new Bulk($options);
 $insertedId = $bulk->insert($this->document);
 $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $this->createOptions());
 return new InsertOneResult($writeResult, $insertedId);
 }
 private function createOptions() {
 $options = [];
 if (isset($this->options['session'])) {
 $options['session'] = $this->options['session'];
 }
 if (isset($this->options['writeConcern'])) {
 $options['writeConcern'] = $this->options['writeConcern'];
 }
 return $options;
 }
 }
 