-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69c006e
commit 8b99ce3
Showing
1 changed file
with
139 additions
and
0 deletions.
There are no files selected for viewing
139 changes: 139 additions & 0 deletions
139
...dle/MediaBundle/Infrastructure/Sulu/Content/PropertyResolver/ImageMapPropertyResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\MediaBundle\Infrastructure\Sulu\Content\PropertyResolver; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Sulu\Bundle\AdminBundle\Exception\MetadataNotFoundException; | ||
use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FieldMetadata; | ||
use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FormMetadata; | ||
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView; | ||
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ResolvableResource; | ||
use Sulu\Bundle\ContentBundle\Content\Application\MetadataResolver\MetadataResolver; | ||
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverInterface; | ||
use Sulu\Bundle\MediaBundle\Infrastructure\Sulu\Content\ResourceLoader\MediaResourceLoader; | ||
|
||
/** | ||
* @internal if you need to override this service, create a new service with based on ResourceLoaderInterface instead of extending this class | ||
* | ||
* @final | ||
*/ | ||
class ImageMapPropertyResolver implements PropertyResolverInterface | ||
{ | ||
private MetadataResolver $metadataResolver; | ||
|
||
public function __construct( | ||
private LoggerInterface $logger, | ||
private bool $debug = false, | ||
) { | ||
} | ||
|
||
/** | ||
* @internal | ||
* | ||
* Prevent circular dependency by injecting the MetadataResolver after instantiation | ||
*/ | ||
public function setMetadataResolver(MetadataResolver $metadataResolver): void | ||
{ | ||
$this->metadataResolver = $metadataResolver; | ||
} | ||
|
||
public function resolve(mixed $data, string $locale, array $params = []): ContentView | ||
{ | ||
$hotspots = (\is_array($data) && isset($data['hotspots']) && \is_array($data['hotspots'])) && \array_is_list($data['hotspots']) | ||
? $data['hotspots'] | ||
: []; | ||
|
||
$hotspots = $this->resolveHotspots($hotspots, $locale, $params); | ||
|
||
if (!\is_array($data) | ||
|| !isset($data['imageId']) | ||
|| !\is_numeric($data['imageId']) | ||
) { | ||
return ContentView::create(null, ['imageId' => null, 'hotspots' => $hotspots, ...$params]); | ||
} | ||
|
||
/** @var string $resourceLoaderKey */ | ||
$resourceLoaderKey = $params['resourceLoader'] ?? MediaResourceLoader::getKey(); | ||
$imageId = (int) $data['imageId']; | ||
|
||
return ContentView::create( | ||
[ | ||
'image' => new ResolvableResource($imageId, $resourceLoaderKey), | ||
'hotspots' => $hotspots, | ||
], | ||
[ | ||
'imageId' => $imageId, | ||
...$params, | ||
], | ||
); | ||
} | ||
|
||
/** | ||
* @param array<array<mixed>> $hotspots | ||
* @param array<string, mixed> $params | ||
* | ||
* @return array<array<mixed>> | ||
*/ | ||
private function resolveHotspots(array $hotspots, string $locale, array $params): array | ||
{ | ||
$metadata = $params['metadata'] ?? null; | ||
\assert($metadata instanceof FieldMetadata, 'Metadata must be set to resolve hotspots.'); | ||
$metadataTypes = $metadata->getTypes(); | ||
$contentViews = []; | ||
foreach ($hotspots as $block) { | ||
if (!\is_array($block) || !isset($block['type']) || !\is_string($block['type'])) { | ||
continue; | ||
} | ||
if (!isset($block['hotspot']) || !\is_array($block['hotspot'])) { | ||
continue; | ||
} | ||
|
||
$type = $block['type']; | ||
$hotspot = $block['hotspot']; | ||
|
||
|
||
$formMetadata = $metadataTypes[$type] ?? null; | ||
|
||
if (!$formMetadata instanceof FormMetadata) { | ||
$errorMessage = \sprintf( | ||
'Metadata type "%s" in "%s" not found, founded types are: %s', | ||
$type, | ||
$metadata->getName(), | ||
\implode(', ', \array_keys($metadataTypes)), | ||
); | ||
|
||
$this->logger->error($errorMessage); | ||
|
||
if ($this->debug) { | ||
throw new \UnexpectedValueException($errorMessage); | ||
} | ||
|
||
continue; | ||
} | ||
|
||
$contentViews[] = [ | ||
'type' => $type, | ||
'hotspot' => $hotspot, | ||
...$this->metadataResolver->resolveItems($formMetadata->getItems(), $block, $locale), | ||
]; | ||
} | ||
|
||
return $contentViews; | ||
} | ||
|
||
public static function getType(): string | ||
{ | ||
return 'single_media_selection'; | ||
} | ||
} |