Skip to content

Commit 924bfa4

Browse files
Add an annotation @filter-default-field (#20)
1 parent 54189ac commit 924bfa4

File tree

6 files changed

+28
-13
lines changed

6 files changed

+28
-13
lines changed

src/Cli/OpCommands.php

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function parse($expr, $options = ['format' => 'yaml', 'dump' => false])
3434
*
3535
* @command edit
3636
* @aliases ed
37+
* @filter-default-field color
3738
* @filter-output
3839
* @return array
3940
*/

src/FactoryInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ interface FactoryInterface
1717
* @param string $expression
1818
* @return OperatorInterface
1919
*/
20-
public function evaluate($expression);
20+
public function evaluate($expression, $defaultField = false);
2121
}

src/Hooks/FilterHooks.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ class FilterHooks
1717
public function filterOutput($result, CommandData $commandData)
1818
{
1919
$expr = $commandData->input()->getOption('filter');
20+
$default_field = $commandData->annotationData()->get('filter-default-field');
2021
if (!empty($expr)) {
2122
$factory = LogicalOpFactory::get();
22-
$op = $factory->evaluate($expr);
23+
$op = $factory->evaluate($expr, $default_field);
2324
$filter = new FilterOutputData();
24-
$result = $this->wrapFilteredResult($filter->filter($result, $op), get_class($result));
25+
$result = $this->wrapFilteredResult($filter->filter($result, $op), $result);
2526
}
2627

2728
return $result;
@@ -31,11 +32,12 @@ public function filterOutput($result, CommandData $commandData)
3132
* If the source data was wrapped in a marker class such
3233
* as RowsOfFields, then re-apply the wrapper.
3334
*/
34-
protected function wrapFilteredResult($data, $sourceClass)
35+
protected function wrapFilteredResult($data, $source)
3536
{
36-
if (!$sourceClass) {
37+
if (!$source instanceof \ArrayObject) {
3738
return $data;
3839
}
40+
$sourceClass = get_class($source);
3941

4042
return new $sourceClass($data);
4143
}

src/LogicalOpFactory.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ public static function get()
3838
* @param string $expression
3939
* @return OperatorInterface
4040
*/
41-
public function evaluate($expression)
41+
public function evaluate($expression, $default_field = false)
4242
{
4343
$exprSet = $this->splitByLogicOp($expression);
4444
$result = false;
4545

4646
foreach ($exprSet as $exprWithLogicOp) {
4747
$logicOp = $exprWithLogicOp[1];
4848
$expr = $exprWithLogicOp[2];
49-
$rhs = $this->factory->evaluate($expr);
49+
$rhs = $this->factory->evaluate($expr, $default_field);
5050
$result = $this->combineUsingLogicalOp($result, $logicOp, $rhs);
5151
}
5252

src/OperatorFactory.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ public function __construct()
3434
* @param string $expression
3535
* @return OperatorInterface
3636
*/
37-
public function evaluate($expression)
37+
public function evaluate($expression, $defaultField = false)
3838
{
3939
if ($expression[0] == '!') {
40-
$op = $this->evaluateNonNegated(substr($expression, 1));
40+
$op = $this->evaluateNonNegated(substr($expression, 1), $defaultField);
4141
return new NotOp($op);
4242
}
43-
return $this->evaluateNonNegated($expression);
43+
return $this->evaluateNonNegated($expression, $defaultField);
4444
}
4545

46-
protected function evaluateNonNegated($expression)
46+
protected function evaluateNonNegated($expression, $defaultField = false)
4747
{
48-
list($key, $op, $comparitor) = $this->splitOnOperator($expression);
48+
list($key, $op, $comparitor) = $this->splitOnOperator($expression, $defaultField);
4949
if (empty($key) || empty($op)) {
5050
throw new \Exception('Could not parse expression ' . $expression);
5151
}
@@ -79,8 +79,13 @@ protected function instantiate($key, $op, $comparitor)
7979
* @param string @expression
8080
* @return array
8181
*/
82-
protected function splitOnOperator($expression)
82+
protected function splitOnOperator($expression, $defaultField = false)
8383
{
84+
// If there is a default field, then any expression that is missing
85+
// an operator will be interpreted as "default field contains value".
86+
if (preg_match('#^[a-zA-Z0-9_.:-]+$#', $expression) && ($defaultField !== false)) {
87+
return [$defaultField, '*=', $expression];
88+
}
8489
if (!preg_match('#([^!~*=]*)(!?~?\*?=)(.*)#', $expression, $matches)) {
8590
return ['', '', ''];
8691
}

tests/OpCommandsTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ public function exampleTestCommandParameters()
6969
'edit', 'tests/fixtures/data.yml', '--filter=color=red',
7070
],
7171

72+
[
73+
'b:
74+
color: blue
75+
shape: square', self::STATUS_OK,
76+
'edit', 'tests/fixtures/data.yml', '--filter=blue',
77+
],
78+
7279
];
7380
}
7481

0 commit comments

Comments
 (0)