Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename ContentResolver to ContentProjectionNormalizer #64

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Content/Application/ContentCopier/ContentCopier.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Sulu\Bundle\ContentBundle\Content\Application\ContentLoader\ContentLoaderInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentPersister\ContentPersisterInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ViewResolver\ApiViewResolverInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentProjectionNormalizer\ContentProjectionNormalizerInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\ContentProjectionFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentProjectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
Expand All @@ -39,20 +39,20 @@ class ContentCopier implements ContentCopierInterface
private $contentPersister;

/**
* @var ApiViewResolverInterface
* @var ContentProjectionNormalizerInterface
*/
private $contentResolver;
private $contentProjectionNormalizer;

public function __construct(
ContentLoaderInterface $contentLoader,
ContentProjectionFactoryInterface $viewFactory,
ContentPersisterInterface $contentPersister,
ApiViewResolverInterface $contentResolver
ContentProjectionNormalizerInterface $contentProjectionNormalizer
) {
$this->contentLoader = $contentLoader;
$this->viewFactory = $viewFactory;
$this->contentPersister = $contentPersister;
$this->contentResolver = $contentResolver;
$this->contentProjectionNormalizer = $contentProjectionNormalizer;
}

public function copy(
Expand All @@ -78,11 +78,11 @@ public function copyFromDimensionContentCollection(

public function copyFromContentProjection(
ContentProjectionInterface $sourceContentProjection,
ContentRichEntityInterface $targetContentRichENtity,
ContentRichEntityInterface $targetContentRichEntity,
array $targetDimensionAttributes
): ContentProjectionInterface {
$data = $this->contentResolver->resolve($sourceContentProjection);
$data = $this->contentProjectionNormalizer->normalize($sourceContentProjection);

return $this->contentPersister->persist($targetContentRichENtity, $data, $targetDimensionAttributes);
return $this->contentPersister->persist($targetContentRichEntity, $data, $targetDimensionAttributes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function copyFromDimensionContentCollection(
*/
public function copyFromContentProjection(
ContentProjectionInterface $sourceContentProjection,
ContentRichEntityInterface $targetContentRichENtity,
ContentRichEntityInterface $targetContentRichEntity,
array $targetDimensionAttributes
): ContentProjectionInterface;
}
14 changes: 7 additions & 7 deletions Content/Application/ContentFacade/ContentFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use Sulu\Bundle\ContentBundle\Content\Application\ContentCopier\ContentCopierInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentLoader\ContentLoaderInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentPersister\ContentPersisterInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentProjectionNormalizer\ContentProjectionNormalizerInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentWorkflow\ContentWorkflowInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ViewResolver\ApiViewResolverInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentProjectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;

Expand All @@ -34,9 +34,9 @@ class ContentFacade implements ContentFacadeInterface
private $contentPersister;

/**
* @var ApiViewResolverInterface
* @var ContentProjectionNormalizerInterface
*/
private $contentResolver;
private $contentProjectionNormalizer;

/**
* @var ContentCopierInterface
Expand All @@ -51,13 +51,13 @@ class ContentFacade implements ContentFacadeInterface
public function __construct(
ContentLoaderInterface $contentLoader,
ContentPersisterInterface $contentPersister,
ApiViewResolverInterface $contentResolver,
ContentProjectionNormalizerInterface $contentProjectionNormalizer,
ContentCopierInterface $contentCopier,
ContentWorkflowInterface $contentWorkflow
) {
$this->contentLoader = $contentLoader;
$this->contentPersister = $contentPersister;
$this->contentResolver = $contentResolver;
$this->contentProjectionNormalizer = $contentProjectionNormalizer;
$this->contentCopier = $contentCopier;
$this->contentWorkflow = $contentWorkflow;
}
Expand All @@ -72,9 +72,9 @@ public function persist(ContentRichEntityInterface $contentRichEntity, array $da
return $this->contentPersister->persist($contentRichEntity, $data, $dimensionAttributes);
}

public function resolve(ContentProjectionInterface $contentProjection): array
public function normalize(ContentProjectionInterface $contentProjection): array
{
return $this->contentResolver->resolve($contentProjection);
return $this->contentProjectionNormalizer->normalize($contentProjection);
}

public function copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function persist(ContentRichEntityInterface $contentRichEntity, array $da
/**
* @return mixed[]
*/
public function resolve(ContentProjectionInterface $contentProjection): array;
public function normalize(ContentProjectionInterface $contentProjection): array;

/**
* @param mixed[] $sourceDimensionAttributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,65 @@
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ContentBundle\Content\Application\ViewResolver;
namespace Sulu\Bundle\ContentBundle\Content\Application\ContentProjectionNormalizer;

use Sulu\Bundle\ContentBundle\Content\Application\ViewResolver\Resolver\ResolverInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentProjectionNormalizer\Enhancer\NormalizeEnhancerInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentProjectionInterface;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Serializer;

class ApiViewResolver implements ViewResolverInterface, ApiViewResolverInterface
class ContentProjectionNormalizer implements ContentProjectionNormalizerInterface
{
/**
* @var iterable<ResolverInterface>
* @var iterable<NormalizeEnhancerInterface>
*/
private $resolvers;
private $enhancers;

/**
* @var NormalizerInterface
*/
private $serializer;

/**
* @param iterable<ResolverInterface> $resolvers
* @param iterable<NormalizeEnhancerInterface> $enhancers
*/
public function __construct(
iterable $resolvers,
iterable $enhancers,
?NormalizerInterface $serializer = null
) {
$this->resolvers = $resolvers;
$this->enhancers = $enhancers;
$this->serializer = $serializer ?: $this->createSerializer();
}

public function resolve(ContentProjectionInterface $contentProjection): array
public function normalize(ContentProjectionInterface $contentProjection): array
{
$ignoreAttributes = ['id'];

foreach ($this->resolvers as $resolver) {
foreach ($this->enhancers as $enhancer) {
$ignoreAttributes = array_merge(
$ignoreAttributes,
$resolver->getIgnoredAttributes($contentProjection)
$enhancer->getIgnoredAttributes($contentProjection)
);
}

/** @var mixed[] $viewData */
$viewData = $this->serializer->normalize($contentProjection, null, [
/** @var mixed[] $normalizedData */
$normalizedData = $this->serializer->normalize($contentProjection, null, [
'ignored_attributes' => $ignoreAttributes,
]);

// The view should not be represented by its own id but the id of the content entity
$viewData['id'] = $viewData['contentId'];
unset($viewData['contentId']);
$normalizedData['id'] = $normalizedData['contentId'];
unset($normalizedData['contentId']);

foreach ($this->resolvers as $resolver) {
$viewData = $resolver->resolve($contentProjection, $viewData);
foreach ($this->enhancers as $enhancer) {
$normalizedData = $enhancer->enhance($contentProjection, $normalizedData);
}

ksort($viewData);
ksort($normalizedData);

return $viewData;
return $normalizedData;
}

private function createSerializer(): NormalizerInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ContentBundle\Content\Application\ViewResolver;
namespace Sulu\Bundle\ContentBundle\Content\Application\ContentProjectionNormalizer;

use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentProjectionInterface;

interface ViewResolverInterface
interface ContentProjectionNormalizerInterface
{
/**
* @return mixed[]
*/
public function resolve(ContentProjectionInterface $contentProjection): array;
public function normalize(ContentProjectionInterface $contentProjection): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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\ContentProjectionNormalizer\Enhancer;

use Sulu\Bundle\ContentBundle\Content\Domain\Model\ExcerptInterface;

class ExcerptNormalizeEnhancer implements NormalizeEnhancerInterface
{
public function enhance(object $object, array $normalizeData): array
{
if (!$object instanceof ExcerptInterface) {
return $normalizeData;
}

$normalizeData['excerptTags'] = $normalizeData['excerptTagNames'];
unset($normalizeData['excerptTagNames']);
$normalizeData['excerptCategories'] = $normalizeData['excerptCategoryIds'];
unset($normalizeData['excerptCategoryIds']);

return $normalizeData;
}

public function getIgnoredAttributes(object $object): array
{
if (!$object instanceof ExcerptInterface) {
return [];
}

return [
'excerptTags',
'excerptCategories',
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\ContentProjectionNormalizer\Enhancer;

interface NormalizeEnhancerInterface
{
/**
* @param mixed[] $normalizeData
*
* @return mixed[]
*/
public function enhance(object $object, array $normalizeData): array;

/**
* @return string[]
*/
public function getIgnoredAttributes(object $object): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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\ContentProjectionNormalizer\Enhancer;

use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface;

class TemplateNormalizeEnhancer implements NormalizeEnhancerInterface
{
public function enhance(object $object, array $normalizeData): array
{
if (!$object instanceof TemplateInterface) {
return $normalizeData;
}

$normalizeData = array_merge($normalizeData, $normalizeData['templateData']);
unset($normalizeData['templateData']);

$normalizeData['template'] = $normalizeData['templateKey'];
unset($normalizeData['templateKey']);

return $normalizeData;
}

public function getIgnoredAttributes(object $object): array
{
if (!$object instanceof TemplateInterface) {
return [];
}

return [
'templateType',
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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\ContentProjectionNormalizer\Enhancer;

use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface;

class WorkflowNormalizeEnhancer implements NormalizeEnhancerInterface
{
public function enhance(object $object, array $normalizeData): array
{
if (!$object instanceof WorkflowInterface) {
return $normalizeData;
}

$normalizeData['publishedState'] = WorkflowInterface::WORKFLOW_PLACE_PUBLISHED === $normalizeData['workflowPlace'];
$normalizeData['published'] = $normalizeData['workflowPublished'];
unset($normalizeData['workflowPublished']);

return $normalizeData;
}

public function getIgnoredAttributes(object $object): array
{
if (!$object instanceof WorkflowInterface) {
return [];
}

return [];
}
}
Loading