mirror of
https://github.com/opdavies/gmail-filter-builder.git
synced 2025-01-22 20:17:31 +00:00
Merge pull request #10 from opdavies/use-symfony-components
Use symfony components
This commit is contained in:
commit
3e3cd43b10
|
@ -1,14 +1,4 @@
|
|||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
use Opdavies\GmailFilterBuilder\Builder;
|
||||
|
||||
if (file_exists(__DIR__.'/../../../../filters.php')) {
|
||||
$filters = require(__DIR__.'/../../../../filters.php');
|
||||
} elseif (file_exists(__DIR__.'/../filters.php')) {
|
||||
$filters = require(__DIR__.'/../filters.php');
|
||||
} else {
|
||||
throw new \Exception('No filters.php file found.');
|
||||
}
|
||||
|
||||
echo new Builder($filters);
|
||||
require_once __DIR__.'/generate-filters.php';
|
||||
|
|
14
bin/generate-filters.php
Normal file
14
bin/generate-filters.php
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
use Opdavies\GmailFilterBuilder\Console\Command\GenerateCommand;
|
||||
use Opdavies\GmailFilterBuilder\Container\Container;
|
||||
use Symfony\Component\Console\Application;
|
||||
|
||||
require_once __DIR__.'/../vendor/autoload.php';
|
||||
|
||||
$container = new Container();
|
||||
|
||||
/** @var Application $application */
|
||||
$application = $container->get('app.cli');
|
||||
$application->setDefaultCommand(GenerateCommand::NAME);
|
||||
$application->run();
|
|
@ -10,20 +10,34 @@
|
|||
}
|
||||
],
|
||||
"require": {
|
||||
"symfony/config": "^3.4",
|
||||
"symfony/console": "^3.4",
|
||||
"symfony/dependency-injection": "^3.4",
|
||||
"symfony/filesystem": "^3.4",
|
||||
"symfony/yaml": "^3.4",
|
||||
"tightenco/collect": "^5.4"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^5.7"
|
||||
"phpunit/phpunit": "^5.7",
|
||||
"symfony/var-dumper": "^3.4"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Opdavies\\GmailFilterBuilder\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Tests\\Opdavies\\GmailFilterBuilder\\": "tests/"
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.1-dev"
|
||||
}
|
||||
},
|
||||
"bin": ["bin/generate-filters"]
|
||||
"bin": ["bin/generate-filters"],
|
||||
"config": {
|
||||
"sort-packages": true
|
||||
}
|
||||
}
|
||||
|
|
13
config/services.yml
Normal file
13
config/services.yml
Normal file
|
@ -0,0 +1,13 @@
|
|||
services:
|
||||
app.cli:
|
||||
class: Symfony\Component\Console\Application
|
||||
autowire: true
|
||||
|
||||
app.builder:
|
||||
class: Opdavies\GmailFilterBuilder\Service\Builder
|
||||
|
||||
app.generate.command:
|
||||
class: Opdavies\GmailFilterBuilder\Console\Command\GenerateCommand
|
||||
autowire: true
|
||||
tags:
|
||||
- { name: ConsoleCommand }
|
|
@ -2,89 +2,11 @@
|
|||
|
||||
namespace Opdavies\GmailFilterBuilder;
|
||||
|
||||
class Builder
|
||||
use Opdavies\GmailFilterBuilder\Service\Builder as NewBuilder;
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
class Builder extends NewBuilder
|
||||
{
|
||||
/**
|
||||
* @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));
|
||||
}
|
||||
}
|
||||
|
|
58
src/Console/Command/GenerateCommand.php
Normal file
58
src/Console/Command/GenerateCommand.php
Normal file
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
namespace Opdavies\GmailFilterBuilder\Console\Command;
|
||||
|
||||
use Opdavies\GmailFilterBuilder\Builder;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\Filesystem\Exception\IOException;
|
||||
|
||||
class GenerateCommand extends Command
|
||||
{
|
||||
const NAME = 'generate';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function configure()
|
||||
{
|
||||
$this
|
||||
->setName(self::NAME)
|
||||
->setDefinition([
|
||||
new InputOption('input-file', null, InputOption::VALUE_OPTIONAL, 'The name of the PHP file containing your filters.', 'filters.php'),
|
||||
new InputOption('output-file', null, InputOption::VALUE_OPTIONAL, 'The name of the XML file to generate.', 'filters.xml')
|
||||
])
|
||||
->setDescription('Generates XML for Gmail filters.')
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$inputFile = $input->getOption('input-file');
|
||||
$outputFile = $input->getOption('output-file');
|
||||
|
||||
if (file_exists(__DIR__.'/../../../../'.$inputFile)) {
|
||||
$filters = require(__DIR__.'/../../../../'.$inputFile);
|
||||
} elseif (file_exists(__DIR__.'/../../../'.$inputFile)) {
|
||||
$filters = require(__DIR__.'/../../../'.$inputFile);
|
||||
} else {
|
||||
throw new \Exception('No filters.php file found.');
|
||||
}
|
||||
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
|
||||
try {
|
||||
new Builder($filters, $outputFile);
|
||||
|
||||
$io->success(sprintf('%s file generated.', $outputFile));
|
||||
} catch (IOException $e) {
|
||||
$io->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
22
src/Container/CommandCompilerClass.php
Normal file
22
src/Container/CommandCompilerClass.php
Normal file
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace Opdavies\GmailFilterBuilder\Container;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
class CommandCompilerClass implements CompilerPassInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function process(ContainerBuilder $container) {
|
||||
$definition = $container->findDefinition('app.cli');
|
||||
$taggedServices = $container->findTaggedServiceIds('ConsoleCommand');
|
||||
|
||||
foreach ($taggedServices as $id => $tags) {
|
||||
$definition->addMethodCall('add', [new Reference($id)]);
|
||||
}
|
||||
}
|
||||
}
|
24
src/Container/Container.php
Normal file
24
src/Container/Container.php
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
namespace Opdavies\GmailFilterBuilder\Container;
|
||||
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
|
||||
|
||||
class Container
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->containerBuilder = new ContainerBuilder();
|
||||
$loader = new YamlFileLoader($this->containerBuilder, new FileLocator(__DIR__.'/../../config'));
|
||||
$loader->load('services.yml');
|
||||
$this->containerBuilder->addCompilerPass(new CommandCompilerClass());
|
||||
$this->containerBuilder->compile();
|
||||
}
|
||||
|
||||
public function get($className)
|
||||
{
|
||||
return $this->containerBuilder->get($className);
|
||||
}
|
||||
}
|
241
src/Filter.php
241
src/Filter.php
|
@ -2,240 +2,11 @@
|
|||
|
||||
namespace Opdavies\GmailFilterBuilder;
|
||||
|
||||
class Filter
|
||||
use Opdavies\GmailFilterBuilder\Model\Filter as NewFilter;
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
class Filter extends NewFilter
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $properties = [];
|
||||
|
||||
/**
|
||||
* @return static
|
||||
*/
|
||||
public static function create()
|
||||
{
|
||||
return new static();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function has($value)
|
||||
{
|
||||
$this->properties['hasTheWord'] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function hasNot($value)
|
||||
{
|
||||
$this->properties['doesNotHaveTheWord'] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|array $values
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function from($values)
|
||||
{
|
||||
$this->properties['from'] = collect($values)->map(function ($value) {
|
||||
return trim($value);
|
||||
})->all();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|array $values
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function to($values)
|
||||
{
|
||||
$this->properties['to'] = collect($values)->map(function ($value) {
|
||||
return trim($value);
|
||||
})->all();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $subject
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function subject($subject)
|
||||
{
|
||||
$this->properties['subject'] = $subject;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function hasAttachment()
|
||||
{
|
||||
$this->properties['hasAttachment'] = 'true';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function excludeChats()
|
||||
{
|
||||
$this->properties['excludeChats'] = 'true';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $label
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function label($label)
|
||||
{
|
||||
$this->properties['label'] = $label;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function archive()
|
||||
{
|
||||
$this->properties['shouldArchive'] = 'true';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $label
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function labelAndArchive($label)
|
||||
{
|
||||
$this->label($label)->archive();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function spam()
|
||||
{
|
||||
$this->properties['shouldSpam'] = 'true';
|
||||
$this->properties['shouldNeverSpam'] = 'false';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function neverSpam()
|
||||
{
|
||||
$this->properties['shouldSpam'] = 'false';
|
||||
$this->properties['shouldNeverSpam'] = 'true';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function trash()
|
||||
{
|
||||
$this->properties['shouldTrash'] = 'true';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function read()
|
||||
{
|
||||
$this->properties['markAsRead'] = 'true';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function star()
|
||||
{
|
||||
$this->properties['shouldStar'] = 'true';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $to
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function forward($to)
|
||||
{
|
||||
$this->properties['forwardTo'] = $to;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function important()
|
||||
{
|
||||
$this->properties['shouldAlwaysMarkAsImportant'] = 'true';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function notImportant()
|
||||
{
|
||||
$this->properties['shouldNeverMarkAsImportant'] = 'true';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $category
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function categorise($category)
|
||||
{
|
||||
$this->properties['smartLabelToApply'] = $category;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getProperties()
|
||||
{
|
||||
return $this->properties;
|
||||
}
|
||||
}
|
||||
|
|
241
src/Model/Filter.php
Normal file
241
src/Model/Filter.php
Normal file
|
@ -0,0 +1,241 @@
|
|||
<?php
|
||||
|
||||
namespace Opdavies\GmailFilterBuilder\Model;
|
||||
|
||||
class Filter
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $properties = [];
|
||||
|
||||
/**
|
||||
* @return static
|
||||
*/
|
||||
public static function create()
|
||||
{
|
||||
return new static();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function has($value)
|
||||
{
|
||||
$this->properties['hasTheWord'] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function hasNot($value)
|
||||
{
|
||||
$this->properties['doesNotHaveTheWord'] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|array $values
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function from($values)
|
||||
{
|
||||
$this->properties['from'] = collect($values)->map(function ($value) {
|
||||
return trim($value);
|
||||
})->all();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|array $values
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function to($values)
|
||||
{
|
||||
$this->properties['to'] = collect($values)->map(function ($value) {
|
||||
return trim($value);
|
||||
})->all();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $subject
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function subject($subject)
|
||||
{
|
||||
$this->properties['subject'] = $subject;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function hasAttachment()
|
||||
{
|
||||
$this->properties['hasAttachment'] = 'true';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function excludeChats()
|
||||
{
|
||||
$this->properties['excludeChats'] = 'true';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $label
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function label($label)
|
||||
{
|
||||
$this->properties['label'] = $label;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function archive()
|
||||
{
|
||||
$this->properties['shouldArchive'] = 'true';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $label
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function labelAndArchive($label)
|
||||
{
|
||||
$this->label($label)->archive();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function spam()
|
||||
{
|
||||
$this->properties['shouldSpam'] = 'true';
|
||||
$this->properties['shouldNeverSpam'] = 'false';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function neverSpam()
|
||||
{
|
||||
$this->properties['shouldSpam'] = 'false';
|
||||
$this->properties['shouldNeverSpam'] = 'true';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function trash()
|
||||
{
|
||||
$this->properties['shouldTrash'] = 'true';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function read()
|
||||
{
|
||||
$this->properties['markAsRead'] = 'true';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function star()
|
||||
{
|
||||
$this->properties['shouldStar'] = 'true';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $to
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function forward($to)
|
||||
{
|
||||
$this->properties['forwardTo'] = $to;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function important()
|
||||
{
|
||||
$this->properties['shouldAlwaysMarkAsImportant'] = 'true';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function notImportant()
|
||||
{
|
||||
$this->properties['shouldNeverMarkAsImportant'] = 'true';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $category
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function categorise($category)
|
||||
{
|
||||
$this->properties['smartLabelToApply'] = $category;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getProperties()
|
||||
{
|
||||
return $this->properties;
|
||||
}
|
||||
}
|
128
src/Service/Builder.php
Normal file
128
src/Service/Builder.php
Normal file
|
@ -0,0 +1,128 @@
|
|||
<?php
|
||||
|
||||
namespace Opdavies\GmailFilterBuilder\Service;
|
||||
|
||||
use Opdavies\GmailFilterBuilder\Filter;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
|
||||
class Builder
|
||||
{
|
||||
private $filesystem;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $filters = [];
|
||||
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
private $outputFile;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $writeFile;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $xml;
|
||||
|
||||
public function __construct(array $filters, $outputFile = 'filters.xml', $writeFile = true) {
|
||||
$this->filesystem = new Filesystem();
|
||||
$this->filters = $filters;
|
||||
$this->outputFile = $outputFile;
|
||||
$this->writeFile = $writeFile;
|
||||
|
||||
$this->build();
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the generated XML.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getXml()
|
||||
{
|
||||
return $this->xml;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
$this->xml = collect([$prefix, $xml, $suffix])->implode(PHP_EOL);
|
||||
|
||||
if ($this->writeFile) {
|
||||
$this->filesystem->dumpFile($this->outputFile, $this->xml);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
}
|
|
@ -18,7 +18,7 @@ class BuilderTest extends TestCase
|
|||
->star()
|
||||
->important();
|
||||
|
||||
$result = new Builder([$filterA, $filterB]);
|
||||
$result = new Builder([$filterA, $filterB], '', false);
|
||||
|
||||
$expected = <<<EOF
|
||||
<?xml version='1.0' encoding='UTF-8'?>
|
||||
|
@ -36,6 +36,6 @@ class BuilderTest extends TestCase
|
|||
</feed>
|
||||
EOF;
|
||||
|
||||
$this->assertEquals($expected, $result->__toString());
|
||||
$this->assertEquals($expected, $result->getXml());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue