diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 1513441..0000000 --- a/.editorconfig +++ /dev/null @@ -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 diff --git a/.gitignore b/.gitignore index fd2bdae..3a9875b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ -*.xml +/vendor/ composer.lock -vendor/ diff --git a/README.md b/README.md deleted file mode 100644 index 3e01ab8..0000000 --- a/README.md +++ /dev/null @@ -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 diff --git a/composer.json b/composer.json index 309e791..e182008 100644 --- a/composer.json +++ b/composer.json @@ -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 - } } diff --git a/phpunit.xml b/phpunit.xml deleted file mode 100644 index 580ee27..0000000 --- a/phpunit.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - tests - - - - - src - - - diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..a6907ba --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,13 @@ + + + + + ./tests/Unit + + + diff --git a/src/Opdavies/GmailFilterBuilder/Builder.php b/src/Opdavies/GmailFilterBuilder/Builder.php deleted file mode 100644 index 379336c..0000000 --- a/src/Opdavies/GmailFilterBuilder/Builder.php +++ /dev/null @@ -1,82 +0,0 @@ -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(); - } -} diff --git a/src/Opdavies/GmailFilterBuilder/Filter.php b/src/Opdavies/GmailFilterBuilder/Filter.php deleted file mode 100644 index 3125ace..0000000 --- a/src/Opdavies/GmailFilterBuilder/Filter.php +++ /dev/null @@ -1,240 +0,0 @@ -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; - } -} diff --git a/templates/filters.xml.twig b/templates/filters.xml.twig deleted file mode 100644 index 1314cf7..0000000 --- a/templates/filters.xml.twig +++ /dev/null @@ -1,22 +0,0 @@ - - Mail Filters (Blackhole) - tag:mail.google.com,2008:filters:1297349082768 - 2013-02-04T15:50:38Z - - {% for filter in filters -%} - - - Mail Filter - - - - - {% for condition in filter.conditions -%} - - {%- endfor %} - {%- for label in filter.labels %} - - {% endfor %} - - {% endfor %} - diff --git a/tests/GmailFilterTest.php b/tests/GmailFilterTest.php deleted file mode 100644 index 3e071b9..0000000 --- a/tests/GmailFilterTest.php +++ /dev/null @@ -1,83 +0,0 @@ -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); - } -}