2017-11-04 00:32:39 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-10 17:39:50 +00:00
|
|
|
namespace Opdavies\GmailFilterBuilder\Service;
|
|
|
|
|
2018-06-01 17:18:25 +01:00
|
|
|
use Opdavies\GmailFilterBuilder\Model\Filter;
|
2018-01-11 00:48:02 +00:00
|
|
|
use Symfony\Component\Filesystem\Filesystem;
|
2017-11-04 00:32:39 +00:00
|
|
|
|
|
|
|
class Builder
|
|
|
|
{
|
2018-01-11 00:48:02 +00:00
|
|
|
private $filesystem;
|
|
|
|
|
2017-12-30 10:56:07 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-11-04 00:32:39 +00:00
|
|
|
private $filters = [];
|
|
|
|
|
2018-01-11 00:48:02 +00:00
|
|
|
/**
|
|
|
|
* @var
|
|
|
|
*/
|
|
|
|
private $outputFile;
|
|
|
|
|
2018-01-15 09:24:38 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $writeFile;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $xml;
|
|
|
|
|
2018-03-20 20:34:27 +00:00
|
|
|
public function __construct(array $filters, $outputFile = 'filters.xml', $writeFile = true)
|
|
|
|
{
|
2018-01-11 00:48:02 +00:00
|
|
|
$this->filesystem = new Filesystem();
|
2017-11-04 00:32:39 +00:00
|
|
|
$this->filters = $filters;
|
2018-01-11 00:48:02 +00:00
|
|
|
$this->outputFile = $outputFile;
|
2018-01-15 09:24:38 +00:00
|
|
|
$this->writeFile = $writeFile;
|
2018-01-11 00:48:02 +00:00
|
|
|
|
|
|
|
$this->build();
|
2017-11-04 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 00:54:59 +01:00
|
|
|
public function __toString(): string
|
2017-12-30 13:42:23 +00:00
|
|
|
{
|
|
|
|
return $this->build();
|
|
|
|
}
|
|
|
|
|
2018-01-15 09:24:38 +00:00
|
|
|
/**
|
|
|
|
* Returns the generated XML.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-04-11 00:54:59 +01:00
|
|
|
public function getXml(): string
|
2018-01-15 09:24:38 +00:00
|
|
|
{
|
|
|
|
return $this->xml;
|
|
|
|
}
|
|
|
|
|
2017-12-30 10:56:07 +00:00
|
|
|
/**
|
|
|
|
* Build XML for a set of filters.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-04-11 00:54:59 +01:00
|
|
|
private function build(): void
|
2017-11-04 00:32:39 +00:00
|
|
|
{
|
2018-01-05 00:55:22 +00:00
|
|
|
$prefix = "<?xml version='1.0' encoding='UTF-8'?>" . PHP_EOL . "<feed xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>";
|
2017-12-30 10:28:19 +00:00
|
|
|
$suffix = '</feed>';
|
|
|
|
|
|
|
|
$xml = collect($this->filters)->map(function ($items) {
|
|
|
|
return $this->buildEntry($items);
|
2018-01-04 23:22:23 +00:00
|
|
|
})->implode(PHP_EOL);
|
2017-12-30 10:28:19 +00:00
|
|
|
|
2018-01-15 09:24:38 +00:00
|
|
|
$this->xml = collect([$prefix, $xml, $suffix])->implode(PHP_EOL);
|
2018-01-11 00:48:02 +00:00
|
|
|
|
2018-01-15 09:24:38 +00:00
|
|
|
if ($this->writeFile) {
|
|
|
|
$this->filesystem->dumpFile($this->outputFile, $this->xml);
|
|
|
|
}
|
2017-12-30 10:28:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-30 13:42:23 +00:00
|
|
|
* Build XML for an filter.
|
2017-12-30 10:56:07 +00:00
|
|
|
*
|
2017-12-30 13:42:23 +00:00
|
|
|
* @param Filter $filter
|
2017-12-30 10:28:19 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-04-11 00:54:59 +01:00
|
|
|
private function buildEntry(Filter $filter): string
|
2017-12-30 10:28:19 +00:00
|
|
|
{
|
2019-04-11 00:54:59 +01:00
|
|
|
$entry = collect($filter->toArray())
|
|
|
|
->map(function ($value, $key): string {
|
|
|
|
return $this->buildProperty($value, $key);
|
|
|
|
})
|
|
|
|
->implode(PHP_EOL);
|
2017-12-30 10:28:19 +00:00
|
|
|
|
2018-01-05 00:55:22 +00:00
|
|
|
return collect(['<entry>', $entry, '</entry>'])->implode(PHP_EOL);
|
2017-12-30 10:28:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build XML for a property.
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @param string $key
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-04-11 00:54:59 +01:00
|
|
|
private function buildProperty($value, $key): string
|
2017-12-30 10:28:19 +00:00
|
|
|
{
|
2017-12-30 18:21:15 +00:00
|
|
|
if (collect(['from', 'to'])->contains($key)) {
|
2018-01-05 00:02:14 +00:00
|
|
|
$value = $this->implode($value);
|
2017-12-30 10:28:19 +00:00
|
|
|
}
|
|
|
|
|
2017-12-30 18:20:24 +00:00
|
|
|
return vsprintf("<apps:property name='%s' value='%s'/>", [
|
|
|
|
$key,
|
2018-01-05 00:31:59 +00:00
|
|
|
htmlentities($this->implode($value)),
|
2017-12-30 18:20:24 +00:00
|
|
|
]);
|
2017-11-04 00:32:39 +00:00
|
|
|
}
|
2018-01-05 00:02:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implode values with the appropriate prefix, suffix and separator.
|
|
|
|
*/
|
2019-04-11 00:54:59 +01:00
|
|
|
private function implode($value, $separator = '|'): string
|
2018-01-05 00:02:14 +00:00
|
|
|
{
|
2018-01-05 00:31:59 +00:00
|
|
|
if (is_string($value)) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($value) && count($value) === 1) {
|
|
|
|
return reset($value);
|
|
|
|
}
|
|
|
|
|
2018-01-05 00:02:14 +00:00
|
|
|
return sprintf('(%s)', collect($value)->implode($separator));
|
|
|
|
}
|
2017-11-04 00:32:39 +00:00
|
|
|
}
|