diff --git a/examples/filters.php b/examples/filters.php index 588561c..9ff481d 100644 --- a/examples/filters.php +++ b/examples/filters.php @@ -1,15 +1,17 @@ from('foo@example.com') - ->labelAndArchive('Example'), + ->from('papercall.io') + ->subject('New Submission For Drupal Camp Bristol 2019') + ->labelAndArchive('DrupalCamp Bristol'), - Filter::create() - ->from('bar@example.com') - ->important(), + ($filter = Filter::create()) + ->from('papercall.io') + ->negate($filter->subject('New Submission For Drupal Camp Bristol 2019')) + ->labelAndArchive('Deletable/Other notifications'), ]; diff --git a/src/Model/Filter.php b/src/Model/Filter.php index 76b14ff..5ac96d0 100644 --- a/src/Model/Filter.php +++ b/src/Model/Filter.php @@ -275,4 +275,22 @@ class Filter { return $this->properties; } + + public function negate(Filter $filter): self + { + $filter->properties = collect($filter->toArray()) + ->mapWithKeys(function ($conditions, $key) { + return [$key => collect($conditions)->map(function ($condition) { + return $this->negateCondition($condition); + })]; + }) + ->toArray(); + + return $this; + } + + private function negateCondition(string $condition): string + { + return "-($condition)"; + } } diff --git a/tests/Unit/Model/FilterTest.php b/tests/Unit/Model/FilterTest.php index 57e7223..02998e0 100644 --- a/tests/Unit/Model/FilterTest.php +++ b/tests/Unit/Model/FilterTest.php @@ -337,4 +337,20 @@ class FilterTest extends TestCase ->toArray() ); } + + /** @test */ + public function conditions_can_be_negated() + { + $this->filter + ->from('example.com') + ->negate($this->filter->subject('test')); + + $this->assertEquals( + [ + 'from' => 'example.com', + 'subject' => '-(test)', + ], + $this->filter->toArray() + ); + } }