Skip to content

Commit

Permalink
Move Property Mapper from Extbase Fluid Book (#2074)
Browse files Browse the repository at this point in the history
* Move Property Mapper from Extbase Fluid Book

Releases: main, 11.5

* Update Documentation/ExtensionArchitecture/Extbase/Reference/Controller/PropertyMapping.rst

Co-authored-by: Chris Müller <[email protected]>

* Include real code

Remove Example "Allow mapping of sub-properties"

here is no method propertyMappingConfigurationBuilder in the Abstract Controller anymore and the PropertyMappingConfigurationBuilder is internal. Therefore removing the section for now

Co-authored-by: lina.wolf <[email protected]>
Co-authored-by: Chris Müller <[email protected]>
  • Loading branch information
3 people authored Aug 25, 2022
1 parent 9b909d3 commit 3c74441
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Documentation/CodeSnippets/Config/Extbase.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,36 @@
'sourceFile'=> 'typo3conf/ext/blog_example/Configuration/Extbase/Persistence/Classes.php',
'targetFileName' => 'Extbase/Persistence/ExtbasePersistenceClasses.rst.txt'
],
[
'action'=> 'createPhpClassCodeSnippet',
'class'=> FriendsOfTYPO3\BlogExample\Controller\PostController::class,
'members' => [
'mapIntegerFromString',
],
'withComment' => true,
'withClassComment' => false,
'targetFileName' => 'Extbase/PropertyManager/IntegerMapping.rst.txt',
],
[
'action'=> 'createPhpClassCodeSnippet',
'class'=> FriendsOfTYPO3\BlogExample\Controller\PostController::class,
'members' => [
'mapTagFromString',
],
'withComment' => true,
'withClassComment' => false,
'targetFileName' => 'Extbase/PropertyManager/ObjectMapping.rst.txt',
],
[
'action'=> 'createPhpClassCodeSnippet',
'class'=> FriendsOfTYPO3\BlogExample\Controller\PostController::class,
'members' => [
'__construct',
],
'withComment' => true,
'withClassComment' => false,
'targetFileName' => 'Extbase/PropertyManager/PropertyMapperInjection.rst.txt',
],
[
'action'=> 'createPhpClassCodeSnippet',
'class'=> FriendsOfTYPO3\BlogExample\Controller\PostController::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.. Generated by https://github.com/TYPO3-Documentation/t3docs-codesnippets
.. Extracted from EXT:blog_example/Classes/Controller/PostController.php
.. code-block:: php
:caption: EXT:blog_example/Classes/Controller/PostController.php
class PostController extends \FriendsOfTYPO3\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');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.. Generated by https://github.com/TYPO3-Documentation/t3docs-codesnippets
.. Extracted from EXT:blog_example/Classes/Controller/PostController.php
.. code-block:: php
:caption: EXT:blog_example/Classes/Controller/PostController.php
use FriendsOfTYPO3\BlogExample\Domain\Model\Tag;
class PostController extends \FriendsOfTYPO3\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
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.. Generated by https://github.com/TYPO3-Documentation/t3docs-codesnippets
.. Extracted from EXT:blog_example/Classes/Controller/PostController.php
.. code-block:: php
:caption: EXT:blog_example/Classes/Controller/PostController.php
use FriendsOfTYPO3\BlogExample\Domain\Repository\BlogRepository;
use FriendsOfTYPO3\BlogExample\Domain\Repository\PersonRepository;
use FriendsOfTYPO3\BlogExample\Domain\Repository\PostRepository;
use TYPO3\CMS\Extbase\Property\PropertyMapper;
class PostController extends \FriendsOfTYPO3\BlogExample\Controller\AbstractController
{
/**
* PostController constructor.
*
* Takes care of dependency injection
*/
public function __construct(
protected readonly BlogRepository $blogRepository,
protected readonly PersonRepository $personRepository,
protected readonly PostRepository $postRepository,
protected readonly PropertyMapper $propertyMapper
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.. include:: /Includes.rst.txt
.. index::
Extbase; PropertyMapper
.. _extbase_property_mapping:

===================
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:

.. include:: /CodeSnippets/Extbase/PropertyManager/IntegerMapping.rst.txt

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

.. note::
The :php:`PropertyMapper` has to be injected before it can be used:

.. include:: /CodeSnippets/Extbase/PropertyManager/PropertyMapperInjection.rst.txt


How to use property mappers
===========================

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

.. include:: /CodeSnippets/Extbase/PropertyManager/ObjectMapping.rst.txt

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

.. note::
The property mapper will not check the validation rules. The result will be
whatever the input is.

0 comments on commit 3c74441

Please sign in to comment.