mirror of
https://github.com/opdavies/gmail-filter-builder.git
synced 2025-09-05 13:05:34 +01:00
Start using Symfony Components
This commit is contained in:
parent
b61178b9b0
commit
bc3dae9c48
9 changed files with 131 additions and 13 deletions
90
src/Service/Builder.php
Normal file
90
src/Service/Builder.php
Normal file
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
|
||||
namespace Opdavies\GmailFilterBuilder;
|
||||
|
||||
class Builder
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $filters = [];
|
||||
|
||||
public function __construct(array $filters)
|
||||
{
|
||||
$this->filters = $filters;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Build XML for a set of filters.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function build()
|
||||
{
|
||||
$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'>";
|
||||
$suffix = '</feed>';
|
||||
|
||||
$xml = collect($this->filters)->map(function ($items) {
|
||||
return $this->buildEntry($items);
|
||||
})->implode(PHP_EOL);
|
||||
|
||||
return collect([$prefix, $xml, $suffix])->implode(PHP_EOL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build XML for an filter.
|
||||
*
|
||||
* @param Filter $filter
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function buildEntry(Filter $filter)
|
||||
{
|
||||
$entry = collect($filter->getProperties())->map(function ($value, $key) {
|
||||
return $this->buildProperty($value, $key);
|
||||
})->implode(PHP_EOL);
|
||||
|
||||
return collect(['<entry>', $entry, '</entry>'])->implode(PHP_EOL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build XML for a property.
|
||||
*
|
||||
* @param string $value
|
||||
* @param string $key
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function buildProperty($value, $key)
|
||||
{
|
||||
if (collect(['from', 'to'])->contains($key)) {
|
||||
$value = $this->implode($value);
|
||||
}
|
||||
|
||||
return vsprintf("<apps:property name='%s' value='%s'/>", [
|
||||
$key,
|
||||
htmlentities($this->implode($value)),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implode values with the appropriate prefix, suffix and separator.
|
||||
*/
|
||||
private function implode($value, $separator = '|')
|
||||
{
|
||||
if (is_string($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
if (is_array($value) && count($value) === 1) {
|
||||
return reset($value);
|
||||
}
|
||||
|
||||
return sprintf('(%s)', collect($value)->implode($separator));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue