Skip to content

Commit

Permalink
Merge pull request #39 from xp-forge/refactor/options
Browse files Browse the repository at this point in the history
Refactor all methods to receive options varargs
  • Loading branch information
thekid authored Aug 19, 2023
2 parents e85a0a6 + 784b0ff commit 279b656
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 122 deletions.
78 changes: 39 additions & 39 deletions src/main/php/com/mongodb/Collection.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public function namespace() { return $this->database.'.'.$this->name; }
* @deprecated Use `run()` instead!
* @param string $name
* @param [:var] $params
* @param ?com.mongodb.Session $session
* @param com.mongodb.Options... $options
* @return var
* @throws com.mongodb.Error
*/
public function command($name, array $params= [], Session $session= null) {
return $this->proto->write($session, [$name => $this->name] + $params + ['$db' => $this->database])['body'];
public function command($name, array $params= [], Options... $options) {
return $this->proto->write($options, [$name => $this->name] + $params + ['$db' => $this->database])['body'];
}

/**
Expand All @@ -46,16 +46,16 @@ public function command($name, array $params= [], Session $session= null) {
* @param string $name
* @param [:var] $params
* @param string $semantics one of `read` or `write`
* @param ?com.mongodb.Session $session
* @param com.mongodb.Options... $options
* @return com.mongodb.result.Run
* @throws com.mongodb.Error
*/
public function run($name, array $params= [], $semantics= 'write', Session $session= null) {
public function run($name, array $params= [], $semantics= 'write', Options... $options) {
$commands= Commands::using($this->proto, $semantics);
return new Run(
$commands,
$session,
$commands->send($session, [$name => $this->name] + $params + ['$db' => $this->database])
$options,
$commands->send($options, [$name => $this->name] + $params + ['$db' => $this->database])
);
}

Expand All @@ -64,11 +64,11 @@ public function run($name, array $params= [], $semantics= 'write', Session $sess
* passed documents.
*
* @param com.mongodb.Document|com.mongodb.Document[] $arg
* @param ?com.mongodb.Session $session
* @param com.mongodb.Options... $options
* @return com.mongodb.result.Insert
* @throws com.mongodb.Error
*/
public function insert($arg, Session $session= null): Insert {
public function insert($arg, Options... $options): Insert {
$documents= is_array($arg) ? $arg : [$arg];

// See https://docs.mongodb.com/manual/reference/method/db.collection.insert/#id-field:
Expand All @@ -78,7 +78,7 @@ public function insert($arg, Session $session= null): Insert {
$ids[]= $document['_id'] ?? $document['_id']= ObjectId::create();
}

$result= $this->proto->write($session, [
$result= $this->proto->write($options, [
'insert' => $this->name,
'documents' => $documents,
'ordered' => true,
Expand All @@ -92,12 +92,12 @@ public function insert($arg, Session $session= null): Insert {
*
* @param string|com.mongodb.ObjectId|[:var] $query
* @param [:var]|com.mongodb.Document $arg Update operator expressions or document
* @param ?com.mongodb.Session $session
* @param com.mongodb.Options... $options
* @return com.mongodb.result.Update
* @throws com.mongodb.Error
*/
public function upsert($query, $arg, Session $session= null): Update {
$result= $this->proto->write($session, [
public function upsert($query, $arg, Options... $options): Update {
$result= $this->proto->write($options, [
'update' => $this->name,
'updates' => [[
'q' => $query instanceof ObjectId ? ['_id' => $query] : $query,
Expand All @@ -115,12 +115,12 @@ public function upsert($query, $arg, Session $session= null): Update {
*
* @param string|com.mongodb.ObjectId|[:var] $query
* @param [:var] $statements Update operator expressions
* @param ?com.mongodb.Session $session
* @param com.mongodb.Options... $options
* @return com.mongodb.result.Update
* @throws com.mongodb.Error
*/
public function update($query, $statements, Session $session= null): Update {
$result= $this->proto->write($session, [
public function update($query, $statements, Options... $options): Update {
$result= $this->proto->write($options, [
'update' => $this->name,
'updates' => [['u' => $statements] + (is_array($query)
? ['q' => $query, 'multi' => true]
Expand All @@ -136,12 +136,12 @@ public function update($query, $statements, Session $session= null): Update {
* Delete documents
*
* @param string|com.mongodb.ObjectId|[:var] $query
* @param ?com.mongodb.Session $session
* @param com.mongodb.Options... $options
* @return com.mongodb.result.Delete
* @throws com.mongodb.Error
*/
public function delete($query, Session $session= null): Delete {
$result= $this->proto->write($session, [
public function delete($query, Options... $options): Delete {
$result= $this->proto->write($options, [
'delete' => $this->name,
'deletes' => [is_array($query)
? ['q' => $query, 'limit' => 0]
Expand All @@ -157,31 +157,31 @@ public function delete($query, Session $session= null): Delete {
* Find documents in this collection
*
* @param string|com.mongodb.ObjectId|[:var] $query
* @param ?com.mongodb.Session $session
* @param com.mongodb.Options... $options
* @return com.mongodb.result.Cursor
* @throws com.mongodb.Error
*/
public function find($query= [], Session $session= null): Cursor {
public function find($query= [], Options... $options): Cursor {
$commands= Commands::reading($this->proto);
$result= $commands->send($session, [
$result= $commands->send($options, [
'find' => $this->name,
'filter' => is_array($query) ? ($query ?: (object)[]) : ['_id' => $query],
'$db' => $this->database,
]);
return new Cursor($commands, $session, $result['body']['cursor']);
return new Cursor($commands, $options, $result['body']['cursor']);
}

/**
* Count documents in this collection
*
* @param [:var] $filter
* @param ?com.mongodb.Session $session
* @param com.mongodb.Options... $options
* @return int
* @throws com.mongodb.Error
*/
public function count($filter= [], Session $session= null): int {
public function count($filter= [], Options... $options): int {
$count= ['$count' => 'n'];
$result= $this->proto->read($session, [
$result= $this->proto->read($options, [
'aggregate' => $this->name,
'pipeline' => $filter ? [['$match' => $filter], $count] : [$count],
'cursor' => (object)[],
Expand All @@ -195,13 +195,13 @@ public function count($filter= [], Session $session= null): int {
*
* @param string $key
* @param [:var] $filter
* @param ?com.mongodb.Session $session
* @param com.mongodb.Options... $options
* @return var[]
* @throws com.mongodb.Error
*/
public function distinct($key, $filter= [], Session $session= null): array {
public function distinct($key, $filter= [], Options... $options): array {
$distinct= ['$group' => ['_id' => 1, 'values' => ['$addToSet' => '$'.$key]]];
$result= $this->proto->read($session, [
$result= $this->proto->read($options, [
'aggregate' => $this->name,
'pipeline' => $filter ? [['$match' => $filter], $distinct] : [$distinct],
'cursor' => (object)[],
Expand All @@ -214,11 +214,11 @@ public function distinct($key, $filter= [], Session $session= null): array {
* Perfom aggregation over documents this collection
*
* @param [:var][] $pipeline
* @param ?com.mongodb.Session $session
* @param com.mongodb.Options... $options
* @return com.mongodb.result.Cursor
* @throws com.mongodb.Error
*/
public function aggregate(array $pipeline= [], Session $session= null): Cursor {
public function aggregate(array $pipeline= [], Options... $options): Cursor {
$sections= [
'aggregate' => $this->name,
'pipeline' => $pipeline,
Expand All @@ -239,30 +239,30 @@ public function aggregate(array $pipeline= [], Session $session= null): Cursor {
$commands= Commands::reading($this->proto);
}

$result= $commands->send($session, $sections);
return new Cursor($commands, $session, $result['body']['cursor']);
$result= $commands->send($options, $sections);
return new Cursor($commands, $options, $result['body']['cursor']);
}

/**
* Watch for changes in this collection
*
* @param [:var][] $pipeline
* @param [:var] $options
* @param ?com.mongodb.Session $session
* @param [:var] $params
* @param com.mongodb.Options... $options
* @return com.mongodb.result.ChangeStream
* @throws com.mongodb.Error
*/
public function watch(array $pipeline= [], array $options= [], Session $session= null): ChangeStream {
array_unshift($pipeline, ['$changeStream' => (object)$options]);
public function watch(array $pipeline= [], array $params= [], Options... $options): ChangeStream {
array_unshift($pipeline, ['$changeStream' => (object)$params]);

$commands= Commands::reading($this->proto);
$result= $commands->send($session, [
$result= $commands->send($options, [
'aggregate' => $this->name,
'pipeline' => $pipeline,
'cursor' => (object)[],
'$db' => $this->database,
]);
return new ChangeStream($commands, $session, $result['body']['cursor']);
return new ChangeStream($commands, $options, $result['body']['cursor']);
}

/** @return string */
Expand Down
20 changes: 10 additions & 10 deletions src/main/php/com/mongodb/Database.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,39 @@ public function collection(string $name): Collection {
/**
* Returns a list of database information objects
*
* @param ?com.mongodb.Session $session
* @param com.mongodb.Options... $options
* @return [:var][]
* @throws com.mongodb.Error
*/
public function collections(Session $session= null) {
public function collections(Options... $options) {
$commands= Commands::reading($this->proto);
$result= $commands->send($session, [
$result= $commands->send($options, [
'listCollections' => (object)[],
'$db' => $this->name
]);
return new Cursor($commands, $session, $result['body']['cursor']);
return new Cursor($commands, $options, $result['body']['cursor']);
}

/**
* Watch for changes in this database
*
* @param [:var][] $pipeline
* @param [:var] $options
* @param ?com.mongodb.Session $session
* @param [:var] $params
* @param com.mongodb.Options... $options
* @return com.mongodb.result.ChangeStream
* @throws com.mongodb.Error
*/
public function watch(array $pipeline= [], array $options= [], Session $session= null): ChangeStream {
array_unshift($pipeline, ['$changeStream' => (object)$options]);
public function watch(array $pipeline= [], array $params= [], Options... $options): ChangeStream {
array_unshift($pipeline, ['$changeStream' => (object)$params]);

$commands= Commands::reading($this->proto);
$result= $commands->send($session, [
$result= $commands->send($options, [
'aggregate' => 1,
'pipeline' => $pipeline,
'cursor' => (object)[],
'$db' => $this->name,
]);
return new ChangeStream($commands, $session, $result['body']['cursor']);
return new ChangeStream($commands, $options, $result['body']['cursor']);
}

/** @return string */
Expand Down
26 changes: 13 additions & 13 deletions src/main/php/com/mongodb/MongoConnection.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ public function connect(): self {
* @param string $name
* @param [:var] $arguments
* @param string $semantics one of `read` or `write`
* @param ?com.mongodb.Session $session
* @param com.mongodb.Options... $options
* @return com.mongodb.result.Run
* @throws com.mongodb.Error
*/
public function run($name, array $arguments= [], $semantics= 'write', Session $session= null) {
public function run($name, array $arguments= [], $semantics= 'write', Options... $options) {
$this->proto->connect();

$commands= Commands::using($this->proto, $semantics);
return new Run(
$commands,
$session,
$commands->send($session, [$name => 1] + $params + ['$db' => 'admin'])
$options,
$commands->send($options, [$name => 1] + $params + ['$db' => 'admin'])
);
}

Expand Down Expand Up @@ -120,11 +120,11 @@ public function collection(... $args): Collection {
*
* @see https://docs.mongodb.com/manual/reference/command/listDatabases/
* @param ?string|com.mongodb.Regex|[:string|com.mongodb.Regex] $filter
* @return ?com.mongodb.Session $session
* @param com.mongodb.Options... $options
* @return iterable
* @throws com.mongodb.Error
*/
public function databases($filter= null, $session= null) {
public function databases($filter= null, Options... $options) {
$this->proto->connect();

$request= ['listDatabases' => 1, '$db' => 'admin'];
Expand All @@ -136,7 +136,7 @@ public function databases($filter= null, $session= null) {
$request+= ['filter' => ['name' => $filter]];
}

foreach ($this->proto->read($session, $request)['body']['databases'] as $d) {
foreach ($this->proto->read($options, $request)['body']['databases'] as $d) {
yield $d['name'] => [
'name' => $d['name'],
'sizeOnDisk' => $d['sizeOnDisk'] instanceof Int64 ? $d['sizeOnDisk'] : new Int64($d['sizeOnDisk']),
Expand All @@ -150,24 +150,24 @@ public function databases($filter= null, $session= null) {
* Watch for changes in all databases.
*
* @param [:var][] $pipeline
* @param [:var] $options
* @param ?com.mongodb.Session $session
* @param [:var] $params
* @param com.mongodb.Options... $options
* @return com.mongodb.result.ChangeStream
* @throws com.mongodb.Error
*/
public function watch(array $pipeline= [], array $options= [], Session $session= null): ChangeStream {
public function watch(array $pipeline= [], array $params= [], Options... $options): ChangeStream {
$this->proto->connect();

array_unshift($pipeline, ['$changeStream' => ['allChangesForCluster' => true] + $options]);
array_unshift($pipeline, ['$changeStream' => ['allChangesForCluster' => true] + $params]);

$commands= Commands::reading($this->proto);
$result= $commands->send($session, [
$result= $commands->send($options, [
'aggregate' => 1,
'pipeline' => $pipeline,
'cursor' => (object)[],
'$db' => 'admin',
]);
return new ChangeStream($commands, $session, $result['body']['cursor']);
return new ChangeStream($commands, $options, $result['body']['cursor']);
}

/** @return string */
Expand Down
31 changes: 31 additions & 0 deletions src/main/php/com/mongodb/Options.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php namespace com\mongodb;

class Options {
protected $pairs;

/** @param [:var] */
public function __construct(array $pairs= []) {
$this->pairs= $pairs;
}

/**
* Sets read preference
*
* @param string $mode
* @return self
*/
public function readPreference($mode) {
$this->pairs['$readPreference']= ['mode' => $mode];
return $this;
}

/**
* Returns fields to be sent along with the command. Used by Protocol class.
*
* @param com.mongodb.io.Protocol
* @return [:var]
*/
public function send($proto) {
return $this->pairs;
}
}
Loading

0 comments on commit 279b656

Please sign in to comment.