Skip to content

Commit

Permalink
Merge pull request #62 from wollanup/develop
Browse files Browse the repository at this point in the history
Modifier doesn't throw Exception anymore
  • Loading branch information
wollanup authored Dec 7, 2018
2 parents 1d9c78a + 8ffe2e4 commit e05692f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
39 changes: 31 additions & 8 deletions src/Service/QueryModifier/Easy/Modifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class Modifier
* @var UseQueryFromDotNotation
*/
protected $dotUseQuery;
/**
* @var array List of failed filters
*/
protected $failures = [];
/**
* @var ModelCriteria
*/
Expand All @@ -45,12 +49,27 @@ public function filterBy(string $dotProperty, $value = null, $operator = null):

$property = $this->before($dotProperty);
$method = $this->buildMethodName(__FUNCTION__, $property);
$this->query = call_user_func([$this->query, $method], $value, $operator);
if ($this->methodExists($method)) {
$this->query = $this->query->{$method}($value, $operator);
} else {
$this->failures[] = $method;
}
$this->after();

return $this->query;
}

/**
* Determine if method is callable in Query class
*
* @param $method
* @return bool
*/
public function methodExists($method)
{
return method_exists($this->query, $method);
}

/**
* @param string $dotProperty
* @param $order
Expand All @@ -67,6 +86,16 @@ public function orderBy(string $dotProperty, $order = Criteria::ASC): ModelCrite
return $this->query;
}

/**
* List of failed method call
*
* @return array
*/
public function getFailures(): array
{
return $this->failures;
}

/**
* @throws UseQueryFromDotNotationException
*/
Expand Down Expand Up @@ -98,12 +127,6 @@ private function before(string $dotProperty)
*/
private function buildMethodName(string $action, string $property): string
{
# Determine if method is callable in Query class
$method = $action . $property;
if (!method_exists($this->query, $method)) {
throw new BadMethodCallException(sprintf('Call to undefined method: %s.', $method));
}

return $method;
return $action . $property;
}
}
3 changes: 1 addition & 2 deletions tests/Service/QueryModifier/Easy/ModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Propel\Generator\Util\QuickBuilder;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\ActiveQuery\ModelCriteria;
use Propel\Runtime\Exception\BadMethodCallException;
use Propel\Runtime\Propel;

class ModifierTest extends TestCase
Expand Down Expand Up @@ -62,8 +61,8 @@ public function testFilterByFailure()
{
$query = $this->mockQueryInstance();
$modifier = new Modifier($query);
$this->expectException(BadMethodCallException::class);
$modifier->filterBy('Foo');
$this->assertTrue(in_array('filterByFoo', $modifier->getFailures()));
}

/**
Expand Down

0 comments on commit e05692f

Please sign in to comment.