Skip to content

Commit

Permalink
Add ImageMap Property Resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Oct 17, 2024
1 parent 69c006e commit 8b99ce3
Showing 1 changed file with 139 additions and 0 deletions.
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';
}
}

0 comments on commit 8b99ce3

Please sign in to comment.