diff --git a/src/Model/Filter.php b/src/Model/Filter.php
index 76b14ff..1d6daa6 100644
--- a/src/Model/Filter.php
+++ b/src/Model/Filter.php
@@ -10,7 +10,12 @@ class Filter
/**
* @var array
*/
- private $properties = [];
+ private $conditions = [];
+
+ /**
+ * @var array
+ */
+ private $actions = [];
/**
* @return static
@@ -27,7 +32,7 @@ class Filter
*/
public function has(string $value): self
{
- $this->properties['hasTheWord'] = $value;
+ $this->conditions['hasTheWord'] = $value;
return $this;
}
@@ -39,7 +44,7 @@ class Filter
*/
public function hasNot(string $value): self
{
- $this->properties['doesNotHaveTheWord'] = $value;
+ $this->conditions['doesNotHaveTheWord'] = $value;
return $this;
}
@@ -52,7 +57,7 @@ class Filter
public function from($values): self
{
if (!empty($values)) {
- $this->properties['from'] = collect($values)->map(function ($value) {
+ $this->conditions['from'] = collect($values)->map(function ($value) {
return trim($value);
})->all();
}
@@ -68,7 +73,7 @@ class Filter
public function to($values): self
{
if (!empty($values)) {
- $this->properties['to'] = collect($values)->map(function ($value) {
+ $this->conditions['to'] = collect($values)->map(function ($value) {
return trim($value);
})->all();
}
@@ -83,7 +88,7 @@ class Filter
*/
public function subject($values): self
{
- $this->properties['subject'] = collect($values)->map(function ($value) {
+ $this->conditions['subject'] = collect($values)->map(function ($value) {
return json_encode($value);
})->implode('|');
@@ -95,7 +100,7 @@ class Filter
*/
public function hasAttachment(): self
{
- $this->properties['hasAttachment'] = 'true';
+ $this->conditions['hasAttachment'] = 'true';
return $this;
}
@@ -120,7 +125,7 @@ class Filter
*/
public function excludeChats(): self
{
- $this->properties['excludeChats'] = 'true';
+ $this->conditions['excludeChats'] = 'true';
return $this;
}
@@ -132,7 +137,7 @@ class Filter
*/
public function label(string $label): self
{
- $this->properties['label'] = $label;
+ $this->actions['label'] = $label;
return $this;
}
@@ -142,7 +147,7 @@ class Filter
*/
public function archive(): self
{
- $this->properties['shouldArchive'] = 'true';
+ $this->actions['shouldArchive'] = 'true';
return $this;
}
@@ -164,8 +169,8 @@ class Filter
*/
public function spam(): self
{
- $this->properties['shouldSpam'] = 'true';
- $this->properties['shouldNeverSpam'] = 'false';
+ $this->actions['shouldSpam'] = 'true';
+ $this->actions['shouldNeverSpam'] = 'false';
return $this;
}
@@ -175,8 +180,8 @@ class Filter
*/
public function neverSpam(): self
{
- $this->properties['shouldSpam'] = 'false';
- $this->properties['shouldNeverSpam'] = 'true';
+ $this->actions['shouldSpam'] = 'false';
+ $this->actions['shouldNeverSpam'] = 'true';
return $this;
}
@@ -186,7 +191,7 @@ class Filter
*/
public function trash(): self
{
- $this->properties['shouldTrash'] = 'true';
+ $this->actions['shouldTrash'] = 'true';
return $this;
}
@@ -196,7 +201,7 @@ class Filter
*/
public function read(): self
{
- $this->properties['markAsRead'] = 'true';
+ $this->actions['markAsRead'] = 'true';
return $this;
}
@@ -206,7 +211,7 @@ class Filter
*/
public function star(): self
{
- $this->properties['shouldStar'] = 'true';
+ $this->actions['shouldStar'] = 'true';
return $this;
}
@@ -218,7 +223,7 @@ class Filter
*/
public function forward(string $to): self
{
- $this->properties['forwardTo'] = $to;
+ $this->actions['forwardTo'] = $to;
return $this;
}
@@ -228,7 +233,7 @@ class Filter
*/
public function important(): self
{
- $this->properties['shouldAlwaysMarkAsImportant'] = 'true';
+ $this->actions['shouldAlwaysMarkAsImportant'] = 'true';
return $this;
}
@@ -238,7 +243,7 @@ class Filter
*/
public function notImportant(): self
{
- $this->properties['shouldNeverMarkAsImportant'] = 'true';
+ $this->actions['shouldNeverMarkAsImportant'] = 'true';
return $this;
}
@@ -250,29 +255,43 @@ class Filter
*/
public function categorise(string $category): self
{
- $this->properties['smartLabelToApply'] = $category;
+ $this->actions['smartLabelToApply'] = $category;
return $this;
}
/**
- * Return the filter properties as an array.
- *
- * @return array
- * @deprecated toArray()
+ * @deprecated
+ * @see toArray()
*/
public function getProperties(): array
{
- return $this->properties;
+ return $this->toArray();
+ }
+
+ public function getConditions(): array
+ {
+ $conditions = $this->conditions;
+ ksort($conditions);
+
+ return $conditions;
+ }
+
+ public function getActions(): array
+ {
+ $actions = $this->actions;
+ ksort($actions);
+
+ return $actions;
}
/**
- * Return the filter properties as an array.
- *
- * @return array
+ * @deprecated
+ * @see getConditions()
+ * @see getActions()
*/
public function toArray(): array
{
- return $this->properties;
+ return array_merge($this->conditions, $this->actions);
}
}
diff --git a/src/Service/Builder.php b/src/Service/Builder.php
index 0092b7b..c192b9b 100644
--- a/src/Service/Builder.php
+++ b/src/Service/Builder.php
@@ -88,13 +88,16 @@ class Builder
*/
private function buildEntry(Filter $filter): string
{
- $entry = collect($filter->toArray())
- ->map(function ($value, $key): string {
- return $this->buildProperty($value, $key);
- })
- ->implode($this->glue());
+ $conditions = $filter->getConditions();
+ $actions = $filter->getActions();
- return collect(['', $entry, ''])->implode($this->glue());
+ $entry = collect();
+
+ foreach (array_merge($conditions, $actions) as $property => $value) {
+ $entry->push($this->buildProperty($value, $property));
+ }
+
+ return collect(['', $entry->implode($this->glue()), ''])->implode($this->glue());
}
/**
diff --git a/tests/Unit/Model/FilterTest.php b/tests/Unit/Model/FilterTest.php
index 57e7223..4e7d533 100644
--- a/tests/Unit/Model/FilterTest.php
+++ b/tests/Unit/Model/FilterTest.php
@@ -33,7 +33,7 @@ class FilterTest extends TestCase
{
$this->assertEquals(
['hasTheWord' => 'something'],
- $this->filter->has('something')->toArray()
+ $this->filter->has('something')->getConditions()
);
}
@@ -45,7 +45,7 @@ class FilterTest extends TestCase
{
$this->assertEquals(
['doesNotHaveTheWord' => 'something'],
- $this->filter->hasNot('something')->toArray()
+ $this->filter->hasNot('something')->getConditions()
);
}
@@ -58,13 +58,13 @@ class FilterTest extends TestCase
// Ensure that we can set one from address.
$this->assertEquals(
['from' => ['foo@example.com']],
- $this->filter->from('foo@example.com')->toArray()
+ $this->filter->from('foo@example.com')->getConditions()
);
// Ensure that we can set multiple from addresses.
$this->assertEquals(
['from' => ['foo@example.com', 'bar@example.com']],
- $this->filter->from(['foo@example.com', 'bar@example.com'])->toArray()
+ $this->filter->from(['foo@example.com', 'bar@example.com'])->getConditions()
);
}
@@ -74,8 +74,8 @@ class FilterTest extends TestCase
*/
public function no_from_property_exists_if_the_value_is_empty()
{
- $this->assertArrayNotHasKey('from', $this->filter->from('')->toArray());
- $this->assertArrayNotHasKey('from', $this->filter->from([])->toArray());
+ $this->assertArrayNotHasKey('from', $this->filter->from('')->getConditions());
+ $this->assertArrayNotHasKey('from', $this->filter->from([])->getConditions());
}
/**
@@ -86,12 +86,12 @@ class FilterTest extends TestCase
{
$this->assertEquals(
['to' => ['foo@example.com']],
- $this->filter->to('foo@example.com')->toArray()
+ $this->filter->to('foo@example.com')->getConditions()
);
$this->assertEquals(
['to' => ['bar@example.com', 'baz@example.com']],
- $this->filter->to(['bar@example.com', 'baz@example.com'])->toArray()
+ $this->filter->to(['bar@example.com', 'baz@example.com'])->getConditions()
);
}
@@ -110,12 +110,12 @@ class FilterTest extends TestCase
{
$this->assertEquals(
['subject' => '"Something"'],
- $this->filter->subject('Something')->toArray()
+ $this->filter->subject('Something')->getConditions()
);
$this->assertEquals(
['subject' => '"Test"|"Foo bar"'],
- $this->filter->subject(['Test', 'Foo bar'])->toArray()
+ $this->filter->subject(['Test', 'Foo bar'])->getConditions()
);
}
@@ -127,7 +127,7 @@ class FilterTest extends TestCase
{
$this->assertEquals(
['hasAttachment' => 'true'],
- $this->filter->hasAttachment()->toArray()
+ $this->filter->hasAttachment()->getConditions()
);
}
@@ -139,12 +139,12 @@ class FilterTest extends TestCase
{
$this->assertEquals(
['hasTheWord' => 'list:foobar'],
- $this->filter->fromList('foobar')->toArray()
+ $this->filter->fromList('foobar')->getConditions()
);
$this->assertEquals(
['hasTheWord' => 'list:list-one.com|list-two.com'],
- $this->filter->fromList(['list-one.com', 'list-two.com'])->toArray()
+ $this->filter->fromList(['list-one.com', 'list-two.com'])->getConditions()
);
}
@@ -156,7 +156,7 @@ class FilterTest extends TestCase
{
$this->assertEquals(
['excludeChats' => 'true'],
- $this->filter->excludeChats()->toArray()
+ $this->filter->excludeChats()->getConditions()
);
}
@@ -168,7 +168,7 @@ class FilterTest extends TestCase
{
$this->assertEquals(
['label' => 'Foo'],
- $this->filter->label('Foo')->toArray()
+ $this->filter->label('Foo')->getActions()
);
}
@@ -180,7 +180,7 @@ class FilterTest extends TestCase
{
$this->assertEquals(
['shouldArchive' => 'true'],
- $this->filter->archive()->toArray()
+ $this->filter->archive()->getActions()
);
}
@@ -192,7 +192,7 @@ class FilterTest extends TestCase
{
$this->assertEquals(
['shouldArchive' => 'true', 'label' => 'Foo'],
- $this->filter->labelAndArchive('Foo')->toArray()
+ $this->filter->labelAndArchive('Foo')->getActions()
);
}
@@ -207,7 +207,7 @@ class FilterTest extends TestCase
'shouldSpam' => 'true',
'shouldNeverSpam' => 'false'
],
- $this->filter->spam()->toArray()
+ $this->filter->spam()->getActions()
);
}
@@ -222,7 +222,7 @@ class FilterTest extends TestCase
'shouldSpam' => 'false',
'shouldNeverSpam' => 'true'
],
- $this->filter->neverSpam()->toArray()
+ $this->filter->neverSpam()->getActions()
);
}
@@ -234,7 +234,7 @@ class FilterTest extends TestCase
{
$this->assertEquals(
['shouldTrash' => 'true'],
- $this->filter->trash()->toArray()
+ $this->filter->trash()->getActions()
);
}
@@ -246,7 +246,7 @@ class FilterTest extends TestCase
{
$this->assertEquals(
['markAsRead' => 'true'],
- $this->filter->read()->toArray()
+ $this->filter->read()->getActions()
);
}
@@ -258,7 +258,7 @@ class FilterTest extends TestCase
{
$this->assertEquals(
['shouldStar' => 'true'],
- $this->filter->star()->toArray()
+ $this->filter->star()->getActions()
);
}
@@ -270,7 +270,7 @@ class FilterTest extends TestCase
{
$this->assertEquals(
['forwardTo' => 'foo@example.com'],
- $this->filter->forward('foo@example.com')->toArray()
+ $this->filter->forward('foo@example.com')->getActions()
);
}
@@ -282,7 +282,7 @@ class FilterTest extends TestCase
{
$this->assertEquals(
['shouldAlwaysMarkAsImportant' => 'true'],
- $this->filter->important()->toArray()
+ $this->filter->important()->getActions()
);
}
@@ -294,7 +294,7 @@ class FilterTest extends TestCase
{
$this->assertEquals(
['shouldNeverMarkAsImportant' => 'true'],
- $this->filter->notImportant()->toArray()
+ $this->filter->notImportant()->getActions()
);
}
@@ -306,35 +306,37 @@ class FilterTest extends TestCase
{
$this->assertEquals(
['smartLabelToApply' => 'Foo'],
- $this->filter->categorise('Foo')->toArray()
+ $this->filter->categorise('Foo')->getActions()
);
}
/** @test */
public function methods_can_be_chained()
{
- $this->assertEquals(
- [
- 'from' => ['foo@example.com', 'bar@example.com'],
- 'hasTheWord' => 'Something',
- 'excludeChats' => 'true',
- 'label' => 'Foo',
- 'markAsRead' => 'true',
- 'shouldArchive' => 'true',
- 'shouldNeverSpam' => 'true',
- 'shouldSpam' => 'false',
- 'shouldStar' => 'true',
- 'shouldAlwaysMarkAsImportant' => 'true',
- ],
- $this->filter->from(['foo@example.com ', 'bar@example.com'])
- ->has('Something')
- ->excludeChats()
- ->labelAndArchive('Foo')
- ->read()
- ->important()
- ->neverSpam()
- ->star()
- ->toArray()
- );
+ $filter = $this->filter
+ ->from(['foo@example.com ', 'bar@example.com'])
+ ->has('Something')
+ ->excludeChats()
+ ->labelAndArchive('Foo')
+ ->read()
+ ->important()
+ ->neverSpam()
+ ->star();
+
+ $this->assertSame([
+ 'excludeChats' => 'true',
+ 'from' => ['foo@example.com', 'bar@example.com'],
+ 'hasTheWord' => 'Something',
+ ], $filter->getConditions());
+
+ $this->assertSame([
+ 'label' => 'Foo',
+ 'markAsRead' => 'true',
+ 'shouldAlwaysMarkAsImportant' => 'true',
+ 'shouldArchive' => 'true',
+ 'shouldNeverSpam' => 'true',
+ 'shouldSpam' => 'false',
+ 'shouldStar' => 'true',
+ ], $filter->getActions());
}
}
diff --git a/tests/Unit/Service/BuilderTest.php b/tests/Unit/Service/BuilderTest.php
index 715299f..3d3af46 100644
--- a/tests/Unit/Service/BuilderTest.php
+++ b/tests/Unit/Service/BuilderTest.php
@@ -33,8 +33,8 @@ class BuilderTest extends TestCase
-
+
EOF;
diff --git a/tests/Unit/Service/PartialsTest.php b/tests/Unit/Service/PartialsTest.php
index 47a1b3c..b259d55 100644
--- a/tests/Unit/Service/PartialsTest.php
+++ b/tests/Unit/Service/PartialsTest.php
@@ -15,10 +15,9 @@ class PartialsTest extends TestCase
$filters = FakePartials::load('filters');
$this->assertCount(3, $filters);
-
- $this->assertSame('foo@example.com', $filters[0]->toArray()['from'][0]);
- $this->assertSame('bar@example.com', $filters[1]->toArray()['from'][0]);
- $this->assertSame('baz@example.com', $filters[2]->toArray()['from'][0]);
+ $this->assertSame(['foo@example.com'], $filters[0]->getConditions()['from']);
+ $this->assertSame(['bar@example.com'], $filters[1]->getConditions()['from']);
+ $this->assertSame(['baz@example.com'], $filters[2]->getConditions()['from']);
}
}
@@ -29,6 +28,6 @@ class FakePartials extends Partials
*/
protected static function getFilePattern($directoryName): string
{
- return __DIR__ . '/../../stubs/filters/*.php';
+ return 'tests/stubs/filters/*.php';
}
}