Skip to content

Commit

Permalink
Add an annotation @filter-default-field (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-1-anderson committed Oct 4, 2018
1 parent 54189ac commit 924bfa4
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/Cli/OpCommands.php
Expand Up @@ -34,6 +34,7 @@ public function parse($expr, $options = ['format' => 'yaml', 'dump' => false])
*
* @command edit
* @aliases ed
* @filter-default-field color
* @filter-output
* @return array
*/
Expand Down
2 changes: 1 addition & 1 deletion src/FactoryInterface.php
Expand Up @@ -17,5 +17,5 @@ interface FactoryInterface
* @param string $expression
* @return OperatorInterface
*/
public function evaluate($expression);
public function evaluate($expression, $defaultField = false);
}
10 changes: 6 additions & 4 deletions src/Hooks/FilterHooks.php
Expand Up @@ -17,11 +17,12 @@ class FilterHooks
public function filterOutput($result, CommandData $commandData)
{
$expr = $commandData->input()->getOption('filter');
$default_field = $commandData->annotationData()->get('filter-default-field');
if (!empty($expr)) {
$factory = LogicalOpFactory::get();
$op = $factory->evaluate($expr);
$op = $factory->evaluate($expr, $default_field);
$filter = new FilterOutputData();
$result = $this->wrapFilteredResult($filter->filter($result, $op), get_class($result));
$result = $this->wrapFilteredResult($filter->filter($result, $op), $result);
}

return $result;
Expand All @@ -31,11 +32,12 @@ public function filterOutput($result, CommandData $commandData)
* If the source data was wrapped in a marker class such
* as RowsOfFields, then re-apply the wrapper.
*/
protected function wrapFilteredResult($data, $sourceClass)
protected function wrapFilteredResult($data, $source)
{
if (!$sourceClass) {
if (!$source instanceof \ArrayObject) {
return $data;
}
$sourceClass = get_class($source);

return new $sourceClass($data);
}
Expand Down
4 changes: 2 additions & 2 deletions src/LogicalOpFactory.php
Expand Up @@ -38,15 +38,15 @@ public static function get()
* @param string $expression
* @return OperatorInterface
*/
public function evaluate($expression)
public function evaluate($expression, $default_field = false)
{
$exprSet = $this->splitByLogicOp($expression);
$result = false;

foreach ($exprSet as $exprWithLogicOp) {
$logicOp = $exprWithLogicOp[1];
$expr = $exprWithLogicOp[2];
$rhs = $this->factory->evaluate($expr);
$rhs = $this->factory->evaluate($expr, $default_field);
$result = $this->combineUsingLogicalOp($result, $logicOp, $rhs);
}

Expand Down
17 changes: 11 additions & 6 deletions src/OperatorFactory.php
Expand Up @@ -34,18 +34,18 @@ public function __construct()
* @param string $expression
* @return OperatorInterface
*/
public function evaluate($expression)
public function evaluate($expression, $defaultField = false)
{
if ($expression[0] == '!') {
$op = $this->evaluateNonNegated(substr($expression, 1));
$op = $this->evaluateNonNegated(substr($expression, 1), $defaultField);
return new NotOp($op);
}
return $this->evaluateNonNegated($expression);
return $this->evaluateNonNegated($expression, $defaultField);
}

protected function evaluateNonNegated($expression)
protected function evaluateNonNegated($expression, $defaultField = false)
{
list($key, $op, $comparitor) = $this->splitOnOperator($expression);
list($key, $op, $comparitor) = $this->splitOnOperator($expression, $defaultField);
if (empty($key) || empty($op)) {
throw new \Exception('Could not parse expression ' . $expression);
}
Expand Down Expand Up @@ -79,8 +79,13 @@ protected function instantiate($key, $op, $comparitor)
* @param string @expression
* @return array
*/
protected function splitOnOperator($expression)
protected function splitOnOperator($expression, $defaultField = false)
{
// If there is a default field, then any expression that is missing
// an operator will be interpreted as "default field contains value".
if (preg_match('#^[a-zA-Z0-9_.:-]+$#', $expression) && ($defaultField !== false)) {
return [$defaultField, '*=', $expression];
}
if (!preg_match('#([^!~*=]*)(!?~?\*?=)(.*)#', $expression, $matches)) {
return ['', '', ''];
}
Expand Down
7 changes: 7 additions & 0 deletions tests/OpCommandsTest.php
Expand Up @@ -69,6 +69,13 @@ public function exampleTestCommandParameters()
'edit', 'tests/fixtures/data.yml', '--filter=color=red',
],

[
'b:
color: blue
shape: square', self::STATUS_OK,
'edit', 'tests/fixtures/data.yml', '--filter=blue',
],

];
}

Expand Down

0 comments on commit 924bfa4

Please sign in to comment.