Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
[TASK] Add data attributes formatting in the asset handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Mopolo authored and Nathan Boiron committed Nov 3, 2017
1 parent 384ebad commit bd614fa
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
33 changes: 30 additions & 3 deletions Classes/AssetHandler/Html/DataAttributesAssetHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@

namespace Romm\Formz\AssetHandler\Html;

use DateTime;
use Romm\Formz\AssetHandler\AbstractAssetHandler;
use Romm\Formz\Error\FormResult;
use Romm\Formz\Error\FormzMessageInterface;
use Romm\Formz\Exceptions\InvalidArgumentTypeException;
use Romm\Formz\Service\MessageService;
use Romm\Formz\Service\StringService;
use Throwable;
use Traversable;
use TYPO3\CMS\Extbase\Error\Result;
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;

Expand Down Expand Up @@ -56,9 +60,12 @@ public function getFieldsValuesDataAttributes(FormResult $formResult)

if (false === $formResult->fieldIsDeactivated($field)) {
$value = ObjectAccess::getProperty($formInstance, $fieldName);
$value = (is_array($value))
? implode(' ', $value)
: $value;

try {
$value = $this->formatValue($value);
} catch (Throwable $e) {
throw InvalidArgumentTypeException::dataAttributeValueNotFormattable($formInstance, $fieldName, $value);
}

if (false === empty($value)) {
$result[self::getFieldDataValueKey($fieldName)] = $value;
Expand Down Expand Up @@ -188,6 +195,26 @@ protected function addFieldMessageDataAttribute($fieldName, array $messages, $ty
return $result;
}

/**
* Checks the type of a given data attribute and formats it if needed.
*
* @param mixed $value
* @return array
*/
protected function formatValue($value)
{
if (is_array($value) || $value instanceof Traversable) {
$value = implode(',', $value);
} elseif ($value instanceof DateTime) {
$format = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] . ' ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'];
$value = $value->format($format);
} elseif (false === is_string($value)) {
$value = (string)$value;
}

return $value;
}

/**
* Formats the data value attribute key for a given field name.
*
Expand Down
25 changes: 25 additions & 0 deletions Classes/Exceptions/InvalidArgumentTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class InvalidArgumentTypeException extends FormzException

const CONDITION_CLASS_NAME_NOT_VALID = 'The condition class must implement "%s" (given class is "%s").';

const DATA_ATTRIBUTE_NOT_FORMATTABLE = 'The field "%s" in the form "%s" cannot be formatted. It needs to either implement `__toString()` or you need to use the `@formz-ignore` annotation in the property docblock.';

/**
* @code 1477468571
*
Expand Down Expand Up @@ -192,4 +194,27 @@ final public static function ajaxControllerWrongFormType($className)

return $exception;
}

/**
* @code 1509724328
*
* @param FormInterface $form
* @param $fieldName
* @param $value
* @return InvalidArgumentTypeException
*/
final public static function dataAttributeValueNotFormattable(FormInterface $form, $fieldName, $value)
{
/** @var self $exception */
$exception = self::getNewExceptionInstance(
self::DATA_ATTRIBUTE_NOT_FORMATTABLE,
[
$fieldName,
get_class($form),
$value,
]
);

return $exception;
}
}

0 comments on commit bd614fa

Please sign in to comment.