From 1c252eac433db41db8de8c637177bdf05c31af70 Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Tue, 3 Mar 2015 10:57:23 +0100 Subject: [PATCH] Move csvCellFilter into Templating\CSVCellTwig - close #416 --- Resources/config/services.yml | 5 ++ Templating/CSVCellTwig.php | 69 +++++++++++++++++++++++++++ Templating/TranslatableStringTwig.php | 21 ++++---- 3 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 Templating/CSVCellTwig.php diff --git a/Resources/config/services.yml b/Resources/config/services.yml index e0dcbf936..87523bd26 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -50,6 +50,11 @@ services: - [ setContainer, ["@service_container"]] tags: - { name: twig.extension } + + chill.main.twig.csv_cell: + class: Chill\MainBundle\Templating\CSVCellTwig + tags: + - { name: twig.extension } chill.main.form.type.select2choice: class: Chill\MainBundle\Form\Type\Select2ChoiceType diff --git a/Templating/CSVCellTwig.php b/Templating/CSVCellTwig.php new file mode 100644 index 000000000..0d65dd038 --- /dev/null +++ b/Templating/CSVCellTwig.php @@ -0,0 +1,69 @@ +, + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\MainBundle\Templating; + +/** + * Twig filter to transform a string in a safer way to be the content of a csv + * cell. + * + * This filter replace the char " by "" + */ +class CSVCellTwig extends \Twig_Extension +{ + /* + * Returns a list of filters to add to the existing list. + * + * (non-PHPdoc) + * @see Twig_Extension::getFilters() + */ + public function getFilters() + { + return array( + new \Twig_SimpleFilter( + 'csv_cell', + array($this, 'csvCellFilter'), + array('is_safe' => array('html'))) + ); + } + + /* + * Replace into a string the char " by "" + * + * @param String $content The input string. + * @return String The safe string. + */ + public function csvCellFilter($content) + { + return str_replace('"', '""', $content); + } + + /* + * Returns the name of the extension. + * + * @return The name of the extension. + */ + public function getName() + { + return 'chill_main_csv_cell'; + } +} diff --git a/Templating/TranslatableStringTwig.php b/Templating/TranslatableStringTwig.php index 593da7868..7d44b2ddb 100644 --- a/Templating/TranslatableStringTwig.php +++ b/Templating/TranslatableStringTwig.php @@ -29,26 +29,16 @@ class TranslatableStringTwig extends \Twig_Extension use ContainerAwareTrait; /* + * Returns a list of filters to add to the existing list. + * * (non-PHPdoc) * @see Twig_Extension::getFilters() */ public function getFilters() { return array( - new \Twig_SimpleFilter('localize_translatable_string', array($this, 'localize')), new \Twig_SimpleFilter( - 'csv_cell', - array($this, 'csvCellFilter'), - array('is_safe' => array('html'))) - ); - } - - /* - * - */ - public function csvCellFilter($content) - { - return str_replace('"', '""', $content); + 'localize_translatable_string', array($this, 'localize'))); } public function localize(array $translatableStrings) @@ -57,6 +47,11 @@ class TranslatableStringTwig extends \Twig_Extension ->localize($translatableStrings); } + /* + * Returns the name of the extension. + * + * @return The name of the extension. + */ public function getName() { return 'chill_main_localize';