-
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.
- Loading branch information
Showing
7 changed files
with
640 additions
and
0 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,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\HeadlessBundle\Content\ContentTypeResolver; | ||
|
||
use Sulu\Bundle\HeadlessBundle\Content\ContentView; | ||
use Sulu\Bundle\HeadlessBundle\Content\Serializer\TeaserSerializerInterface; | ||
use Sulu\Bundle\PageBundle\Teaser\Teaser; | ||
use Sulu\Bundle\PageBundle\Teaser\TeaserManagerInterface; | ||
use Sulu\Component\Content\Compat\PropertyInterface; | ||
|
||
class TeaserSelectionResolver implements ContentTypeResolverInterface | ||
{ | ||
public static function getContentType(): string | ||
{ | ||
return 'teaser_selection'; | ||
} | ||
|
||
/** | ||
* @var TeaserManagerInterface | ||
*/ | ||
private $teaserManager; | ||
|
||
/** | ||
* @var TeaserSerializerInterface | ||
*/ | ||
private $teaserSerializer; | ||
|
||
public function __construct(TeaserManagerInterface $teaserManager, TeaserSerializerInterface $teaserSerializer) | ||
{ | ||
$this->teaserManager = $teaserManager; | ||
$this->teaserSerializer = $teaserSerializer; | ||
} | ||
|
||
public function resolve($data, PropertyInterface $property, string $locale, array $attributes = []): ContentView | ||
{ | ||
$value = array_merge( | ||
[ | ||
'presentAs' => null, | ||
'items' => [], | ||
], | ||
\is_array($data) ? $data : [] | ||
); | ||
$items = $value['items'] ?? []; | ||
|
||
if (!\is_array($items) || 0 === \count($items)) { | ||
return new ContentView([], $value); | ||
} | ||
|
||
$teasers = $this->teaserManager->find($items, $locale); | ||
$teasers = array_map( | ||
function (Teaser $teaser) use ($locale) { | ||
return $this->teaserSerializer->serialize($teaser, $locale); | ||
}, | ||
$teasers | ||
); | ||
|
||
return new ContentView($teasers, $value); | ||
} | ||
} |
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,103 @@ | ||
<?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\Serializer; | ||
|
||
use JMS\Serializer\SerializationContext; | ||
use Sulu\Bundle\MediaBundle\Media\Manager\MediaManagerInterface; | ||
use Sulu\Bundle\PageBundle\Teaser\Teaser; | ||
use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStoreNotExistsException; | ||
use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStorePoolInterface; | ||
use Sulu\Component\Serializer\ArraySerializerInterface; | ||
|
||
class TeaserSerializer implements TeaserSerializerInterface | ||
{ | ||
/** | ||
* @var ArraySerializerInterface | ||
*/ | ||
private $arraySerializer; | ||
|
||
/** | ||
* @var MediaSerializerInterface | ||
*/ | ||
private $mediaSerializer; | ||
|
||
/** | ||
* @var MediaManagerInterface | ||
*/ | ||
private $mediaManager; | ||
|
||
/** | ||
* @var ReferenceStorePoolInterface | ||
*/ | ||
private $referenceStorePool; | ||
|
||
public function __construct( | ||
ArraySerializerInterface $arraySerializer, | ||
MediaSerializerInterface $mediaSerializer, | ||
MediaManagerInterface $mediaManager, | ||
ReferenceStorePoolInterface $referenceStorePool | ||
) { | ||
$this->arraySerializer = $arraySerializer; | ||
$this->mediaSerializer = $mediaSerializer; | ||
$this->mediaManager = $mediaManager; | ||
$this->referenceStorePool = $referenceStorePool; | ||
} | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
public function serialize(Teaser $teaser, string $locale, ?SerializationContext $context = null): array | ||
{ | ||
$teaserData = $this->arraySerializer->serialize($teaser, $context); | ||
unset($teaserData['mediaId']); | ||
|
||
$mediaId = $teaser->getMediaId(); | ||
$mediaData = null; | ||
if ($mediaId) { | ||
$media = $this->mediaManager->getEntityById($mediaId); | ||
$mediaData = $this->mediaSerializer->serialize($media, $locale); | ||
} | ||
|
||
$teaserData['media'] = $mediaData; | ||
|
||
$this->addToReferenceStore($teaser->getId(), $teaser->getType()); | ||
|
||
return $teaserData; | ||
} | ||
|
||
/** | ||
* @param int|string $id | ||
*/ | ||
private function addToReferenceStore($id, string $alias): void | ||
{ | ||
if ('pages' === $alias) { | ||
// unfortunately the reference store for pages was not adjusted and still uses content as alias | ||
$alias = 'content'; | ||
} | ||
|
||
if ('articles' === $alias) { | ||
$alias = 'article'; | ||
} | ||
|
||
try { | ||
$referenceStore = $this->referenceStorePool->getStore($alias); | ||
} catch (ReferenceStoreNotExistsException $e) { | ||
// @ignoreException do nothing when reference store was not found | ||
|
||
return; | ||
} | ||
|
||
$referenceStore->add($id); | ||
} | ||
} |
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,25 @@ | ||
<?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\Serializer; | ||
|
||
use JMS\Serializer\SerializationContext; | ||
use Sulu\Bundle\PageBundle\Teaser\Teaser; | ||
|
||
interface TeaserSerializerInterface | ||
{ | ||
/** | ||
* @return mixed[] | ||
*/ | ||
public function serialize(Teaser $teaser, string $locale, ?SerializationContext $context = null): array; | ||
} |
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
Oops, something went wrong.