2017-11-04 00:32:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Opdavies\GmailFilterBuilder;
|
|
|
|
|
|
|
|
class Builder
|
|
|
|
{
|
2017-12-30 10:56:07 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-11-04 00:32:39 +00:00
|
|
|
private $filters = [];
|
|
|
|
|
2017-12-30 10:56:07 +00:00
|
|
|
public function __construct(array $filters)
|
|
|
|
{
|
2017-11-04 00:32:39 +00:00
|
|
|
$this->filters = $filters;
|
|
|
|
}
|
|
|
|
|
2017-12-30 13:42:23 +00:00
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return $this->build();
|
|
|
|
}
|
|
|
|
|
2017-12-30 10:56:07 +00:00
|
|
|
/**
|
|
|
|
* Build XML for a set of filters.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-12-30 13:42:23 +00:00
|
|
|
private function build()
|
2017-11-04 00:32:39 +00:00
|
|
|
{
|
2017-12-30 10:28:19 +00:00
|
|
|
$prefix = "<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>";
|
|
|
|
$suffix = '</feed>';
|
|
|
|
|
|
|
|
$xml = collect($this->filters)->map(function ($items) {
|
|
|
|
return $this->buildEntry($items);
|
|
|
|
})->implode('');
|
|
|
|
|
|
|
|
return $prefix . $xml . $suffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*/
|
2017-12-30 13:42:23 +00:00
|
|
|
private function buildEntry(Filter $filter)
|
2017-12-30 10:28:19 +00:00
|
|
|
{
|
2017-12-30 13:42:23 +00:00
|
|
|
$entry = collect($filter->getProperties())->map(function ($value, $key) {
|
2017-12-30 10:28:19 +00:00
|
|
|
return $this->buildProperty($value, $key);
|
|
|
|
})->implode('');
|
|
|
|
|
|
|
|
return "<entry>{$entry}</entry>";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build XML for a property.
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @param string $key
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function buildProperty($value, $key)
|
|
|
|
{
|
2017-12-30 18:21:15 +00:00
|
|
|
if (collect(['from', 'to'])->contains($key)) {
|
2017-12-30 10:28:19 +00:00
|
|
|
$value = collect($value)->implode('|');
|
|
|
|
}
|
|
|
|
|
2017-12-30 18:20:24 +00:00
|
|
|
return vsprintf("<apps:property name='%s' value='%s'/>", [
|
|
|
|
$key,
|
|
|
|
htmlentities($value),
|
|
|
|
]);
|
2017-11-04 00:32:39 +00:00
|
|
|
}
|
|
|
|
}
|