-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Prokyonn/enhancement/teaser-property-provider
Add TeaserSelectionpropertyResolver
- Loading branch information
Showing
15 changed files
with
315 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
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
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
70 changes: 70 additions & 0 deletions
70
Content/Application/PropertyResolver/Resolver/TeaserSelectionPropertyResolver.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,70 @@ | ||
<?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\ContentBundle\Content\Application\PropertyResolver\Resolver; | ||
|
||
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView; | ||
use Sulu\Bundle\ContentBundle\Content\Application\ResourceLoader\Loader\TeaserResourceLoader; | ||
use Sulu\Bundle\PageBundle\Teaser\Teaser; | ||
|
||
class TeaserSelectionPropertyResolver implements PropertyResolverInterface | ||
{ | ||
/** | ||
* @param mixed[] $data | ||
* @param mixed[] $params | ||
*/ | ||
public function resolve(mixed $data, string $locale, array $params = []): ContentView | ||
{ | ||
$returnedParams = ['presentsAs' => $data['presentsAs'] ?? null, ...$params]; | ||
unset($returnedParams['metadata']); | ||
|
||
if ( | ||
!\is_array($data) | ||
|| !\array_key_exists('items', $data) | ||
|| !\is_array($data['items']) | ||
) { | ||
return ContentView::create($data, $returnedParams); | ||
} | ||
|
||
/** @var string $resourceLoaderKey */ | ||
$resourceLoaderKey = $params['resourceLoader'] ?? TeaserResourceLoader::getKey(); | ||
|
||
$contentViews = []; | ||
foreach ($data['items'] as $item) { | ||
if (!\is_array($item) || !\array_key_exists('id', $item) || !\array_key_exists('type', $item)) { | ||
continue; | ||
} | ||
$type = $item['type']; | ||
$id = $item['id']; | ||
|
||
$contentViews[] = ContentView::createResolvable( | ||
$type . '::' . $id, | ||
$resourceLoaderKey, | ||
[ | ||
'id' => $id, | ||
'type' => $type, | ||
], | ||
static function(Teaser $resource) use ($item) { | ||
return $resource->merge($item); | ||
} | ||
); | ||
} | ||
|
||
return ContentView::create($contentViews, $returnedParams); | ||
} | ||
|
||
public static function getType(): string | ||
{ | ||
return 'teaser_selection'; | ||
} | ||
} |
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
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
78 changes: 78 additions & 0 deletions
78
Content/Application/ResourceLoader/Loader/TeaserResourceLoader.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,78 @@ | ||
<?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\ContentBundle\Content\Application\ResourceLoader\Loader; | ||
|
||
use Sulu\Bundle\PageBundle\Teaser\TeaserManagerInterface; | ||
|
||
class TeaserResourceLoader implements ResourceLoaderInterface | ||
{ | ||
public const RESOURCE_LOADER_KEY = 'teaser'; | ||
|
||
public function __construct( | ||
private TeaserManagerInterface $teaserManager | ||
) { | ||
} | ||
|
||
/** | ||
* @param string[] $ids | ||
* @param mixed[] $params | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function load(array $ids, ?string $locale, array $params = []): array | ||
{ | ||
if ([] === $ids) { | ||
return []; | ||
} | ||
|
||
if (null === $locale) { | ||
throw new \RuntimeException('Locale is required to resolve Teasers'); | ||
} | ||
|
||
$items = $this->getItemsByProvider($ids); | ||
$teasers = []; | ||
foreach ($items as $provider => $itemsPerProvider) { | ||
$loadedTeasers = $this->teaserManager->find($itemsPerProvider, $locale); | ||
foreach ($loadedTeasers as $teaser) { | ||
$teasers[$provider . '::' . $teaser->getId()] = $teaser; | ||
} | ||
} | ||
|
||
return $teasers; | ||
} | ||
|
||
/** | ||
* @param string[] $ids | ||
* | ||
* @return array<string, non-empty-array<int, array{id: string, type: string}>> | ||
*/ | ||
private function getItemsByProvider(array $ids): array | ||
{ | ||
$idsByProvider = []; | ||
foreach ($ids as $id) { | ||
[$provider, $id] = \explode('::', $id, 2); | ||
$idsByProvider[$provider][] = [ | ||
'id' => $id, | ||
'type' => $provider, | ||
]; | ||
} | ||
|
||
return $idsByProvider; | ||
} | ||
|
||
public static function getKey(): string | ||
{ | ||
return self::RESOURCE_LOADER_KEY; | ||
} | ||
} |
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
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
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
143 changes: 143 additions & 0 deletions
143
...nit/Content/Application/PropertyResolver/Resolver/TeaserSelectionPropertyResolverTest.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,143 @@ | ||
<?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\ContentBundle\Tests\Unit\Content\Application\PropertyResolver\Resolver; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView; | ||
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ResolvableResource; | ||
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\Resolver\TeaserSelectionPropertyResolver; | ||
use Sulu\Bundle\PageBundle\Teaser\Teaser; | ||
|
||
class TeaserSelectionPropertyResolverTest extends TestCase | ||
{ | ||
private TeaserSelectionPropertyResolver $resolver; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->resolver = new TeaserSelectionPropertyResolver(); | ||
} | ||
|
||
public function testResolveEmpty(): void | ||
{ | ||
$contentView = $this->resolver->resolve([], 'en'); | ||
|
||
$this->assertSame([], $contentView->getContent()); | ||
$this->assertSame(['presentsAs' => null], $contentView->getView()); | ||
} | ||
|
||
public function testResolveParams(): void | ||
{ | ||
$contentView = $this->resolver->resolve([], 'en', ['custom' => 'params']); | ||
|
||
$this->assertSame([], $contentView->getContent()); | ||
$this->assertSame(['presentsAs' => null, 'custom' => 'params'], $contentView->getView()); | ||
} | ||
|
||
public function testResolveData(): void | ||
{ | ||
$data = [ | ||
'presentsAs' => 'two-columns', | ||
'items' => [ | ||
['id' => '123', 'type' => 'article'], | ||
['id' => '456', 'type' => 'page'], | ||
], | ||
]; | ||
|
||
$contentView = $this->resolver->resolve($data, 'en'); | ||
|
||
$content = $contentView->getContent(); | ||
$this->assertIsArray($content); | ||
$this->assertCount(2, $content); | ||
|
||
$innerContentView1 = $content[0]; | ||
$this->assertInstanceOf(ContentView::class, $innerContentView1); | ||
$resolvableResource = $innerContentView1->getContent(); | ||
$this->assertInstanceOf(ResolvableResource::class, $resolvableResource); | ||
$this->assertSame('article::123', $resolvableResource->getId()); | ||
$this->assertSame('teaser', $resolvableResource->getResourceLoaderKey()); | ||
$this->assertSame(['id' => '123', 'type' => 'article'], $innerContentView1->getView()); | ||
|
||
$innerContentView2 = $content[1]; | ||
$this->assertInstanceOf(ContentView::class, $innerContentView2); | ||
$resolvableResource = $innerContentView2->getContent(); | ||
$this->assertInstanceOf(ResolvableResource::class, $resolvableResource); | ||
$this->assertSame('page::456', $resolvableResource->getId()); | ||
$this->assertSame('teaser', $resolvableResource->getResourceLoaderKey()); | ||
$this->assertSame(['id' => '456', 'type' => 'page'], $innerContentView2->getView()); | ||
|
||
$this->assertSame(['presentsAs' => 'two-columns'], $contentView->getView()); | ||
} | ||
|
||
public function testResolveCustomResourceLoader(): void | ||
{ | ||
$data = [ | ||
'items' => [ | ||
['id' => '123', 'type' => 'article'], | ||
], | ||
]; | ||
|
||
$contentView = $this->resolver->resolve($data, 'en', ['resourceLoader' => 'custom_teaser']); | ||
|
||
$content = $contentView->getContent(); | ||
$this->assertIsArray($content); | ||
$this->assertCount(1, $content); | ||
|
||
$innerContent = $content[0]; | ||
$this->assertInstanceOf(ContentView::class, $innerContent); | ||
$resolvable = $innerContent->getContent(); | ||
$this->assertInstanceOf(ResolvableResource::class, $resolvable); | ||
$this->assertSame('article::123', $resolvable->getId()); | ||
$this->assertSame('custom_teaser', $resolvable->getResourceLoaderKey()); | ||
} | ||
|
||
public function testResolveWithResourceCallback(): void | ||
{ | ||
$data = [ | ||
'items' => [ | ||
[ | ||
'id' => '123', | ||
'type' => 'article', | ||
'title' => 'Article Title', | ||
'description' => 'Article Description', | ||
'mediaId' => 11, | ||
], | ||
], | ||
]; | ||
|
||
$contentView = $this->resolver->resolve($data, 'en'); | ||
|
||
$content = $contentView->getContent(); | ||
$this->assertIsArray($content); | ||
$this->assertCount(1, $content); | ||
|
||
$innerContent = $content[0]; | ||
$this->assertInstanceOf(ContentView::class, $innerContent); | ||
$resolvable = $innerContent->getContent(); | ||
$this->assertInstanceOf(ResolvableResource::class, $resolvable); | ||
$this->assertSame('article::123', $resolvable->getId()); | ||
$this->assertSame('teaser', $resolvable->getResourceLoaderKey()); | ||
|
||
$teaser = new Teaser('123', 'article', 'en', '', '', '', 'http://example.com', 1); | ||
$mergedTeaser = $resolvable->executeResourceCallback($teaser); | ||
$this->assertInstanceOf(Teaser::class, $mergedTeaser); | ||
$this->assertSame('Article Title', $mergedTeaser->getTitle()); | ||
$this->assertSame('Article Description', $mergedTeaser->getDescription()); | ||
$this->assertSame(11, $mergedTeaser->getMediaId()); | ||
} | ||
|
||
public function testGetType(): void | ||
{ | ||
$this->assertSame('teaser_selection', TeaserSelectionPropertyResolver::getType()); | ||
} | ||
} |
Oops, something went wrong.