Skip to content

Commit

Permalink
Add Criteria::fromFindCriteria
Browse files Browse the repository at this point in the history
The goals is to be able to quick reproduce a criteria looking like parameters given to ORM find methods.
  • Loading branch information
soullivaneuh committed Jun 16, 2016
1 parent 46a476a commit f3d9832
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/Doctrine/Common/Collections/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ public static function create()
return new static();
}

/**
* Creates an instance with same behaviour as criteria parameters passed on ORM find methods.
*
* @param array $findCriteria
*
* @return Criteria
*/
public static function fromFindCriteria(array $findCriteria)
{
$criteria = static::create();

foreach ($findCriteria as $key => $value) {
$criteria->where(static::expr()->eq($key, $value));
}

return $criteria;
}

/**
* Returns the expression builder.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/Doctrine/Tests/Common/Collections/CriteriaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ public function testCreate()
$this->assertInstanceOf('Doctrine\Common\Collections\Criteria', $criteria);
}

public function testFromFindCriteria()
{
$criteria = Criteria::fromFindCriteria(array('name' => 'test', 'foo' => 42));

$this->assertInstanceOf('Doctrine\Common\Collections\Criteria', $criteria);
}

public function testConstructor()
{
$expr = new Comparison("field", "=", "value");
Expand Down

0 comments on commit f3d9832

Please sign in to comment.