-
Notifications
You must be signed in to change notification settings - Fork 266
PHPLIB-869: Support viewOn and pipeline options in createCollection helper #935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
d4d22cf
1516146
8daa27c
c64cdf9
93add22
79000d6
0f89fe8
ae4718b
fef9a9b
49231c4
57212a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
use function is_integer; | ||
use function is_object; | ||
use function is_string; | ||
use function sprintf; | ||
use function trigger_error; | ||
|
||
use const E_USER_DEPRECATED; | ||
|
@@ -100,6 +101,10 @@ class CreateCollection implements Executable | |
* * maxTimeMS (integer): The maximum amount of time to allow the query to | ||
* run. | ||
* | ||
* * pipeline (array): An array that consists of the aggregation pipeline | ||
* stage(s), which will be applied to the collection or view specified by | ||
* viewOn. | ||
* | ||
* * session (MongoDB\Driver\Session): Client session. | ||
* | ||
* * size (integer): The maximum number of bytes for a capped collection. | ||
|
@@ -119,6 +124,9 @@ class CreateCollection implements Executable | |
* | ||
* * validator (document): Validation rules or expressions. | ||
* | ||
* * viewOn (string): The name of the source collection or view from which | ||
* to create the view. | ||
* | ||
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern. | ||
* | ||
* @see https://source.wiredtiger.com/2.4.1/struct_w_t___s_e_s_s_i_o_n.html#a358ca4141d59c345f401c58501276bbb | ||
|
@@ -170,6 +178,10 @@ public function __construct($databaseName, $collectionName, array $options = []) | |
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer'); | ||
} | ||
|
||
if (isset($options['pipeline']) && ! is_array($options['pipeline'])) { | ||
throw InvalidArgumentException::invalidType('"pipeline" option', $options['pipeline'], 'array'); | ||
} | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (isset($options['session']) && ! $options['session'] instanceof Session) { | ||
throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class); | ||
} | ||
|
@@ -202,6 +214,10 @@ public function __construct($databaseName, $collectionName, array $options = []) | |
throw InvalidArgumentException::invalidType('"validator" option', $options['validator'], 'array or object'); | ||
} | ||
|
||
if (isset($options['viewOn']) && ! is_string($options['viewOn'])) { | ||
throw InvalidArgumentException::invalidType('"viewOn" option', $options['viewOn'], 'string'); | ||
} | ||
|
||
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) { | ||
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class); | ||
} | ||
|
@@ -214,6 +230,22 @@ public function __construct($databaseName, $collectionName, array $options = []) | |
trigger_error('The "autoIndexId" option is deprecated and will be removed in a future release', E_USER_DEPRECATED); | ||
} | ||
|
||
if (isset($options['pipeline'])) { | ||
$expectedIndex = 0; | ||
|
||
foreach ($options['pipeline'] as $i => $operation) { | ||
if ($i !== $expectedIndex) { | ||
throw new InvalidArgumentException(sprintf('The "pipeline" option is not a list (unexpected index: "%s")', $i)); | ||
} | ||
|
||
if (! is_array($operation) && ! is_object($operation)) { | ||
throw InvalidArgumentException::invalidType(sprintf('$options["pipeline"][%d]', $i), $operation, 'array or object'); | ||
} | ||
|
||
$expectedIndex += 1; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was copied from Aggregate.php, but the exception messages were not updated to reflect that I also created PHPLIB-881 to refactor this to share common code with other instances of pipeline validation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code updated in #1078 |
||
|
||
$this->databaseName = (string) $databaseName; | ||
$this->collectionName = (string) $collectionName; | ||
$this->options = $options; | ||
|
@@ -247,7 +279,7 @@ private function createCommand() | |
{ | ||
$cmd = ['create' => $this->collectionName]; | ||
|
||
foreach (['autoIndexId', 'capped', 'expireAfterSeconds', 'flags', 'max', 'maxTimeMS', 'size', 'validationAction', 'validationLevel'] as $option) { | ||
foreach (['autoIndexId', 'capped', 'expireAfterSeconds', 'flags', 'max', 'maxTimeMS', 'pipeline', 'size', 'validationAction', 'validationLevel', 'viewOn'] as $option) { | ||
if (isset($this->options[$option])) { | ||
$cmd[$option] = $this->options[$option]; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.