PageRenderTime 54ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/mongodb/mongodb/src/Operation/CreateCollection.php

https://gitlab.com/vnsoftdev/sna
PHP | 173 lines | 82 code | 26 blank | 65 comment | 32 complexity | b349af74fc2bc2abf2c48a95cfa99236 MD5 | raw file
  1. <?php
  2. namespace MongoDB\Operation;
  3. use MongoDB\Driver\Command;
  4. use MongoDB\Driver\Server;
  5. use MongoDB\Exception\InvalidArgumentException;
  6. /**
  7. * Operation for the create command.
  8. *
  9. * @api
  10. * @see MongoDB\Database::createCollection()
  11. * @see http://docs.mongodb.org/manual/reference/command/create/
  12. */
  13. class CreateCollection implements Executable
  14. {
  15. const USE_POWER_OF_2_SIZES = 1;
  16. const NO_PADDING = 2;
  17. private $databaseName;
  18. private $collectionName;
  19. private $options = [];
  20. /**
  21. * Constructs a create command.
  22. *
  23. * Supported options:
  24. *
  25. * * autoIndexId (boolean): Specify false to disable the automatic creation
  26. * of an index on the _id field. For replica sets, this option cannot be
  27. * false. The default is true.
  28. *
  29. * * capped (boolean): Specify true to create a capped collection. If set,
  30. * the size option must also be specified. The default is false.
  31. *
  32. * * flags (integer): Options for the MMAPv1 storage engine only. Must be a
  33. * bitwise combination CreateCollection::USE_POWER_OF_2_SIZES and
  34. * CreateCollection::NO_PADDING. The default is
  35. * CreateCollection::USE_POWER_OF_2_SIZES.
  36. *
  37. * * indexOptionDefaults (document): Default configuration for indexes when
  38. * creating the collection.
  39. *
  40. * * max (integer): The maximum number of documents allowed in the capped
  41. * collection. The size option takes precedence over this limit.
  42. *
  43. * * maxTimeMS (integer): The maximum amount of time to allow the query to
  44. * run.
  45. *
  46. * * size (integer): The maximum number of bytes for a capped collection.
  47. *
  48. * * storageEngine (document): Storage engine options.
  49. *
  50. * * typeMap (array): Type map for BSON deserialization. This will only be
  51. * used for the returned command result document.
  52. *
  53. * * validationAction (string): Validation action.
  54. *
  55. * * validationLevel (string): Validation level.
  56. *
  57. * * validator (document): Validation rules or expressions.
  58. *
  59. * @see http://source.wiredtiger.com/2.4.1/struct_w_t___s_e_s_s_i_o_n.html#a358ca4141d59c345f401c58501276bbb
  60. * @see https://docs.mongodb.org/manual/core/document-validation/
  61. * @param string $databaseName Database name
  62. * @param string $collectionName Collection name
  63. * @param array $options Command options
  64. * @throws InvalidArgumentException
  65. */
  66. public function __construct($databaseName, $collectionName, array $options = [])
  67. {
  68. if (isset($options['autoIndexId']) && ! is_bool($options['autoIndexId'])) {
  69. throw InvalidArgumentException::invalidType('"autoIndexId" option', $options['autoIndexId'], 'boolean');
  70. }
  71. if (isset($options['capped']) && ! is_bool($options['capped'])) {
  72. throw InvalidArgumentException::invalidType('"capped" option', $options['capped'], 'boolean');
  73. }
  74. if (isset($options['flags']) && ! is_integer($options['flags'])) {
  75. throw InvalidArgumentException::invalidType('"flags" option', $options['flags'], 'integer');
  76. }
  77. if (isset($options['indexOptionDefaults']) && ! is_array($options['indexOptionDefaults']) && ! is_object($options['indexOptionDefaults'])) {
  78. throw InvalidArgumentException::invalidType('"indexOptionDefaults" option', $options['indexOptionDefaults'], 'array or object');
  79. }
  80. if (isset($options['max']) && ! is_integer($options['max'])) {
  81. throw InvalidArgumentException::invalidType('"max" option', $options['max'], 'integer');
  82. }
  83. if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
  84. throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
  85. }
  86. if (isset($options['size']) && ! is_integer($options['size'])) {
  87. throw InvalidArgumentException::invalidType('"size" option', $options['size'], 'integer');
  88. }
  89. if (isset($options['storageEngine']) && ! is_array($options['storageEngine']) && ! is_object($options['storageEngine'])) {
  90. throw InvalidArgumentException::invalidType('"storageEngine" option', $options['storageEngine'], 'array or object');
  91. }
  92. if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
  93. throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
  94. }
  95. if (isset($options['validationAction']) && ! is_string($options['validationAction'])) {
  96. throw InvalidArgumentException::invalidType('"validationAction" option', $options['validationAction'], 'string');
  97. }
  98. if (isset($options['validationLevel']) && ! is_string($options['validationLevel'])) {
  99. throw InvalidArgumentException::invalidType('"validationLevel" option', $options['validationLevel'], 'string');
  100. }
  101. if (isset($options['validator']) && ! is_array($options['validator']) && ! is_object($options['validator'])) {
  102. throw InvalidArgumentException::invalidType('"validator" option', $options['validator'], 'array or object');
  103. }
  104. $this->databaseName = (string) $databaseName;
  105. $this->collectionName = (string) $collectionName;
  106. $this->options = $options;
  107. }
  108. /**
  109. * Execute the operation.
  110. *
  111. * @see Executable::execute()
  112. * @param Server $server
  113. * @return array|object Command result document
  114. */
  115. public function execute(Server $server)
  116. {
  117. $cursor = $server->executeCommand($this->databaseName, $this->createCommand());
  118. if (isset($this->options['typeMap'])) {
  119. $cursor->setTypeMap($this->options['typeMap']);
  120. }
  121. return current($cursor->toArray());
  122. }
  123. /**
  124. * Create the create command.
  125. *
  126. * @return Command
  127. */
  128. private function createCommand()
  129. {
  130. $cmd = ['create' => $this->collectionName];
  131. foreach (['autoIndexId', 'capped', 'flags', 'max', 'maxTimeMS', 'size', 'validationAction', 'validationLevel'] as $option) {
  132. if (isset($this->options[$option])) {
  133. $cmd[$option] = $this->options[$option];
  134. }
  135. }
  136. if (isset($this->options['indexOptionDefaults'])) {
  137. $cmd['indexOptionDefaults'] = (object) $this->options['indexOptionDefaults'];
  138. }
  139. if (isset($this->options['storageEngine'])) {
  140. $cmd['storageEngine'] = (object) $this->options['storageEngine'];
  141. }
  142. if (isset($this->options['validator'])) {
  143. $cmd['validator'] = (object) $this->options['validator'];
  144. }
  145. return new Command($cmd);
  146. }
  147. }