Differences Between: [Versions 310 and 311] [Versions 311 and 401] [Versions 39 and 311]
1 <?php 2 /* 3 * Copyright 2018 MongoDB, Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 namespace MongoDB\Operation; 19 20 use MongoDB\Driver\Command; 21 use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException; 22 use MongoDB\Driver\Server; 23 use MongoDB\Driver\Session; 24 use MongoDB\Driver\WriteConcern; 25 use MongoDB\Exception\InvalidArgumentException; 26 use MongoDB\Exception\UnsupportedException; 27 use function current; 28 use function is_array; 29 use function MongoDB\server_supports_feature; 30 31 /** 32 * Operation for the collMod command. 33 * 34 * @api 35 * @see \MongoDB\Database::modifyCollection() 36 * @see http://docs.mongodb.org/manual/reference/command/collMod/ 37 */ 38 class ModifyCollection implements Executable 39 { 40 /** @var string */ 41 private $databaseName; 42 43 /** @var string */ 44 private $collectionName; 45 46 /** @var array */ 47 private $collectionOptions; 48 49 /** @var array */ 50 private $options; 51 52 /** 53 * Constructs a collMod command. 54 * 55 * Supported options: 56 * 57 * * session (MongoDB\Driver\Session): Client session. 58 * 59 * Sessions are not supported for server versions < 3.6. 60 * 61 * * typeMap (array): Type map for BSON deserialization. This will only be 62 * used for the returned command result document. 63 * 64 * * writeConcern (MongoDB\Driver\WriteConcern): Write concern. 65 * 66 * This is not supported for server versions < 3.2 and will result in an 67 * exception at execution time if used. 68 * 69 * @param string $databaseName Database name 70 * @param string $collectionName Collection or view to modify 71 * @param array $collectionOptions Collection or view options to assign 72 * @param array $options Command options 73 * @throws InvalidArgumentException for parameter/option parsing errors 74 */ 75 public function __construct($databaseName, $collectionName, array $collectionOptions, array $options = []) 76 { 77 if (empty($collectionOptions)) { 78 throw new InvalidArgumentException('$collectionOptions is empty'); 79 } 80 81 if (isset($options['session']) && ! $options['session'] instanceof Session) { 82 throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class); 83 } 84 85 if (isset($options['typeMap']) && ! is_array($options['typeMap'])) { 86 throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array'); 87 } 88 89 if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) { 90 throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class); 91 } 92 93 if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) { 94 unset($options['writeConcern']); 95 } 96 97 $this->databaseName = (string) $databaseName; 98 $this->collectionName = (string) $collectionName; 99 $this->collectionOptions = $collectionOptions; 100 $this->options = $options; 101 } 102 103 /** 104 * Execute the operation. 105 * 106 * @see Executable::execute() 107 * @param Server $server 108 * @return array|object Command result document 109 * @throws DriverRuntimeException for other driver errors (e.g. connection errors) 110 */ 111 public function execute(Server $server) 112 { 113 if (isset($this->options['writeConcern']) && ! server_supports_feature($server, self::$wireVersionForWriteConcern)) { 114 throw UnsupportedException::writeConcernNotSupported(); 115 } 116 117 $cursor = $server->executeWriteCommand($this->databaseName, new Command(['collMod' => $this->collectionName] + $this->collectionOptions), $this->createOptions()); 118 119 if (isset($this->options['typeMap'])) { 120 $cursor->setTypeMap($this->options['typeMap']); 121 } 122 123 return current($cursor->toArray()); 124 } 125 126 /** 127 * Create options for executing the command. 128 * 129 * @see http://php.net/manual/en/mongodb-driver-server.executewritecommand.php 130 * @return array 131 */ 132 private function createOptions() 133 { 134 $options = []; 135 136 if (isset($this->options['session'])) { 137 $options['session'] = $this->options['session']; 138 } 139 140 if (isset($this->options['writeConcern'])) { 141 $options['writeConcern'] = $this->options['writeConcern']; 142 } 143 144 return $options; 145 } 146 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body