mirror of
https://github.com/opdavies/gmail-filter-builder.git
synced 2025-03-13 05:26:57 +00:00
Initial commit
This commit is contained in:
commit
d56e7c6620
13
.editorconfig
Normal file
13
.editorconfig
Normal file
|
@ -0,0 +1,13 @@
|
|||
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
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
vendor/
|
18
composer.json
Normal file
18
composer.json
Normal file
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"name": "opdavies/gmail-filter-builder",
|
||||
"description": "",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Oliver Davies",
|
||||
"email": "oliver@oliverdavies.uk"
|
||||
}
|
||||
],
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"": "src/"
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
"twig/twig": "^1.24"
|
||||
}
|
||||
}
|
80
composer.lock
generated
Normal file
80
composer.lock
generated
Normal file
|
@ -0,0 +1,80 @@
|
|||
{
|
||||
"_readme": [
|
||||
"This file locks the dependencies of your project to a known state",
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"hash": "ee4a05d220006a064b4fda49803c239b",
|
||||
"content-hash": "e796f90e007457e383570e7ae0e15248",
|
||||
"packages": [
|
||||
{
|
||||
"name": "twig/twig",
|
||||
"version": "v1.24.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/twigphp/Twig.git",
|
||||
"reference": "3566d311a92aae4deec6e48682dc5a4528c4a512"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/twigphp/Twig/zipball/3566d311a92aae4deec6e48682dc5a4528c4a512",
|
||||
"reference": "3566d311a92aae4deec6e48682dc5a4528c4a512",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.2.7"
|
||||
},
|
||||
"require-dev": {
|
||||
"symfony/debug": "~2.7",
|
||||
"symfony/phpunit-bridge": "~2.7"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.24-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": {
|
||||
"Twig_": "lib/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"BSD-3-Clause"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Fabien Potencier",
|
||||
"email": "fabien@symfony.com",
|
||||
"homepage": "http://fabien.potencier.org",
|
||||
"role": "Lead Developer"
|
||||
},
|
||||
{
|
||||
"name": "Armin Ronacher",
|
||||
"email": "armin.ronacher@active-4.com",
|
||||
"role": "Project Founder"
|
||||
},
|
||||
{
|
||||
"name": "Twig Team",
|
||||
"homepage": "http://twig.sensiolabs.org/contributors",
|
||||
"role": "Contributors"
|
||||
}
|
||||
],
|
||||
"description": "Twig, the flexible, fast, and secure template language for PHP",
|
||||
"homepage": "http://twig.sensiolabs.org",
|
||||
"keywords": [
|
||||
"templating"
|
||||
],
|
||||
"time": "2016-05-30 09:11:59"
|
||||
}
|
||||
],
|
||||
"packages-dev": [],
|
||||
"aliases": [],
|
||||
"minimum-stability": "stable",
|
||||
"stability-flags": [],
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": [],
|
||||
"platform-dev": []
|
||||
}
|
175
src/GmailFilter.php
Normal file
175
src/GmailFilter.php
Normal file
|
@ -0,0 +1,175 @@
|
|||
<?php
|
||||
|
||||
class GmailFilter
|
||||
{
|
||||
/**
|
||||
* @param array
|
||||
*/
|
||||
private $conditions = [];
|
||||
|
||||
/**
|
||||
* @param array
|
||||
*/
|
||||
private $labels = [];
|
||||
|
||||
private $archive = false;
|
||||
|
||||
private $spam = false;
|
||||
|
||||
private $trash = false;
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function isTrash() {
|
||||
return $this->trash;
|
||||
}
|
||||
|
||||
private $neverSpam = false;
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getConditions() {
|
||||
return $this->conditions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
public function __get($name) {
|
||||
return $this->$name;
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return new static();
|
||||
}
|
||||
|
||||
public function contains($value) {
|
||||
return $this->condition('hasTheWord', $value);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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('||', $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('||', $values));
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
77
src/GmailFilterBuilder.php
Normal file
77
src/GmailFilterBuilder.php
Normal file
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
class GmailFilterBuilder
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $email;
|
||||
|
||||
/**
|
||||
* @var \Twig_Environment
|
||||
*/
|
||||
private $twig;
|
||||
|
||||
/**
|
||||
* An array of filters.
|
||||
*
|
||||
* @var GmailFilter[]
|
||||
*/
|
||||
private $filters = [];
|
||||
|
||||
public function __construct($name, $email, $filters)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->email = $email;
|
||||
|
||||
$this->twig = new Twig_Environment(
|
||||
new Twig_Loader_Filesystem(__DIR__ . '/../templates')
|
||||
);
|
||||
|
||||
$this->filters = $filters;
|
||||
|
||||
return $this->generate();
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->generate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Build Gmail filters.
|
||||
*
|
||||
* @param string $name
|
||||
* The author name.
|
||||
* @param string $email
|
||||
* The author email address.
|
||||
* @param GmailFilter[] $filters
|
||||
* An array of filters to process.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public static function build($name, $email, array $filters)
|
||||
{
|
||||
return new static($name, $email, $filters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
private function generate()
|
||||
{
|
||||
return $this->twig->render(
|
||||
'filters.xml.twig',
|
||||
[
|
||||
'name' => $this->name,
|
||||
'email' => $this->email,
|
||||
'filters' => $this->filters,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
25
templates/filters.xml.twig
Normal file
25
templates/filters.xml.twig
Normal file
|
@ -0,0 +1,25 @@
|
|||
<?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>
|
||||
<author>
|
||||
<name>{{ name }}</name>
|
||||
<email>{{ email }}</email>
|
||||
</author>
|
||||
|
||||
{% for filter in filters -%}
|
||||
<entry>
|
||||
<category term='filter'></category>
|
||||
<title>Mail Filter</title>
|
||||
<content></content>
|
||||
<apps:property name='shouldArchive' value='{{ filter.isArchive ? 'true' : 'false' }}'/>
|
||||
<apps:property name='shouldTrash' value='{{ filter.isTrash ? 'true' : 'false' }}'/>
|
||||
{% 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>
|
Loading…
Reference in a new issue