gmail-filter-builder/src/Builder.php

67 lines
1.4 KiB
PHP
Raw Normal View History

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 10:56:07 +00:00
/**
* Build XML for a set of filters.
*
* @return string
*/
2017-11-04 00:32:39 +00:00
public function build()
{
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 10:56:07 +00:00
* Build XML for an entry.
*
2017-12-30 10:28:19 +00:00
* @param array $items
*
* @return string
*/
private function buildEntry(array $items)
{
$entry = collect($items)->map(function ($value, $key) {
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)
{
if ($key == 'from') {
$value = collect($value)->implode('|');
}
return "<apps:property name='{$key}' value='{$value}'/>";
2017-11-04 00:32:39 +00:00
}
}