Skip to content

Commit

Permalink
Rename NormalizerHelper into NormalizeEnhancer
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Dec 17, 2019
1 parent c65a286 commit d820692
Show file tree
Hide file tree
Showing 17 changed files with 440 additions and 137 deletions.
4 changes: 2 additions & 2 deletions Content/Application/ContentCopier/ContentCopier.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ public function copyFromDimensionContentCollection(

public function copyFromContentProjection(
ContentProjectionInterface $sourceContentProjection,
ContentRichEntityInterface $targetContentRichENtity,
ContentRichEntityInterface $targetContentRichEntity,
array $targetDimensionAttributes
): ContentProjectionInterface {
$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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

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

use Sulu\Bundle\ContentBundle\Content\Application\ContentProjectionNormalizer\Helper\NormalizerHelperInterface;
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;
Expand All @@ -23,53 +23,53 @@
class ContentProjectionNormalizer implements ContentProjectionNormalizerInterface
{
/**
* @var iterable<NormalizerHelperInterface>
* @var iterable<NormalizeEnhancerInterface>
*/
private $helpers;
private $enhancers;

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

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

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

foreach ($this->helpers 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->helpers as $helper) {
$viewData = $helper->normalize($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,28 +11,32 @@
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ContentBundle\Content\Application\ContentProjectionNormalizer\Helper;
namespace Sulu\Bundle\ContentBundle\Content\Application\ContentProjectionNormalizer\Enhancer;

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

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

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

return $viewData;
return $normalizeData;
}

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

return [
'excerptTags',
'excerptCategories',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ContentBundle\Content\Application\ContentProjectionNormalizer\Helper;
namespace Sulu\Bundle\ContentBundle\Content\Application\ContentProjectionNormalizer\Enhancer;

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

/**
* @return string[]
Expand Down
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 [];
}
}

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion DependencyInjection/SuluContentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('mapper.xml');
$loader->load('merger.xml');
$loader->load('normalizer-helper.xml');
$loader->load('normalize-enhancer.xml');
$loader->load('services.xml');
}
}
19 changes: 19 additions & 0 deletions Resources/config/normalize-enhancer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<!-- Enhancer -->
<service id="sulu_content.excerpt_normalize_enhancer" class="Sulu\Bundle\ContentBundle\Content\Application\ContentProjectionNormalizer\Enhancer\ExcerptNormalizeEnhancer">
<tag name="sulu_content.normalize_enhancer"/>
</service>

<service id="sulu_content.template_normalize_enhancer" class="Sulu\Bundle\ContentBundle\Content\Application\ContentProjectionNormalizer\Enhancer\TemplateNormalizeEnhancer">
<tag name="sulu_content.normalize_enhancer"/>
</service>

<service id="sulu_content.workflow_normalize_enhancer" class="Sulu\Bundle\ContentBundle\Content\Application\ContentProjectionNormalizer\Enhancer\WorkflowNormalizeEnhancer">
<tag name="sulu_content.normalize_enhancer"/>
</service>
</services>
</container>
19 changes: 0 additions & 19 deletions Resources/config/normalizer-helper.xml

This file was deleted.

2 changes: 1 addition & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@

<!-- ContentProjectionNormalizer -->
<service id="sulu_content.content_projection_normalizer" class="Sulu\Bundle\ContentBundle\Content\Application\ContentProjectionNormalizer\ContentProjectionNormalizer">
<argument type="tagged" tag="sulu_content.content_projection_normalizer_helper"/>
<argument type="tagged" tag="sulu_content.normalize_enhancer"/>
</service>

<service id="Sulu\Bundle\ContentBundle\Content\Application\ContentProjectionNormalizer\ContentProjectionNormalizerInterface" alias="sulu_content.content_projection_normalizer"/>
Expand Down
Loading

0 comments on commit d820692

Please sign in to comment.