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).
This commit is contained in:
2015-12-28 00:38:27 +01:00
parent 89221599d4
commit d2039893b3
4 changed files with 102 additions and 98 deletions

View File

@@ -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());

View File

@@ -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");
}
}