Skip to content

Commit

Permalink
Merge pull request #7 from ibrahimgunduz34/regex_operator
Browse files Browse the repository at this point in the history
Regex operator
  • Loading branch information
ibrahimgunduz34 committed Dec 11, 2019
2 parents 8738e43 + 8aeebd5 commit 1693e2a
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 21 deletions.
4 changes: 3 additions & 1 deletion Operator/OperatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function create($rules)
/**
* @param $field
* @param $condition
* @return EqualOperator|GreaterOperator|GreaterOrEqualOperator|InOperator|LessOperator|LessOrEqualOperator|BetweenOperator
* @return EqualOperator|GreaterOperator|GreaterOrEqualOperator|InOperator|LessOperator|LessOrEqualOperator|BetweenOperator|RegexOperator
*/
private function createComparisionOperator($field, $condition)
{
Expand All @@ -51,6 +51,8 @@ private function createComparisionOperator($field, $condition)
return new InOperator($field, $value);
case 'btw':
return new BetweenOperator($field, $value);
case "regex":
return new RegexOperator($field, $value);
default:
throw new InvalidConfigurationException('Invalid operator type: ' . $operatorType);
}
Expand Down
19 changes: 19 additions & 0 deletions Operator/RegexOperator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace SweetCode\MariaBundle\Operator;

class RegexOperator extends Operator
{
/**
* @param array $data
* @return bool
*/
protected function accept($data)
{
$field = $this->getField();
if (!array_key_exists($field, $data)) {
return false;
}

return boolval(preg_match($this->getValue(), $data[$field]));
}
}
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,28 @@ maria:

# amount > 100 AND category_id IN [1, 2, 3]
amount: {gt: 100}
category_id: {in: [1,2,3]}
category_id: { in: [1,2,3] }

# (amount > AND category_id=1) OR (amount < 500 AND category_id IN [5,6])
- amount: {gt: 100}
category_id: {eql: 1}
category_id: { eql: 1 }
- amount: [lt: 500]
category_id: [in: [5,6]]
category_id: { in: [5,6] }

# (category_id IN [1,2] AND amount BETWEEN 100-200) OR (category_id = 3 AND amount >= 200
- amount: [btw: [100, 200]]
category_id: [in: [1, 2]]
- amount: { btw: [100, 200] }
category_id: { in: [1, 2] }
- amount: [lte: 200]
category_id: [eql: 3]
category_id: { eql: 3 }

# Matching by RegExp
- { category_id: { in: [1,5] }, description: { regex: /awesome/i } }
```

## See Also
* [Iterable Matchers](/Resources/docs/matchers.md)
* [Arithmetic And Logic Operators](/Resources/docs/operators.md)

## Example Usage

Define the following configuration into `config/packages/maria.yaml`
Expand Down Expand Up @@ -245,7 +252,7 @@ Enjoy!
* Validation improvement for configuration.

## License:
You can access the license documentation [here](/Resources/docs/LICENSE).
You can access the license documentation [here](/LICENSE).

## Credits:
Bundles structure, extension tests and the documentation partially inspired `RabbitMqBundle`.
13 changes: 0 additions & 13 deletions Resources/config/maria.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
parameters:
maria.event_listener.trigger.class: SweetCode\MariaBundle\Listener\TriggerEventListener

maria.comparator.eql.class: SweetCode\MariaBundle\Comparator\EqualComparator
maria.comparator.gt.class: SweetCode\MariaBundle\Comparator\GreaterComparator
maria.comparator.gte.class: SweetCode\MariaBundle\Comparator\GreaterOrEqualComparator
maria.comparator.lt.class: SweetCode\MariaBundle\Comparator\LessComparator
maria.comparator.lte.class: SweetCode\MariaBundle\Comparator\LessOrEqualComparator
maria.comparator.in.class: SweetCode\MariaBundle\Comparator\InComparator

maria.matcher.any.class: SweetCode\MariaBundle\Matcher\AnyMatcher
maria.matcher.all.class: SweetCode\MariaBundle\Matcher\AllMatcher
maria.matcher.first.class: SweetCode\MariaBundle\Matcher\FirstMatcher
maria.matcher.last.class: SweetCode\MariaBundle\Matcher\LastMatcher
maria.matcher.none.class: SweetCode\MariaBundle\Matcher\NoneMatcher

maria.service_factory.matcher.class: SweetCode\MariaBundle\Matcher\MatcherFactory
maria.service_factory.operator.class: SweetCode\MariaBundle\Operator\OperatorFactory

Expand Down
66 changes: 66 additions & 0 deletions Resources/docs/matchers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
## Iterable Matchers

[Back to home page](/README.md)

### ALL:
```yaml
# ...
rules:
all:
- amount: { gt: 100 }
# ...
```

### ANY:
```yaml
# ...
rules:
any:
- amount: { gt: 100 }
# ...
```

### NONE:
```yaml
# ...
rules:
none:
- amount: { gt: 100 }
# ...
```

### FIRST:
```yaml
# ...
rules:
first:
- amount: { gt: 100 }
# ...
```

### LAST:
```yaml
# ...
rules:
last:
- amount: { gt: 100 }
# ...
```

### DEFAULT:

```yaml
# ...
rules:
- amount: { gt: 100 }
# ...
```
or

```yaml
# ...
rules:
default:
- amount: { gt: 100 }
# ...
```
92 changes: 92 additions & 0 deletions Resources/docs/operators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
## Arithmetic And Logic Operators

[Back to home page](/README.md)

### Greater Than X

```yaml
# ...
rules:
- amount: { gt: 100 }
# ...
```

### Less Than X

```yaml
# ...
rules:
- amount: { lt: 100 }
# ...
```

### Greater Than Or EqualX

```yaml
# ...
rules:
- amount: { gte: 100 }
# ...
```

### Less Than Or Equal X

```yaml
# ...
rules:
- amount: { lte: 100 }
# ...
```

### Between

```yaml
# ...
rules:
- amount: { btw: [100, 500] }
# ...
```

### Regex

```yaml
# ...
rules:
- description: { regex: /awesome/i }
# ...
```

### In [...]
```yaml
# ...
rules:
- category_id: { in: [1,2,3] }
# ...
```
### And

```yaml
# ...
rules:
- amount: { gt: 100 }
category_id: { in: [1,2,3] }
# ...
```

### Or

```yaml
# ...
rules:
- category_id: { eql: 10 }
- category_id: { eql: 5 }
# ...
```








25 changes: 25 additions & 0 deletions Tests/Operator/RegexOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace SweetCode\MariaBundle\Tests\Operator;

use PHPUnit\Framework\TestCase;
use SweetCode\MariaBundle\Operator\RegexOperator;

class RegexOperatorTest extends TestCase
{
public function testCompareSuccess()
{
$operatorObject = new RegexOperator('field', "/gunduz/i");

$data = ['field' => 'Ibrahim Gunduz is a developer'];
$this->assertTrue($operatorObject->compare($data));
}

public function testCompareFail()
{
$operatorObject = new RegexOperator('field', "/manager/i");

$data = ['field' => 'Ibrahim Gunduz is a developer'];
$this->assertFalse($operatorObject->compare($data));
}
}

0 comments on commit 1693e2a

Please sign in to comment.