-
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.
Add single collection and multi collection property resolver
- Loading branch information
1 parent
3ced396
commit e2f600a
Showing
8 changed files
with
453 additions
and
7 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
50 changes: 50 additions & 0 deletions
50
...ndle/Infrastructure/Sulu/Content/PropertyResolver/CollectionSelectionPropertyResolver.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,50 @@ | ||
<?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 Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView; | ||
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverInterface; | ||
use Sulu\Bundle\MediaBundle\Infrastructure\Sulu\Content\ResourceLoader\CollectionResourceLoader; | ||
|
||
/** | ||
* @internal if you need to override this service, create a new service with based on PropertyResolverInterface instead of extending this class | ||
* | ||
* @final | ||
*/ | ||
class CollectionSelectionPropertyResolver implements PropertyResolverInterface | ||
{ | ||
public function resolve(mixed $data, string $locale, array $params = []): ContentView | ||
{ | ||
if (!\is_array($data) | ||
|| 0 === \count($data) | ||
|| !\array_is_list($data) | ||
) { | ||
return ContentView::create([], ['ids' => [], ...$params]); | ||
} | ||
|
||
/** @var string $resourceLoaderKey */ | ||
$resourceLoaderKey = $params['resourceLoader'] ?? CollectionResourceLoader::getKey(); | ||
|
||
return ContentView::createResolvables( | ||
$data, | ||
$resourceLoaderKey, | ||
['ids' => $data, ...$params], | ||
); | ||
} | ||
|
||
public static function getType(): string | ||
{ | ||
return 'collection_selection'; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...nfrastructure/Sulu/Content/PropertyResolver/SingleCollectionSelectionPropertyResolver.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,50 @@ | ||
<?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 Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView; | ||
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverInterface; | ||
use Sulu\Bundle\MediaBundle\Infrastructure\Sulu\Content\ResourceLoader\CollectionResourceLoader; | ||
|
||
/** | ||
* @internal if you need to override this service, create a new service with based on PropertyResolverInterface instead of extending this class | ||
* | ||
* @final | ||
*/ | ||
class SingleCollectionSelectionPropertyResolver implements PropertyResolverInterface | ||
{ | ||
public function resolve(mixed $data, string $locale, array $params = []): ContentView | ||
{ | ||
if (!\is_numeric($data)) { | ||
return ContentView::create(null, ['id' => null, ...$params]); | ||
} | ||
|
||
/** @var string $resourceLoaderKey */ | ||
$resourceLoaderKey = $params['resourceLoader'] ?? CollectionResourceLoader::getKey(); | ||
|
||
return ContentView::createResolvable( | ||
(int) $data, | ||
$resourceLoaderKey, | ||
[ | ||
'id' => $data, | ||
...$params, | ||
], | ||
); | ||
} | ||
|
||
public static function getType(): string | ||
{ | ||
return 'single_collection_selection'; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...undle/MediaBundle/Infrastructure/Sulu/Content/ResourceLoader/CollectionResourceLoader.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,53 @@ | ||
<?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\ResourceLoader; | ||
|
||
use Sulu\Bundle\ContentBundle\Content\Application\ResourceLoader\ResourceLoaderInterface; | ||
use Sulu\Bundle\MediaBundle\Collection\Manager\CollectionManagerInterface; | ||
use Sulu\Bundle\MediaBundle\Media\Exception\CollectionNotFoundException; | ||
|
||
/** | ||
* @internal if you need to override this service, create a new service with based on ResourceLoaderInterface instead of extending this class | ||
* | ||
* @final | ||
*/ | ||
class CollectionResourceLoader implements ResourceLoaderInterface | ||
{ | ||
public const RESOURCE_LOADER_KEY = 'collection'; | ||
|
||
public function __construct( | ||
private CollectionManagerInterface $collectionManager, | ||
) { | ||
} | ||
|
||
public function load(array $ids, ?string $locale, array $params = []): array | ||
{ | ||
$mappedResult = []; | ||
foreach ($ids as $id) { | ||
try { | ||
$collection = $this->collectionManager->getById($id, $locale); // TODO load all over one query | ||
$mappedResult[$collection->getId()] = $collection; | ||
} catch (CollectionNotFoundException $e) { | ||
// @ignoreException: do not crash page if selected collection is deleted | ||
} | ||
} | ||
|
||
return $mappedResult; | ||
} | ||
|
||
public static function getKey(): string | ||
{ | ||
return self::RESOURCE_LOADER_KEY; | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
.../Infrastructure/Sulu/Content/PropertyResolver/CollectionSelectionPropertyResolverTest.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,114 @@ | ||
<?php | ||
|
||
/* | ||
* 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\Tests\Unit\Infrastructure\Sulu\Content\PropertyResolver; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ResolvableResource; | ||
use Sulu\Bundle\MediaBundle\Infrastructure\Sulu\Content\PropertyResolver\CollectionSelectionPropertyResolver; | ||
|
||
#[CoversClass(CollectionSelectionPropertyResolver::class)] | ||
class CollectionSelectionPropertyResolverTest extends TestCase | ||
{ | ||
private CollectionSelectionPropertyResolver $resolver; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->resolver = new CollectionSelectionPropertyResolver(); | ||
} | ||
|
||
public function testResolveEmpty(): void | ||
{ | ||
$contentView = $this->resolver->resolve([], 'en'); | ||
|
||
$this->assertSame([], $contentView->getContent()); | ||
$this->assertSame(['ids' => []], $contentView->getView()); | ||
} | ||
|
||
public function testResolveParams(): void | ||
{ | ||
$contentView = $this->resolver->resolve([], 'en', ['custom' => 'params']); | ||
|
||
$this->assertSame([], $contentView->getContent()); | ||
$this->assertSame([ | ||
'ids' => [], | ||
'custom' => 'params', | ||
], $contentView->getView()); | ||
} | ||
|
||
#[DataProvider('provideUnresolvableData')] | ||
public function testResolveUnresolvableData(mixed $data): void | ||
{ | ||
$contentView = $this->resolver->resolve($data, 'en'); | ||
|
||
$this->assertSame([], $contentView->getContent()); | ||
$this->assertSame(['ids' => []], $contentView->getView()); | ||
} | ||
|
||
/** | ||
* @return iterable<array{ | ||
* 0: mixed, | ||
* }> | ||
*/ | ||
public static function provideUnresolvableData(): iterable | ||
{ | ||
yield 'null' => [null]; | ||
yield 'smart_content' => [['source' => '123']]; | ||
yield 'single_value' => [1]; | ||
yield 'object' => [(object) [1, 2]]; | ||
} | ||
|
||
/** | ||
* @param array<string|int> $data | ||
*/ | ||
#[DataProvider('provideResolvableData')] | ||
public function testResolveResolvableData(array $data): void | ||
{ | ||
$contentView = $this->resolver->resolve($data, 'en'); | ||
|
||
$content = $contentView->getContent(); | ||
$this->assertIsArray($content); | ||
foreach ($data as $key => $value) { | ||
$resolvable = $content[$key] ?? null; | ||
$this->assertInstanceOf(ResolvableResource::class, $resolvable); | ||
$this->assertSame($value, $resolvable->getId()); | ||
$this->assertSame('collection', $resolvable->getResourceLoaderKey()); | ||
} | ||
|
||
$this->assertSame(['ids' => $data], $contentView->getView()); | ||
} | ||
|
||
/** | ||
* @return iterable<array{ | ||
* 0: array<string|int>, | ||
* }> | ||
*/ | ||
public static function provideResolvableData(): iterable | ||
{ | ||
yield 'empty' => [[]]; | ||
yield 'int_list' => [[1, 2]]; | ||
yield 'string_list' => [['1', '2']]; | ||
} | ||
|
||
public function testCustomResourceLoader(): void | ||
{ | ||
$contentView = $this->resolver->resolve([1], 'en', ['resourceLoader' => 'custom_collection']); | ||
|
||
$content = $contentView->getContent(); | ||
$this->assertIsArray($content); | ||
$resolvable = $content[0] ?? null; | ||
$this->assertInstanceOf(ResolvableResource::class, $resolvable); | ||
$this->assertSame(1, $resolvable->getId()); | ||
$this->assertSame('custom_collection', $resolvable->getResourceLoaderKey()); | ||
} | ||
} |
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
Oops, something went wrong.