Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from b2pweb/feature-collation-on-query
Define options like collation on query
- Loading branch information
Showing
8 changed files
with
255 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
namespace Bdf\Prime\MongoDB\Query; | ||
|
||
/** | ||
* The query allow configuring options parameters | ||
* | ||
* Note: in mongo, options are not shared between write and querying, so be aware of the context when using it | ||
*/ | ||
interface OptionsConfigurable | ||
{ | ||
/** | ||
* Define an option for the current query | ||
* | ||
* @param string $name The option name | ||
* @param mixed $value The option value | ||
* | ||
* @return $this | ||
*/ | ||
public function option(string $name, $value); | ||
|
||
/** | ||
* Define the collation to use on the current query | ||
* | ||
* Collation allows users to specify language-specific rules for string comparison, | ||
* such as rules for lettercase and accent marks. | ||
* | ||
* To enable case-insensitive search or sort you can use `$query->collation(['locale' => 'en', 'strength' => 1])` | ||
* | ||
* This option is used by all query types | ||
* | ||
* @param array{ | ||
* locale: string, | ||
* strength?: int, | ||
* caseLevel?: bool, | ||
* caseFirst?: "upper"|"lower"|"off", | ||
* numericOrdering?: bool, | ||
* alternate?: "non-ignorable"|"shifted", | ||
* maxVariable?: "punct"|"space", | ||
* backwards?: bool, | ||
* normalization?: bool, | ||
* } $collation | ||
* @return mixed | ||
* | ||
* @see https://docs.mongodb.com/upcoming/reference/collation/#collation-document | ||
*/ | ||
public function collation(array $collation); | ||
|
||
/** | ||
* Index specification. | ||
* Specify either the index name as a string or the index key pattern. | ||
* If specified, then the query system will only consider plans using the hinted index. | ||
* | ||
* This option is used by all query types | ||
* | ||
* @param string|array|object $hint | ||
* @return $this | ||
*/ | ||
public function hint($hint); | ||
|
||
/** | ||
* Update only the first matching document if false, or all matching documents true. | ||
* This option cannot be true if newObj is a replacement document. | ||
* | ||
* This option is only supported by "update" query | ||
* | ||
* @param bool $flag | ||
* @return $this | ||
*/ | ||
public function multi(bool $flag = true); | ||
|
||
/** | ||
* If filter does not match an existing document, insert a single document. | ||
* The document will be created from newObj if it is a replacement document (i.e. no update operators); otherwise, | ||
* the operators in newObj will be applied to filter to create the new document. | ||
* | ||
* This option is only supported by "update" query | ||
* | ||
* @param bool $flag | ||
* @return $this | ||
*/ | ||
public function upsert(bool $flag = true); | ||
|
||
/** | ||
* An array of filter documents that determines which array elements to modify for an update operation on an array field. | ||
* | ||
* This option is only supported by "update" query | ||
* | ||
* @param array $filters | ||
* @return $this | ||
* | ||
* @see https://docs.mongodb.com/manual/reference/command/update/#update-command-arrayfilters | ||
*/ | ||
public function arrayFilters(array $filters); | ||
|
||
/** | ||
* Delete all matching documents (false), or only the first matching document (true) | ||
* | ||
* The configured option is "limit", which is in conflict with `Limitable::limit()` of the "select" query. | ||
* So use this option only on "delete" query | ||
* | ||
* This option is only supported by "delete" query | ||
* | ||
* @param bool $flag | ||
* @return $this | ||
*/ | ||
public function onlyDeleteFirst(bool $flag = true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Bdf\Prime\MongoDB\Query; | ||
|
||
/** | ||
* Implements @see OptionsConfigurable | ||
* | ||
* @psalm-require-implements OptionsConfigurable | ||
*/ | ||
trait OptionsTrait | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function option(string $name, $value) | ||
{ | ||
$this->statements['options'][$name] = $value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function collation(array $collation) | ||
{ | ||
return $this->option('collation', $collation); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hint($hint) | ||
{ | ||
return $this->option('hint', $hint); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function multi(bool $flag = true) | ||
{ | ||
return $this->option('multi', $flag); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function upsert(bool $flag = true) | ||
{ | ||
return $this->option('upsert', $flag); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function arrayFilters(array $filters) | ||
{ | ||
return $this->option('arrayFilters', $filters); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function onlyDeleteFirst(bool $flag = true) | ||
{ | ||
return $this->option('limit', $flag); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters