Add filter conditions using a class

This commit is contained in:
Oliver Davies 2019-05-03 21:02:13 +01:00
parent 22b696d47c
commit 5d5d5118ef
3 changed files with 146 additions and 78 deletions

View file

@ -2,6 +2,8 @@
namespace Opdavies\GmailFilterBuilder\Model;
use Tightenco\Collect\Support\Collection;
class Filter
{
/** @var string */
@ -12,6 +14,13 @@ class Filter
*/
private $properties = [];
private $conditions;
public function __construct()
{
$this->conditions = collect();
}
/**
* @return static
*/
@ -27,7 +36,7 @@ class Filter
*/
public function has(string $value): self
{
$this->properties['hasTheWord'] = $value;
$this->conditions->push(new FilterCondition('hasTheWord', $value));
return $this;
}
@ -39,7 +48,7 @@ class Filter
*/
public function hasNot(string $value): self
{
$this->properties['doesNotHaveTheWord'] = $value;
$this->conditions->push(new FilterCondition('doesNotHaveTheWord', $value));
return $this;
}
@ -51,11 +60,7 @@ class Filter
*/
public function from($values): self
{
if (!empty($values)) {
$this->properties['from'] = collect($values)->map(function ($value) {
return trim($value);
})->all();
}
$this->conditions->push(new FilterCondition('from', $values));
return $this;
}
@ -67,11 +72,7 @@ class Filter
*/
public function to($values): self
{
if (!empty($values)) {
$this->properties['to'] = collect($values)->map(function ($value) {
return trim($value);
})->all();
}
$this->conditions->push(new FilterCondition('to', $values));
return $this;
}
@ -83,9 +84,10 @@ class Filter
*/
public function subject($values): self
{
$this->properties['subject'] = collect($values)->map(function ($value) {
return json_encode($value);
})->implode('|');
$this->conditions->push(new FilterCondition(
'subject',
collect($values)->map('json_encode')
));
return $this;
}
@ -95,7 +97,7 @@ class Filter
*/
public function hasAttachment(): self
{
$this->properties['hasAttachment'] = 'true';
$this->conditions->push(new FilterCondition('hasAttachment', 'true'));
return $this;
}
@ -109,7 +111,6 @@ class Filter
*/
public function fromList($value): self
{
$value = collect($value)->implode(self::SEPARATOR);
$this->has("list:{$value}");
return $this;
@ -120,7 +121,7 @@ class Filter
*/
public function excludeChats(): self
{
$this->properties['excludeChats'] = 'true';
$this->conditions->push(new FilterCondition('excludeChats', 'true'));
return $this;
}
@ -275,4 +276,9 @@ class Filter
{
return $this->properties;
}
public function getConditions(): Collection
{
return $this->conditions;
}
}

View file

@ -0,0 +1,30 @@
<?php
namespace Opdavies\GmailFilterBuilder\Model;
use Tightenco\Collect\Support\Collection;
class FilterCondition
{
/** @var string */
private $property;
/** @var Collection */
private $values;
public function __construct(string $property, $values)
{
$this->property = $property;
$this->values = collect($values);
}
public function getProperty(): string
{
return $this->property;
}
public function getValues(): Collection
{
return $this->values;
}
}