-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Link resolver * increase min requirement to sulu ^2.4
- Loading branch information
Showing
5 changed files
with
234 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?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\HeadlessBundle\Content\ContentTypeResolver; | ||
|
||
use Sulu\Bundle\HeadlessBundle\Content\ContentView; | ||
use Sulu\Bundle\MarkupBundle\Markup\Link\LinkProviderPoolInterface; | ||
use Sulu\Component\Content\Compat\PropertyInterface; | ||
use Sulu\Component\Content\Types\Link; | ||
|
||
class LinkResolver implements ContentTypeResolverInterface | ||
{ | ||
/** | ||
* @var LinkProviderPoolInterface | ||
*/ | ||
private $linkProviderPool; | ||
|
||
public static function getContentType(): string | ||
{ | ||
return 'link'; | ||
} | ||
|
||
public function __construct( | ||
LinkProviderPoolInterface $linkProviderPool | ||
) { | ||
$this->linkProviderPool = $linkProviderPool; | ||
} | ||
|
||
public function resolve($data, PropertyInterface $property, string $locale, array $attributes = []): ContentView | ||
{ | ||
$content = $this->getContentData($property); | ||
$view = $this->getViewData($property); | ||
|
||
return new ContentView($content, $view); | ||
} | ||
|
||
/** | ||
* @return array{ | ||
* provider?: string, | ||
* locale?: string, | ||
* target?: string, | ||
* title?: string | ||
* } | ||
*/ | ||
private function getViewData(PropertyInterface $property): array | ||
{ | ||
$value = $property->getValue(); | ||
|
||
if (!$value) { | ||
return []; | ||
} | ||
|
||
$result = [ | ||
'provider' => $value['provider'], | ||
'locale' => $value['locale'], | ||
]; | ||
|
||
if (isset($value['target'])) { | ||
$result['target'] = $value['target']; | ||
} | ||
|
||
if (isset($value['title'])) { | ||
$result['title'] = $value['title']; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
private function getContentData(PropertyInterface $property): ?string | ||
{ | ||
$value = $property->getValue(); | ||
|
||
if (!$value || !isset($value['provider'])) { | ||
return null; | ||
} | ||
|
||
if (Link::LINK_TYPE_EXTERNAL === $value['provider']) { | ||
return $value['href']; | ||
} | ||
|
||
$provider = $this->linkProviderPool->getProvider($value['provider']); | ||
|
||
$linkItems = $provider->preload([$value['href']], $value['locale']); | ||
|
||
if (0 === \count($linkItems)) { | ||
return null; | ||
} | ||
|
||
$url = reset($linkItems)->getUrl(); | ||
if (isset($value['anchor'])) { | ||
$url = sprintf('%s#%s', $url, $value['anchor']); | ||
} | ||
|
||
return $url; | ||
} | ||
} |
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
116 changes: 116 additions & 0 deletions
116
Tests/Unit/Content/ContentTypeResolver/LinkResolverTest.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,116 @@ | ||
<?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\HeadlessBundle\Tests\Unit\Content\ContentTypeResolver; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Sulu\Bundle\HeadlessBundle\Content\ContentTypeResolver\LinkResolver; | ||
use Sulu\Bundle\MarkupBundle\Markup\Link\LinkItem; | ||
use Sulu\Bundle\MarkupBundle\Markup\Link\LinkProviderInterface; | ||
use Sulu\Bundle\MarkupBundle\Markup\Link\LinkProviderPoolInterface; | ||
use Sulu\Component\Content\Compat\PropertyInterface; | ||
|
||
class LinkResolverTest extends TestCase | ||
{ | ||
public function testGetContentType(): void | ||
{ | ||
$linkProviderPool = $this->prophesize(LinkProviderPoolInterface::class); | ||
$linkResolver = new LinkResolver($linkProviderPool->reveal()); | ||
|
||
self::assertSame('link', $linkResolver::getContentType()); | ||
} | ||
|
||
public function testResolve(): void | ||
{ | ||
$providerPool = $this->prophesize(LinkProviderPoolInterface::class); | ||
$provider = $this->prophesize(LinkProviderInterface::class); | ||
$linkResolver = new LinkResolver($providerPool->reveal()); | ||
|
||
$property = $this->prophesize(PropertyInterface::class); | ||
$property->getValue() | ||
->shouldBeCalled() | ||
->willReturn([ | ||
'provider' => 'page', | ||
'target' => '_self', | ||
'anchor' => 'link', | ||
'href' => '76fcf58e-0624-4cf0-85a5-170de9f14252', | ||
'title' => 'Internal Link', | ||
'locale' => 'en', | ||
]); | ||
|
||
$providerPool->getProvider(Argument::type('string')) | ||
->shouldBeCalled() | ||
->willReturn($provider->reveal()); | ||
|
||
$linkItem = new LinkItem( | ||
'76fcf58e-0624-4cf0-85a5-170de9f14252', | ||
'Internal Link', | ||
'https://example.lo/link', | ||
true | ||
); | ||
|
||
$provider->preload(['76fcf58e-0624-4cf0-85a5-170de9f14252'], 'en') | ||
->shouldBeCalled() | ||
->willReturn([$linkItem]); | ||
|
||
$result = $linkResolver->resolve([], $property->reveal(), 'en'); | ||
|
||
$this->assertSame('https://example.lo/link#link', $result->getContent()); | ||
$this->assertSame([ | ||
'provider' => 'page', | ||
'locale' => 'en', | ||
'target' => '_self', | ||
'title' => 'Internal Link', | ||
], $result->getView()); | ||
} | ||
|
||
public function testResolveMinimal(): void | ||
{ | ||
$providerPool = $this->prophesize(LinkProviderPoolInterface::class); | ||
$provider = $this->prophesize(LinkProviderInterface::class); | ||
$linkResolver = new LinkResolver($providerPool->reveal()); | ||
|
||
$property = $this->prophesize(PropertyInterface::class); | ||
$property->getValue() | ||
->shouldBeCalled() | ||
->willReturn([ | ||
'provider' => 'page', | ||
'href' => '76fcf58e-0624-4cf0-85a5-170de9f14252', | ||
'locale' => 'en', | ||
]); | ||
|
||
$providerPool->getProvider(Argument::type('string')) | ||
->shouldBeCalled() | ||
->willReturn($provider->reveal()); | ||
|
||
$linkItem = new LinkItem( | ||
'76fcf58e-0624-4cf0-85a5-170de9f14252', | ||
'Internal Link', | ||
'https://example.lo/link', | ||
true | ||
); | ||
|
||
$provider->preload(['76fcf58e-0624-4cf0-85a5-170de9f14252'], 'en') | ||
->shouldBeCalled() | ||
->willReturn([$linkItem]); | ||
|
||
$result = $linkResolver->resolve([], $property->reveal(), 'en'); | ||
|
||
$this->assertSame('https://example.lo/link', $result->getContent()); | ||
$this->assertSame([ | ||
'provider' => 'page', | ||
'locale' => 'en', | ||
], $result->getView()); | ||
} | ||
} |
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