From d2039893b3ca643e7ef0c194b0312885dd0268ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 28 Dec 2015 00:38:27 +0100 Subject: [PATCH] Simplifiying Helper and twig functions Now, the CustomFieldsHelper methods and the twig function which render customFields require always a `Chill\CustomFields\Entity\CustomField`. The possibility to add a slug and an object as argument to those method is abandoned, and was not used in other bundle (nevertheless, the method to catch a customField from his slug was buggy). --- Service/CustomFieldsHelper.php | 88 +------------------ Service/CustomFieldsHelperException.php | 4 +- Templating/Twig/CustomFieldRenderingTwig.php | 18 ++-- Tests/Service/CustomFieldsHelperTest.php | 90 ++++++++++++++++++++ 4 files changed, 102 insertions(+), 98 deletions(-) create mode 100644 Tests/Service/CustomFieldsHelperTest.php diff --git a/Service/CustomFieldsHelper.php b/Service/CustomFieldsHelper.php index 07c8c4f04..7baa48616 100644 --- a/Service/CustomFieldsHelper.php +++ b/Service/CustomFieldsHelper.php @@ -45,10 +45,6 @@ class CustomFieldsHelper * fields */ private $provider; - /** @var array $cache Matrix to cache all the custom fields. The array - * is indexed by the EntityClass then the slug */ - private $cache = array(); - /** * Constructor * @@ -63,83 +59,8 @@ class CustomFieldsHelper $this->provider = $provider; } - /** - * Set in cache all the custom fields of a given class containing some - * custom fields. - * - * @param object|string $class The given class. - * @todo check if this fucntions has to call _cacheCustomFieldsGroup instead of - * _cacheCustomFields ? - */ - private function _cacheCustomFields($class) + public function isEmptyValue(array $fields, CustomField $customField) { - $customFieldsGroups = $this->em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup') - ->findBy(array('entity' => (is_string($class)) ? $class : get_class($class))); - - if (!$customFieldsGroups) { - throw CustomFieldsHelperException::customFieldsGroupNotFound((is_string($class)) ? $class : get_class($class)); - } - - foreach ($customFieldsGroup as $cfGroup) { - $this->_cacheCustomFields($cfGroup); - } - } - - /** - * Set in cache of the custom fields of a customfield Group. - * - * @param CustomFieldsGroup $group The given CustomFieldsGroup - */ - private function _cacheCustomFieldsGroup(CustomFieldsGroup $group) - { - foreach ($group->getCustomFields() as $field) { - $this->cache[$group->getEntity()][$field->getSlug()] = $field; - } - } - - /** - * Return a requested customField - * - * @param object|string $class The requested class - * @param string $slug The slug. BEWARE If the slug is null, throw a proper CustomFieldsHelperException - * @return CustomField The requested CustomField - * @throws CustomFieldsHelperException if $slug is null - */ - public function getCustomField($class, $slug = null) - { - if (!$slug) { - throw CustomFieldsHelperException::slugIsMissing(); - } - - $resolveClass = (is_string($class)) ? $class : get_class($class); - if (!$this->cache[$resolveClass][$slug]) { - $this->_cacheCustomFields($resolveClass); - } - - return $this->cache[$resolveClass][$slug]; - } - - /** - * Return the stored/raw value of a custom field. - * - * The method return null if the slug is not recorded. - * - * @param array $fields the **raw** array, as stored in the db - * @param object|string $class - * @param string $slug - * @return mixed|null The value or null if the slug is not recorded - */ - private function getCustomFieldValue(array $fields, $class, $slug) - { - return (isset($fields[$slug])) ? $this->provider - ->getCustomFieldByType($this->getCustomField($class, $slug)->getType()) - ->deserialize($fields[$slug]) - : null; - } - - public function isEmptyValue(array $fields, $classOrCustomField, $slug = null) - { - $customField = ($classOrCustomField instanceof CustomField) ? $classOrCustomField : $this->getCustomField($classOrCustomField, $slug); $slug = $customField->getSlug(); $rawValue = (isset($fields[$slug])) ? $fields[$slug] : null; @@ -152,16 +73,13 @@ class CustomFieldsHelper * Render the value of a custom field * * @param array $fields the **raw** array, as stored in the db - * @param CustomField|object|string $classOrCustomField the object OR the get_class($object) string OR The CustomField + * @param CustomField $customField the customField entity * @param string $documentType The type of document in which the rendered value is displayed ('html' or 'csv'). - * @param string $slug The slug of the custom field to render. - * @param boolean $showIfEmpty If the widget must be rendered if the value is empty. An empty value is all values described as http://php.net/manual/fr/function.empty.php, except `FALSE` * @throws CustomFieldsHelperException if slug is missing * @return The representation of the value the customField. */ - public function renderCustomField(array $fields, $classOrCustomField, $documentType='html', $slug = null, $showIfEmpty = true) + public function renderCustomField(array $fields, CustomField $customField, $documentType='html') { - $customField = ($classOrCustomField instanceof CustomField) ? $classOrCustomField : $this->getCustomField($classOrCustomField, $slug); $slug = $customField->getSlug(); $rawValue = (isset($fields[$slug])) ? $fields[$slug] : null; $customFieldType = $this->provider->getCustomFieldByType($customField->getType()); diff --git a/Service/CustomFieldsHelperException.php b/Service/CustomFieldsHelperException.php index f43e5cc36..497c96720 100644 --- a/Service/CustomFieldsHelperException.php +++ b/Service/CustomFieldsHelperException.php @@ -26,11 +26,11 @@ class CustomFieldsHelperException extends \Exception { public static function customFieldsGroupNotFound($entity) { - return new CustomFieldsRenderingException("The customFieldsGroups associated with $entity are not found"); + return new CustomFieldsHelperException("The customFieldsGroups associated with $entity are not found"); } public static function slugIsMissing() { - return new CustomFieldsRenderingException("The slug is missing"); + return new CustomFieldsHelperException("The slug is missing"); } } \ No newline at end of file diff --git a/Templating/Twig/CustomFieldRenderingTwig.php b/Templating/Twig/CustomFieldRenderingTwig.php index 8561d26f8..32246cbd7 100644 --- a/Templating/Twig/CustomFieldRenderingTwig.php +++ b/Templating/Twig/CustomFieldRenderingTwig.php @@ -85,10 +85,10 @@ class CustomFieldRenderingTwig extends \Twig_Extension implements ContainerAware } - public function isEmptyValue($customFieldorClass, $fields, $slug = null) + public function isEmptyValue(CustomField $customField, $fields, $slug = null) { return $this->container->get('chill.custom_field.helper') - ->isEmptyValue($fields, $customFieldorClass); + ->isEmptyValue($fields, $customField); } /* (non-PHPdoc) @@ -102,19 +102,15 @@ class CustomFieldRenderingTwig extends \Twig_Extension implements ContainerAware /** * Twig Extension that is used to render the label of a custom field. * - * @param CustomField|object|string $customFieldOrClass Either a customField OR a customizable_entity OR the FQDN of the entity + * @param CustomField $customField Either a customField OR a customizable_entity OR the FQDN of the entity * @param string $slug The slug ONLY necessary if the first argument is NOT a CustomField instance * @param array $params The parameters for rendering. Currently, 'label_layout' allow to choose a different label. Default is 'ChillCustomFieldsBundle:CustomField:render_label.html.twig' * @return string HTML representation of the custom field label. */ - public function renderLabel($customFieldOrClass, $slug = null, array $params = array()) + public function renderLabel(CustomField $customField, $slug = null, array $params = array()) { $resolvedParams = array_merge($this->defaultParams, $params); - $customField = ($customFieldOrClass instanceof CustomField) - ? $customFieldOrClass : $this->container->get('chill.custom_field.provider') - ->getCustomField($customFieldOrClass, $slug); - return $this->container->get('templating') ->render($resolvedParams['label_layout'], array('customField' => $customField)); } @@ -125,14 +121,14 @@ class CustomFieldRenderingTwig extends \Twig_Extension implements ContainerAware * The presentation of the value is influenced by the document type. * * @param array $fields The array raw, as stored in the db - * @param CustomField|object|string $customFieldOrClass Either a customField OR a customizable_entity OR the FQDN of the entity + * @param CustomField $customField Either a customField OR a customizable_entity OR the FQDN of the entity * @param string $documentType The type of the document (csv, html) * @param string $slug The slug of the custom field ONLY necessary if the first argument is NOT a CustomField instance * @return string HTML representation of the custom field value, as described in the CustomFieldInterface. Is HTML safe */ - public function renderWidget(array $fields, $customFieldOrClass, $documentType='html', $slug = null) + public function renderWidget(array $fields, CustomField $customField, $documentType='html') { return $this->container->get('chill.custom_field.helper') - ->renderCustomField($fields, $customFieldOrClass, $documentType, $slug); + ->renderCustomField($fields, $customField, $documentType); } } \ No newline at end of file diff --git a/Tests/Service/CustomFieldsHelperTest.php b/Tests/Service/CustomFieldsHelperTest.php new file mode 100644 index 000000000..82092bf1c --- /dev/null +++ b/Tests/Service/CustomFieldsHelperTest.php @@ -0,0 +1,90 @@ + + * + * 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\CustomFields\Tests\Service; + +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Chill\CustomFieldsBundle\Service\CustomFieldsHelper; +use Chill\CustomFieldsBundle\Entity\CustomField; + +/** + * Tests for custom fields helper + * + * @author Julien Fastré + * @author Champs Libres + */ +class CustomFieldsHelperTest extends KernelTestCase +{ + /** + * + * @var CustomFieldsHelper + */ + private $cfHelper; + + /** + * + * @var CustomField + */ + private $randomCFText; + + public function setUp() + { + self::bootKernel(); + + $container = self::$kernel->getContainer(); + + $this->cfHelper = $container->get('chill.custom_field.helper'); + + $cfieldsText = $container->get('doctrine.orm.entity_manager') + ->getRepository('ChillCustomFieldsBundle:CustomField') + ->findBy(array('type' => 'text')) + ; + $this->randomCFText = end($cfieldsText); + } + + public function testRenderCustomField() + { + $data = array( + $this->randomCFText->getSlug() => 'Sample text' + ); + + $text = $this->cfHelper->renderCustomField($data, $this->randomCFText); + + $this->assertContains('Sample text', $text); + } + + public function testIsEmptyValue() + { + // not empty value + $data = array( + $this->randomCFText->getSlug() => 'Sample text' + ); + + $this->assertFalse($this->cfHelper->isEmptyValue($data, $this->randomCFText)); + + //empty value + $data = array( + $this->randomCFText->getSlug() => '' + ); + + $this->assertTrue($this->cfHelper->isEmptyValue($data, $this->randomCFText)); + } + +}