mirror of
https://github.com/opdavies/gmail-filter-builder.git
synced 2025-01-22 12:07:32 +00:00
Initial setup
This commit is contained in:
parent
348f419410
commit
c5b260f298
|
@ -1,13 +0,0 @@
|
|||
root = true
|
||||
|
||||
[*]
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
|
||||
[*.json]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
[*.{php,xml}]
|
||||
indent_style = space
|
||||
indent_size = 4
|
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,3 +1,2 @@
|
|||
*.xml
|
||||
/vendor/
|
||||
composer.lock
|
||||
vendor/
|
||||
|
|
38
README.md
38
README.md
|
@ -1,38 +0,0 @@
|
|||
# gmail-filter-builder
|
||||
|
||||
## Description
|
||||
|
||||
Inspired by the [gmail-britta](https://github.com/antifuchs/gmail-britta) Ruby library, the Gmail Filter Builder generates XML that can be imported into Gmail’s filter settings.
|
||||
|
||||
## Usage
|
||||
|
||||
* Run `composer require opdavies/gmail-filter-builder` to download the library.
|
||||
* Create a new PHP file and require `autoload.php`.
|
||||
* Create an array of `GmailFilter` objects, each with it’s required methods.
|
||||
* Pass the filters into an instance of `GmailFilterBuilder`.
|
||||
|
||||
```php
|
||||
require __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
$filters = [];
|
||||
|
||||
// Add filters.
|
||||
$filters[] = GmailFilter::create(
|
||||
...
|
||||
);
|
||||
|
||||
// Display the output.
|
||||
new GmailFilterBuilder($filters);
|
||||
```
|
||||
|
||||
To generate the output, run PHP on the file - e.g. `php generate.php`.
|
||||
|
||||
By default, the output is displayed on screen. To generate a file, use the greater than symbol followed by a file name - e.g. `php generate.php > filters.xml`.
|
||||
|
||||
## Example
|
||||
|
||||
For a working example, see the [opdavies/gmail-filters](https://github.com/opdavies/gmail-filters/blob/master/generate.php) repository.
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
|
@ -1,30 +1,21 @@
|
|||
{
|
||||
"name": "opdavies/gmail-filter-builder",
|
||||
"description": "",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Oliver Davies",
|
||||
"email": "oliver@oliverdavies.uk"
|
||||
"name": "opdavies/gmail-filter-builder",
|
||||
"description": "Generates XML to import as Gmail filters.",
|
||||
"type": "library",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Oliver Davies",
|
||||
"email": "oliver@oliverdavies.uk"
|
||||
}
|
||||
],
|
||||
"require": {},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^5.7"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Opdavies\\GmailFilterBuilder\\": "src/"
|
||||
}
|
||||
}
|
||||
],
|
||||
"license": "MIT",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Opdavies\\GmailFilterBuilder\\": "src/Opdavies/GmailFilterBuilder"
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
"opdavies/twig-extensions": "^1.1"
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "0.4-dev"
|
||||
}
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^5.6"
|
||||
},
|
||||
"config": {
|
||||
"sort-packages": true
|
||||
}
|
||||
}
|
||||
|
|
21
phpunit.xml
21
phpunit.xml
|
@ -1,21 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/5.6/phpunit.xsd"
|
||||
bootstrap="vendor/autoload.php"
|
||||
backupGlobals="false"
|
||||
beStrictAboutCoversAnnotation="true"
|
||||
beStrictAboutOutputDuringTests="true"
|
||||
beStrictAboutTestsThatDoNotTestAnything="true"
|
||||
beStrictAboutTodoAnnotatedTests="true"
|
||||
verbose="true"
|
||||
colors="true">
|
||||
<testsuite>
|
||||
<directory suffix="Test.php">tests</directory>
|
||||
</testsuite>
|
||||
|
||||
<filter>
|
||||
<whitelist processUncoveredFilesFromWhitelist="true">
|
||||
<directory suffix=".php">src</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
</phpunit>
|
13
phpunit.xml.dist
Normal file
13
phpunit.xml.dist
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit bootstrap="./vendor/autoload.php"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
stopOnFailure="true">
|
||||
<testsuites>
|
||||
<testsuite name="Unit tests">
|
||||
<directory suffix="Test.php">./tests/Unit</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
</phpunit>
|
|
@ -1,82 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Opdavies\GmailFilterBuilder;
|
||||
|
||||
use Opdavies\Twig\Extensions\TwigBooleanStringExtension;
|
||||
use Twig_Environment;
|
||||
use Twig_Loader_Filesystem;
|
||||
|
||||
class Builder
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $email;
|
||||
|
||||
/**
|
||||
* @var Twig_Environment
|
||||
*/
|
||||
private $twig;
|
||||
|
||||
/**
|
||||
* An array of filters.
|
||||
*
|
||||
* @var GmailFilter[]
|
||||
*/
|
||||
private $filters = [];
|
||||
|
||||
public function __construct(array $filters)
|
||||
{
|
||||
$this->twig = new Twig_Environment(
|
||||
new Twig_Loader_Filesystem(__DIR__.'/../../../templates')
|
||||
);
|
||||
|
||||
$this->twig->addExtension(new TwigBooleanStringExtension());
|
||||
|
||||
$this->filters = $filters;
|
||||
|
||||
return $this->generate();
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->generate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Build Gmail filters.
|
||||
*
|
||||
* @param GmailFilter[] $filters
|
||||
* An array of filters to process.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public static function build(array $filters)
|
||||
{
|
||||
return new static($filters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
private function generate()
|
||||
{
|
||||
ob_start();
|
||||
|
||||
print $this->twig->render(
|
||||
'filters.xml.twig',
|
||||
[
|
||||
'name' => $this->name,
|
||||
'email' => $this->email,
|
||||
'filters' => $this->filters,
|
||||
]
|
||||
);
|
||||
|
||||
return ob_get_contents();
|
||||
}
|
||||
}
|
|
@ -1,240 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Opdavies\GmailFilterBuilder;
|
||||
|
||||
class Filter
|
||||
{
|
||||
/**
|
||||
* @param array
|
||||
*/
|
||||
private $conditions = [];
|
||||
|
||||
/**
|
||||
* @param array
|
||||
*/
|
||||
private $labels = [];
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $archive = false;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $spam = false;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $trash = false;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $neverSpam = false;
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return new static();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function isTrash() {
|
||||
return $this->trash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getConditions() {
|
||||
return $this->conditions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getLabels() {
|
||||
return $this->labels;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function isArchive() {
|
||||
return $this->archive;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function isSpam() {
|
||||
return $this->spam;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function isNeverSpam() {
|
||||
return $this->neverSpam;
|
||||
}
|
||||
|
||||
/**
|
||||
* Condition based on words within the email.
|
||||
*
|
||||
* @param string $value
|
||||
* The value to compare against.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function contains($value) {
|
||||
return $this->condition('hasTheWord', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Condition based on words within the email.
|
||||
*
|
||||
* @param string $value
|
||||
* The value to compare against.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function has($value) {
|
||||
return $this->contains($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Condition based on the subject.
|
||||
*
|
||||
* @param string $value
|
||||
* The value to compare against.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function subject($value) {
|
||||
return $this->condition('subject', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a label.
|
||||
*
|
||||
* @param string $label
|
||||
* The label to assign.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function label($label) {
|
||||
$this->labels[] = $label;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Label and archive a message.
|
||||
*
|
||||
* @param string $label
|
||||
* The label to assign.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function labelAndArchive($label)
|
||||
{
|
||||
$this->label($label)->archive();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function archive() {
|
||||
$this->archive = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark as spam.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function spam() {
|
||||
$this->spam = true;
|
||||
$this->neverSpam = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Never mark as spam.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function neverSpam() {
|
||||
$this->neverSpam = true;
|
||||
$this->spam = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Who the email is from.
|
||||
*
|
||||
* @param array $values
|
||||
* An array of names or email addresses for the sender.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function from(array $values)
|
||||
{
|
||||
$this->condition('from', implode(' OR ', $values));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Who the email is sent to.
|
||||
*
|
||||
* @param array $values
|
||||
* An array of names or email addresses for the receiver.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function to(array $values)
|
||||
{
|
||||
$this->condition('to', implode(' OR ', $values));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark a message to be trashed.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function trash()
|
||||
{
|
||||
$this->trash = TRUE;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a condition.
|
||||
*
|
||||
* @param string $type
|
||||
* The type of condition.
|
||||
* @param $value
|
||||
* The value of the condition.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
private function condition($type, $value)
|
||||
{
|
||||
$this->conditions[] = [$type, $value];
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>
|
||||
<title>Mail Filters (Blackhole)</title>
|
||||
<id>tag:mail.google.com,2008:filters:1297349082768</id>
|
||||
<updated>2013-02-04T15:50:38Z</updated>
|
||||
|
||||
{% for filter in filters -%}
|
||||
<entry>
|
||||
<category term='filter'></category>
|
||||
<title>Mail Filter</title>
|
||||
<content></content>
|
||||
<apps:property name='shouldArchive' value='{{ filter.isArchive|boolean_string }}'/>
|
||||
<apps:property name='shouldNeverSpam' value='{{ filter.isNeverSpam|boolean_string }}'/>
|
||||
<apps:property name='shouldTrash' value='{{ filter.isTrash|boolean_string }}'/>
|
||||
{% for condition in filter.conditions -%}
|
||||
<apps:property name='{{ condition[0] }}' value='{{ condition[1] }}'/>
|
||||
{%- endfor %}
|
||||
{%- for label in filter.labels %}
|
||||
<apps:property name='label' value='{{ label }}'/>
|
||||
{% endfor %}
|
||||
</entry>
|
||||
{% endfor %}
|
||||
</feed>
|
|
@ -1,83 +0,0 @@
|
|||
<?php
|
||||
|
||||
class GmailFilterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var GmailFilter
|
||||
*/
|
||||
private $filter;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->filter = new GmailFilter();
|
||||
}
|
||||
|
||||
public function testSingleFrom() {
|
||||
// TODO: Does this need to be done each time?
|
||||
$output = $this->createBuilder([
|
||||
$this->filter->from(['foo@example.com'])
|
||||
]);
|
||||
|
||||
$this->assertContains('name=\'from\' value=\'foo@example.com\'', $output);
|
||||
}
|
||||
|
||||
public function testMultipleFrom() {
|
||||
$output = $this->createBuilder([
|
||||
$this->filter->from(['foo@example.com', 'bar@example.com'])
|
||||
]);
|
||||
|
||||
$this->assertContains('name=\'from\' value=\'foo@example.com OR bar@example.com\'', $output);
|
||||
}
|
||||
|
||||
public function testSingleTo() {
|
||||
// TODO: Does this need to be done each time?
|
||||
$output = $this->createBuilder([
|
||||
$this->filter->to(['foo@example.com'])
|
||||
]);
|
||||
|
||||
$this->assertContains('name=\'to\' value=\'foo@example.com\'', $output);
|
||||
}
|
||||
|
||||
public function testMultipleTo() {
|
||||
$output = $this->createBuilder([
|
||||
$this->filter->to(['foo@example.com', 'bar@example.com'])
|
||||
]);
|
||||
|
||||
$this->assertContains('name=\'to\' value=\'foo@example.com OR bar@example.com\'', $output);
|
||||
}
|
||||
|
||||
public function testArchive()
|
||||
{
|
||||
$output = $this->createBuilder([
|
||||
$this->filter->archive()
|
||||
]);
|
||||
|
||||
$this->assertContains('name=\'shouldArchive\' value=\'true\'', $output);
|
||||
$this->assertNotContains('name=\'shouldArchive\' value=\'false\'', $output);
|
||||
}
|
||||
|
||||
public function testLabelAndArchive()
|
||||
{
|
||||
$output = $this->createBuilder([
|
||||
$this->filter->labelAndArchive('foo')
|
||||
]);
|
||||
|
||||
$this->assertContains('name=\'label\' value=\'foo\'', $output);
|
||||
|
||||
$this->assertContains('name=\'shouldArchive\' value=\'true\'', $output);
|
||||
$this->assertNotContains('name=\'shouldArchive\' value=\'false\'', $output);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param GmailFilter[] $filters An array of filters.
|
||||
*
|
||||
* @return string A string representation of GmailFilterBuilder.
|
||||
*/
|
||||
private function createBuilder($filters) {
|
||||
return (string) new GmailFilterBuilder($filters);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue