Property mapping

Extbase provides a property mapper to convert different values, like integers or arrays, to other types, like strings or objects.

In this example, we provide a string that will be converted to an integer:

Class T3docs\BlogExample\Controller\PostController
class PostController extends \T3docs\BlogExample\Controller\AbstractController
{
    /**
     * This method demonstrates property mapping to an integer
     * @throws \TYPO3\CMS\Extbase\Property\Exception
     */
    protected function mapIntegerFromString(string $numberString = '42'): int
    {
        return $output = $this->propertyMapper->convert($numberString, 'integer');
    }
}
Copied!

Conversion is done by using the TYPO3\CMS\Extbase\Property\PropertyMapper::convert() method.

How to use property mappers

This example shows a simple conversion of a string into a model:

Class T3docs\BlogExample\Controller\PostController
use T3docs\BlogExample\Domain\Model\Tag;

class PostController extends \T3docs\BlogExample\Controller\AbstractController
{
    /**
     * This method demonstrates property mapping to an object
     * @throws \TYPO3\CMS\Extbase\Property\Exception
     */
    protected function mapTagFromString(string $tagString = 'some tag'): Tag
    {
        $input = [
            'name' => $tagString,
        ];
        return $this->propertyMapper->convert(
            $input,
            Tag::class,
        );
    }
}
Copied!

The result is a new instance of FriendsOfTYPO3\BlogExample\Domain\Model\Tag with defined property name.