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

[TASK] Add data attributes formatting in the asset handler #72

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both \Throwable and \Exception should be caught for PHP < 7 compatibility.

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;
}
}