From 0594f9f0de1c3d280a654b1534435fa1d04d17c8 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Fri, 3 Nov 2017 23:22:13 +0000 Subject: [PATCH] Refactor, allow chaining methods --- composer.json | 1 + src/Filter.php | 50 ++++++++++++++++++++++++++++----------- tests/Unit/FilterTest.php | 46 ++++++++++++++++++++++++++--------- 3 files changed, 72 insertions(+), 25 deletions(-) diff --git a/composer.json b/composer.json index a3c97ed..1463e9f 100644 --- a/composer.json +++ b/composer.json @@ -10,6 +10,7 @@ } ], "require": { + "tightenco/collect": "^5.4" }, "require-dev": { "phpunit/phpunit": "^5.7" diff --git a/src/Filter.php b/src/Filter.php index fef13d8..9fe67ce 100644 --- a/src/Filter.php +++ b/src/Filter.php @@ -4,49 +4,71 @@ namespace Opdavies\GmailFilterBuilder; class Filter { + private $properties = []; + public function has($value) { - return ['hasTheWord' => $value]; + $this->properties['hasTheWord'] = $value; + + return $this; } public function from() { - return ['from' => func_get_args()]; + $this->properties['from'] = collect(func_get_args()) + ->map(function ($address) { + return trim($address); + })->implode(','); + + return $this; } public function label($label) { - return ['label' => $label]; + $this->properties['label'] = $label; + + return $this; } public function archive() { - return ['shouldArchive' => 'true']; + $this->properties['shouldArchive'] = 'true'; + + return $this; } public function labelAndArchive($label) { - return $this->label($label) + $this->archive(); + $this->label($label)->archive(); + + return $this; } public function spam() { - return [ - 'shouldSpam' => 'true', - 'shouldNeverSpam' => 'false', - ]; + $this->properties['shouldSpam'] = 'true'; + $this->properties['shouldNeverSpam'] = 'false'; + + return $this; } public function neverSpam() { - return [ - 'shouldSpam' => 'false', - 'shouldNeverSpam' => 'true', - ]; + $this->properties['shouldSpam'] = 'false'; + $this->properties['shouldNeverSpam'] = 'true'; + + return $this; } public function trash() { - return ['shouldTrash' => 'true']; + $this->properties['shouldTrash'] = 'true'; + + return $this; + } + + public function getProperties() + { + return $this->properties; } } diff --git a/tests/Unit/FilterTest.php b/tests/Unit/FilterTest.php index c67f892..b87dcd6 100644 --- a/tests/Unit/FilterTest.php +++ b/tests/Unit/FilterTest.php @@ -28,7 +28,7 @@ class FilterTest extends \PHPUnit_Framework_TestCase { $this->assertEquals( ['hasTheWord' => 'something'], - $this->filter->has('something') + $this->filter->has('something')->getProperties() ); } @@ -39,14 +39,16 @@ class FilterTest extends \PHPUnit_Framework_TestCase { // Ensure that we can set one from address. $this->assertEquals( - ['from' => ['foo@example.com']], - $this->filter->from('foo@example.com') + ['from' => 'foo@example.com'], + $this->filter->from('foo@example.com')->getProperties() ); // 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') + ['from' => 'foo@example.com,bar@example.com'], + $this->filter + ->from('foo@example.com', 'bar@example.com') + ->getProperties() ); } @@ -55,7 +57,10 @@ class FilterTest extends \PHPUnit_Framework_TestCase */ public function testLabel() { - $this->assertEquals(['label' => 'Foo'], $this->filter->label('Foo')); + $this->assertEquals( + ['label' => 'Foo'], + $this->filter->label('Foo')->getProperties() + ); } /** @@ -65,7 +70,7 @@ class FilterTest extends \PHPUnit_Framework_TestCase { $this->assertEquals( ['shouldArchive' => 'true'], - $this->filter->archive() + $this->filter->archive()->getProperties() ); } @@ -76,7 +81,7 @@ class FilterTest extends \PHPUnit_Framework_TestCase { $this->assertEquals( ['shouldArchive' => 'true', 'label' => 'Foo'], - $this->filter->labelAndArchive('Foo') + $this->filter->labelAndArchive('Foo')->getProperties() ); } @@ -90,7 +95,7 @@ class FilterTest extends \PHPUnit_Framework_TestCase 'shouldSpam' => 'true', 'shouldNeverSpam' => 'false' ], - $this->filter->spam() + $this->filter->spam()->getProperties() ); } @@ -104,7 +109,7 @@ class FilterTest extends \PHPUnit_Framework_TestCase 'shouldSpam' => 'false', 'shouldNeverSpam' => 'true' ], - $this->filter->neverSpam() + $this->filter->neverSpam()->getProperties() ); } @@ -115,7 +120,26 @@ class FilterTest extends \PHPUnit_Framework_TestCase { $this->assertEquals( ['shouldTrash' => 'true'], - $this->filter->trash() + $this->filter->trash()->getProperties() + ); + } + + public function testMethodsCanBeChained() + { + $this->assertEquals( + [ + 'from' => 'foo@example.com,bar@example.com', + 'hasTheWord' => 'Something', + 'label' => 'Foo', + 'shouldArchive' => 'true', + 'shouldNeverSpam' => 'true', + 'shouldSpam' => 'false', + ], + $this->filter->from('foo@example.com ', 'bar@example.com') + ->has('Something') + ->labelAndArchive('Foo') + ->neverSpam() + ->getProperties() ); } }