This commit is contained in:
Oliver Davies 2019-04-23 18:31:15 +01:00
parent caa3767246
commit ba5a972715
3 changed files with 43 additions and 7 deletions

View file

@ -1,15 +1,17 @@
<?php
use Opdavies\GmailFilterBuilder\Filter;
use Opdavies\GmailFilterBuilder\Model\Filter;
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/../vendor/autoload.php';
return [
Filter::create()
->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'),
];

View file

@ -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)";
}
}

View file

@ -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()
);
}
}