Skip to content

Commit bf6e781

Browse files
committed
FindAndModify commands
1 parent dec8371 commit bf6e781

18 files changed

+314
-74
lines changed

src/Command/FindAndModify.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class FindAndModify implements CommandInterface
99
{
1010
use Traits\PrimaryServerTrait;
1111

12+
const RETURN_DOCUMENT_BEFORE = 'before';
13+
const RETURN_DOCUMENT_AFTER = 'after';
14+
1215
/**
1316
* @var string
1417
*/
@@ -45,6 +48,8 @@ public function __construct($databaseName, $collectionName, array $query, array
4548

4649
public function execute(Manager $manager)
4750
{
48-
return $this->executeOnPrimaryServer($manager, $this->databaseName, $this->options);
51+
$options = ['findAndModify' => $this->collectionName, 'query' => $this->query] + $this->options;
52+
53+
return $this->executeOnPrimaryServer($manager, $this->databaseName, $options);
4954
}
5055
}

src/Command/FindOneAndDelete.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Tequila\MongoDB\Command;
4+
5+
use MongoDB\Driver\Manager;
6+
use Tequila\MongoDB\Command\Options\FindOneAndDeleteOptions;
7+
8+
class FindOneAndDelete implements CommandInterface
9+
{
10+
/**
11+
* @var string
12+
*/
13+
private $databaseName;
14+
15+
/**
16+
* @var string
17+
*/
18+
private $collectionName;
19+
20+
/**
21+
* @var array
22+
*/
23+
private $filter;
24+
25+
/**
26+
* @var array
27+
*/
28+
private $options;
29+
30+
/**
31+
* @param string $databaseName
32+
* @param string $collectionName
33+
* @param array $filter
34+
* @param array $options
35+
*/
36+
public function __construct($databaseName, $collectionName, array $filter, array $options = [])
37+
{
38+
$this->databaseName = $databaseName;
39+
$this->collectionName = $collectionName;
40+
$this->filter = $filter;
41+
$this->options = FindOneAndDeleteOptions::resolve($options);
42+
}
43+
44+
public function execute(Manager $manager)
45+
{
46+
$findAndModify = new FindAndModify(
47+
$this->databaseName,
48+
$this->collectionName,
49+
$this->filter,
50+
$this->options
51+
);
52+
53+
return $findAndModify->execute($manager);
54+
}
55+
}

src/Command/FindOneAndReplace.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Tequila\MongoDB\Command;
4+
5+
use MongoDB\Driver\Manager;
6+
use Tequila\MongoDB\Command\Options\FindOneAndUpdateOptions;
7+
8+
class FindOneAndReplace implements CommandInterface
9+
{
10+
/**
11+
* @var string
12+
*/
13+
private $databaseName;
14+
15+
/**
16+
* @var string
17+
*/
18+
private $collectionName;
19+
20+
/**
21+
* @var array
22+
*/
23+
private $filter;
24+
25+
/**
26+
* @var array|object
27+
*/
28+
private $replacement;
29+
30+
/**
31+
* @var array
32+
*/
33+
private $options;
34+
35+
/**
36+
* @param string $databaseName
37+
* @param string $collectionName
38+
* @param array $filter
39+
* @param array|object $replacement
40+
* @param array $options
41+
*/
42+
public function __construct($databaseName, $collectionName, array $filter, $replacement, array $options = [])
43+
{
44+
$this->databaseName = $databaseName;
45+
$this->collectionName = $collectionName;
46+
$this->filter = $filter;
47+
$this->replacement = $replacement;
48+
$this->options = ['update' => $this->replacement] + FindOneAndUpdateOptions::resolve($options);
49+
}
50+
51+
public function execute(Manager $manager)
52+
{
53+
$findAndModify = new FindAndModify(
54+
$this->databaseName,
55+
$this->collectionName,
56+
$this->filter,
57+
$this->options
58+
);
59+
60+
return $findAndModify->execute($manager);
61+
}
62+
}

src/Command/FindOneAndUpdate.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Tequila\MongoDB\Command;
4+
5+
use MongoDB\Driver\Manager;
6+
use Tequila\MongoDB\Command\Options\FindOneAndUpdateOptions;
7+
8+
class FindOneAndUpdate implements CommandInterface
9+
{
10+
/**
11+
* @var string
12+
*/
13+
private $databaseName;
14+
15+
/**
16+
* @var string
17+
*/
18+
private $collectionName;
19+
20+
/**
21+
* @var array
22+
*/
23+
private $filter;
24+
25+
/**
26+
* @var array|object
27+
*/
28+
private $update;
29+
30+
/**
31+
* @var array
32+
*/
33+
private $options;
34+
35+
/**
36+
* @param string $databaseName
37+
* @param string $collectionName
38+
* @param array $filter
39+
* @param $update
40+
* @param array $options
41+
*/
42+
public function __construct($databaseName, $collectionName, array $filter, $update, array $options = [])
43+
{
44+
$this->databaseName = $databaseName;
45+
$this->collectionName = $collectionName;
46+
$this->filter = $filter;
47+
$this->update = $update;
48+
$this->options = ['update' => $this->update] + FindOneAndUpdateOptions::resolve($options);
49+
}
50+
51+
public function execute(Manager $manager)
52+
{
53+
$findAndModify = new FindAndModify(
54+
$this->databaseName,
55+
$this->collectionName,
56+
$this->filter,
57+
$this->options
58+
);
59+
60+
return $findAndModify->execute($manager);
61+
}
62+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Tequila\MongoDB\Command\Options;
4+
5+
use Tequila\MongoDB\Options\OptionsInterface;
6+
use Tequila\MongoDB\Options\OptionsResolver;
7+
use Tequila\MongoDB\Options\Traits\CachedResolverTrait;
8+
9+
class FindOneAndDeleteOptions implements OptionsInterface
10+
{
11+
use CachedResolverTrait {
12+
CachedResolverTrait::resolve as resolveOptions;
13+
}
14+
15+
public static function configureOptions(OptionsResolver $resolver)
16+
{
17+
$resolver->setDefined([
18+
'maxTimeMS',
19+
'projection',
20+
'sort',
21+
'collation',
22+
]);
23+
}
24+
25+
/**
26+
* Translates FindOneAndDelete options to FindAndModify options
27+
*
28+
* @param array $options
29+
* @return array
30+
*/
31+
public static function resolve(array $options = [])
32+
{
33+
$options = self::resolveOptions($options);
34+
$options['remove'] = true;
35+
if (isset($options['projection'])) {
36+
$options['fields'] = $options['projection'];
37+
unset($options['projection']);
38+
}
39+
40+
return $options;
41+
}
42+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Tequila\MongoDB\Command\Options;
4+
5+
use Tequila\MongoDB\Command\FindAndModify;
6+
use Tequila\MongoDB\Options\OptionsInterface;
7+
use Tequila\MongoDB\Options\OptionsResolver;
8+
use Tequila\MongoDB\Options\Traits\CachedResolverTrait;
9+
10+
class FindOneAndUpdateOptions implements OptionsInterface
11+
{
12+
use CachedResolverTrait {
13+
CachedResolverTrait::resolve as resolveOptions;
14+
}
15+
16+
public static function configureOptions(OptionsResolver $resolver)
17+
{
18+
$resolver->setDefined([
19+
'bypassDocumentValidation',
20+
'maxTimeMS',
21+
'projection',
22+
'returnDocument',
23+
'sort',
24+
'upsert',
25+
'collation',
26+
]);
27+
28+
$resolver->setAllowedValues(
29+
'returnDocument',
30+
[
31+
FindAndModify::RETURN_DOCUMENT_BEFORE,
32+
FindAndModify::RETURN_DOCUMENT_AFTER,
33+
]
34+
);
35+
36+
$resolver->setDefault('returnDocument', FindAndModify::RETURN_DOCUMENT_BEFORE);
37+
}
38+
39+
public static function resolve(array $options = [])
40+
{
41+
$options = self::resolveOptions($options);
42+
43+
if (isset($options['projection'])) {
44+
$options['fields'] = $options['projection'];
45+
unset($options['projection']);
46+
}
47+
48+
if (FindAndModify::RETURN_DOCUMENT_AFTER === $options['returnDocument']) {
49+
$options['new'] = true;
50+
unset($options['returnDocument']);
51+
}
52+
53+
return $options;
54+
}
55+
}

src/Operation/Find.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,20 @@ private function configureOptions(OptionsResolver $resolver)
152152
'sort',
153153
]);
154154

155+
$documentTypes = ['array', 'object'];
156+
155157
$resolver
156158
->setAllowedTypes('allowPartialResults', 'bool')
157159
->setAllowedTypes('batchSize', 'integer')
158-
->setAllowedTypes('collation', 'string')
160+
->setAllowedTypes('collation', $documentTypes)
159161
->setAllowedTypes('comment', 'string')
160162
->setAllowedTypes('exhaust', 'bool')
161163
->setAllowedTypes('limit', 'integer')
162164
->setAllowedTypes('maxTimeMS', 'integer')
163-
->setAllowedTypes('modifiers', ['array', 'object'])
165+
->setAllowedTypes('modifiers', $documentTypes)
164166
->setAllowedTypes('noCursorTimeout', 'bool')
165167
->setAllowedTypes('oplogReplay', 'bool')
166-
->setAllowedTypes('projection', ['array', 'object'])
168+
->setAllowedTypes('projection', $documentTypes)
167169
->setAllowedTypes('readConcern', ReadConcern::class)
168170
->setAllowedTypes('readPreference', ReadPreference::class)
169171
->setAllowedTypes('skip', 'integer')

src/Write/Model/Traits/EnsureValidDocumentTrait.php renamed to src/Traits/EnsureValidDocumentTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tequila\MongoDB\Write\Model\Traits;
3+
namespace Tequila\MongoDB\Traits;
44

55
use MongoDB\BSON\Serializable;
66
use Tequila\MongoDB\Exception\InvalidArgumentException;

src/Write/Model/Traits/EnsureValidUpdateTrait.php renamed to src/Traits/EnsureValidUpdateTrait.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
<?php
22

3-
namespace Tequila\MongoDB\Write\Model\Traits;
3+
namespace Tequila\MongoDB\Traits;
44

55
use Tequila\MongoDB\Exception\InvalidArgumentException;
66
use Tequila\MongoDB\Options\OptionsResolver;
7-
use Tequila\MongoDB\Util\TypeUtil;
87
use Symfony\Component\OptionsResolver\Exception\InvalidArgumentException as OptionsResolverException;
98

109
trait EnsureValidUpdateTrait
1110
{
1211
private static $updateResolver;
1312

14-
public static function ensureValidUpdate($update)
13+
public static function ensureValidUpdate(array $update)
1514
{
16-
if (!is_array($update) && !is_object($update)) {
17-
throw new InvalidArgumentException(
18-
sprintf(
19-
'$update must be an array or an object, %s given',
20-
TypeUtil::getType($update)
21-
)
22-
);
23-
}
24-
25-
$update = (array) $update;
26-
2715
if (empty($update)) {
2816
throw new InvalidArgumentException('$update cannot be empty');
2917
}

src/Write/Model/DeleteMany.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77

88
class DeleteMany implements WriteModelInterface
99
{
10-
use Traits\EnsureValidFilterTrait;
1110
use Traits\BulkDeleteTrait;
1211

1312
/**
14-
* @param array|object $filter
13+
* @param array $filter
1514
* @param array $options
1615
*/
17-
public function __construct($filter, array $options = [])
16+
public function __construct(array $filter, array $options = [])
1817
{
19-
$this->ensureValidFilter($filter);
2018
if (isset($options['limit']) && 1 === $options['limit']) {
2119
throw new InvalidArgumentException(
2220
'DeleteMany operation does not allow option "limit" to be set to 1'

0 commit comments

Comments
 (0)