Skip to content

Commit

Permalink
Merge pull request #28 from jakalofnaar/exec-query-builders
Browse files Browse the repository at this point in the history
Query builders usable by Collection methods
  • Loading branch information
bcrowe committed Oct 11, 2015
2 parents 50ed37a + d5e689f commit 0d999a5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 25 deletions.
62 changes: 37 additions & 25 deletions src/League/Monga/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,21 +220,25 @@ public function listIndexes()
public function remove($criteria, $options = [])
{
if ($criteria instanceof Closure) {
$callback = $criteria;

// Create new Remove query
$query = new Query\Remove();
$criteria = new Query\Remove();

// Set the given options
$query->setOptions($options);
$criteria->setOptions($options);

// Execute the callback
$criteria($query);

// Retrieve the where filter
$criteria = $query->getWhere();
$callback($criteria);
}

if ($criteria instanceof Query\Remove) {
// Retrieve the options, these might
// have been altered in the closure.
$options = $query->getOptions();
$options = $criteria->getOptions();

// Retrieve the where filter
$criteria = $criteria->getWhere();
}

if (! is_array($criteria)) {
Expand Down Expand Up @@ -271,7 +275,7 @@ public function remove($criteria, $options = [])
/**
* Finds documents.
*
* @param mixed $query configuration closure, raw mongo conditions array
* @param mixed $query find query, configuration closure, raw mongo conditions array
* @param array $fields associative array for field exclusion/inclusion
* @param boolean $findOne whether to find one or multiple
*
Expand All @@ -282,17 +286,21 @@ public function find($query = [], $fields = [], $findOne = false)
$postFind = false;

if ($query instanceof Closure) {
$find = new Query\Find();
$callback = $query;

$query = new Query\Find();

// set the fields to select
$find->fields($fields);
$find->one($findOne);
$query($find);

$findOne = $find->getFindOne();
$fields = $find->getFields();
$query = $find->getWhere();
$postFind = $find->getPostFindActions();
$query->fields($fields);
$query->one($findOne);
$callback($query);
}

if ($query instanceof Query\Find) {
$findOne = $query->getFindOne();
$fields = $query->getFields();
$postFind = $query->getPostFindActions();
$query = $query->getWhere();
}

if (! is_array($query) || ! is_array($fields)) {
Expand Down Expand Up @@ -334,7 +342,7 @@ public function find($query = [], $fields = [], $findOne = false)
/**
* Finds a single documents.
*
* @param mixed $query configuration closure, raw mongo conditions array
* @param mixed $query find query, configuration closure, raw mongo conditions array
* @param array $fields associative array for field exclusion/inclusion
*
* @return array|null document array when found, null when not found
Expand Down Expand Up @@ -427,7 +435,7 @@ public function insert(array $data, $options = [])
/**
* Updates a collection
*
* @param mixed $values update array or callback
* @param mixed $values update query, array or callback
* @param mixed $query update filter
* @param array $options update options
*
Expand All @@ -436,13 +444,17 @@ public function insert(array $data, $options = [])
public function update($values = [], $query = null, $options = [])
{
if ($values instanceof Closure) {
$query = new Query\Update();
$query->setOptions($options);
$values($query);
$callback = $values;

$options = $query->getOptions();
$values = $query->getUpdate();
$query = $query->getWhere();
$values = new Query\Update();
$values->setOptions($options);
$callback($values);
}

if ($values instanceof Query\Update) {
$options = $values->getOptions();
$query = $values->getWhere();
$values = $values->getUpdate();
}

if (! is_array($values) || ! is_array($options)) {
Expand Down
31 changes: 31 additions & 0 deletions tests/CollectionTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@ public function testRemoveWhereClosure()
$this->assertEquals(0, $this->collection->count());
}

public function testRemoveWithQuery()
{
$where = new League\Monga\Query\Remove();

$where->where('name', 'Frank');

$this->collection->getCollection()->insert(['name' => 'Frank']);
$this->assertEquals(1, $this->collection->count());
$result = $this->collection->remove($where);
$this->assertTrue($result);
$this->assertEquals(0, $this->collection->count());
}

/**
* @expectedException InvalidArgumentException
*/
Expand Down Expand Up @@ -243,6 +256,14 @@ public function testFind()
$this->assertInstanceOf('League\Monga\Cursor', $result);
}

public function testFindWithQuery()
{
$query = new League\Monga\Query\Find();
$result = $this->collection->find($query);

$this->assertInstanceOf('League\Monga\Cursor', $result);
}

public function testFindOneEmpty()
{
$result = $this->collection->findOne();
Expand Down Expand Up @@ -371,6 +392,16 @@ public function testUpdateClosure()
$this->assertTrue($result);
}

public function testUpdateWithQuery()
{
$query = new League\Monga\Query\Update();

$query->set('name', 'changed');

$result = $this->collection->update($query);
$this->assertTrue($result);
}

/**
* @expectedException InvalidArgumentException
*/
Expand Down

0 comments on commit 0d999a5

Please sign in to comment.