gmail-filter-builder/src/GmailFilterBuilder.php

79 lines
1.4 KiB
PHP
Raw Normal View History

2016-07-12 19:58:57 +01:00
<?php
use Opdavies\Twig\Extensions\TwigBooleanStringExtension;
2016-07-12 19:58:57 +01:00
class GmailFilterBuilder
{
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $email;
/**
2016-07-15 01:52:03 +01:00
* @var Twig_Environment
2016-07-12 19:58:57 +01:00
*/
private $twig;
/**
* An array of filters.
*
* @var GmailFilter[]
*/
private $filters = [];
2016-07-15 01:08:08 +01:00
public function __construct(array $filters)
2016-07-12 19:58:57 +01:00
{
$this->twig = new Twig_Environment(
2016-07-15 01:52:03 +01:00
new Twig_Loader_Filesystem(__DIR__.'/../templates')
2016-07-12 19:58:57 +01:00
);
$this->twig->addExtension(new TwigBooleanStringExtension());
2016-07-12 19:58:57 +01:00
$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
*/
2016-07-15 01:08:08 +01:00
public static function build(array $filters)
2016-07-12 19:58:57 +01:00
{
2016-07-15 01:08:08 +01:00
return new static($filters);
2016-07-12 19:58:57 +01:00
}
2016-07-15 01:52:03 +01:00
/**
* @return string
*/
2016-07-12 19:58:57 +01:00
private function generate()
{
2016-09-30 12:59:32 +01:00
ob_start();
print $this->twig->render(
2016-07-12 19:58:57 +01:00
'filters.xml.twig',
[
'name' => $this->name,
'email' => $this->email,
'filters' => $this->filters,
]
);
2016-09-30 12:59:32 +01:00
return ob_get_contents();
2016-07-12 19:58:57 +01:00
}
}