1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace eZ\Publish\Core\Repository\Strategy\ContentThumbnail; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\FieldTypeService; |
12
|
|
|
use eZ\Publish\API\Repository\Values\Content\Field; |
13
|
|
|
use eZ\Publish\API\Repository\Values\Content\Thumbnail; |
14
|
|
|
use eZ\Publish\API\Repository\Values\ContentType\ContentType; |
15
|
|
|
use eZ\Publish\SPI\Repository\Strategy\ContentThumbnail\Field\ThumbnailStrategy as ContentFieldThumbnailStrategy; |
16
|
|
|
use eZ\Publish\SPI\Repository\Strategy\ContentThumbnail\ThumbnailStrategy; |
17
|
|
|
|
18
|
|
|
final class FirstMatchingFieldStrategy implements ThumbnailStrategy |
19
|
|
|
{ |
20
|
|
|
/** @var \eZ\Publish\API\Repository\FieldTypeService */ |
21
|
|
|
private $fieldTypeService; |
22
|
|
|
|
23
|
|
|
/** @var \eZ\Publish\SPI\Repository\Strategy\ContentThumbnail\Field\ThumbnailStrategy */ |
24
|
|
|
private $contentFieldStrategy; |
25
|
|
|
|
26
|
|
|
public function __construct( |
27
|
|
|
ContentFieldThumbnailStrategy $contentFieldStrategy, |
28
|
|
|
FieldTypeService $fieldTypeService |
29
|
|
|
) { |
30
|
|
|
$this->contentFieldStrategy = $contentFieldStrategy; |
31
|
|
|
$this->fieldTypeService = $fieldTypeService; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getThumbnail(ContentType $contentType, array $fields): ?Thumbnail |
35
|
|
|
{ |
36
|
|
|
$fieldDefinitions = $contentType->getFieldDefinitions(); |
37
|
|
|
|
38
|
|
|
foreach ($fieldDefinitions as $fieldDefinition) { |
39
|
|
|
$field = $this->getFieldByIdentifier($fieldDefinition->identifier, $fields); |
40
|
|
|
|
41
|
|
|
if ($field === null) { |
42
|
|
|
continue; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$fieldType = $this->fieldTypeService->getFieldType($fieldDefinition->fieldTypeIdentifier); |
46
|
|
|
|
47
|
|
|
if ( |
48
|
|
|
$fieldDefinition->isThumbnail |
49
|
|
|
&& $this->contentFieldStrategy->hasStrategy($field->fieldTypeIdentifier) |
|
|
|
|
50
|
|
|
&& !$fieldType->isEmptyValue($field->value) |
51
|
|
|
) { |
52
|
|
|
return $this->contentFieldStrategy->getThumbnail($field); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function getFieldByIdentifier(string $identifier, array $fields): ?Field |
60
|
|
|
{ |
61
|
|
|
/** @var \eZ\Publish\API\Repository\Values\Content\Field $field */ |
62
|
|
|
foreach ($fields as $field) { |
63
|
|
|
if ($field->fieldDefIdentifier === $identifier) { |
64
|
|
|
return $field; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return null; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
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 implementation 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 interface: