1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Integration\RepositoryForms; |
6
|
|
|
|
7
|
|
|
use eZ\Publish\API\Repository\Values\ContentType\ContentType; |
8
|
|
|
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition; |
9
|
|
|
use eZ\Publish\API\Repository\Values\ValueObject; |
10
|
|
|
use eZ\Publish\SPI\Search\Field; |
11
|
|
|
use EzSystems\RepositoryForms\Data\Content\FieldData; |
12
|
|
|
use EzSystems\RepositoryForms\Data\Mapper\FormDataMapperInterface; |
13
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
14
|
|
|
|
15
|
|
|
class InformationCollectionMapper implements FormDataMapperInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Maps a ValueObject from eZ content repository to a data usable as underlying form data (e.g. create/update struct). |
19
|
|
|
* |
20
|
|
|
* @param \eZ\Publish\API\Repository\Values\ValueObject|\eZ\Publish\API\Repository\Values\Content\Content $contentDraft |
21
|
|
|
* @param array $params |
22
|
|
|
* |
23
|
|
|
* @return InformationCollectionData |
24
|
|
|
*/ |
25
|
|
|
public function mapToFormData(ValueObject $contentDraft, array $params = []) |
26
|
|
|
{ |
27
|
|
|
$optionsResolver = new OptionsResolver(); |
28
|
|
|
$this->configureOptions($optionsResolver); |
29
|
|
|
|
30
|
|
|
$params = $optionsResolver->resolve($params); |
31
|
|
|
$languageCode = $params['languageCode']; |
32
|
|
|
|
33
|
|
|
$data = new InformationCollectionData(['contentDraft' => $contentDraft]); |
34
|
|
|
$data->initialLanguageCode = $languageCode; |
35
|
|
|
|
36
|
|
|
$fields = $contentDraft->getFieldsByLanguage($languageCode); |
|
|
|
|
37
|
|
|
/** @var FieldDefinition $fieldDef */ |
38
|
|
|
foreach ($params['contentType']->fieldDefinitions as $fieldDef) { |
39
|
|
|
if ($fieldDef->isInfoCollector) { |
40
|
|
|
$field = $fields[$fieldDef->identifier]; |
41
|
|
|
$data->addFieldData(new FieldData([ |
42
|
|
|
'fieldDefinition' => $fieldDef, |
43
|
|
|
'field' => $field, |
44
|
|
|
'value' => $field->value, |
45
|
|
|
])); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $data; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function configureOptions(OptionsResolver $optionsResolver) |
53
|
|
|
{ |
54
|
|
|
$optionsResolver |
55
|
|
|
->setRequired(['languageCode', 'contentType']) |
56
|
|
|
->setAllowedTypes('contentType', ContentType::class); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: