-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AuthorInterface and dynamic settings form (#193)
- Loading branch information
Showing
47 changed files
with
1,499 additions
and
53 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
72 changes: 72 additions & 0 deletions
72
Content/Application/ContentDataMapper/DataMapper/AuthorDataMapper.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,72 @@ | ||
<?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\ContentDataMapper\DataMapper; | ||
|
||
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\ContactFactoryInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\AuthorInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface; | ||
|
||
class AuthorDataMapper implements DataMapperInterface | ||
{ | ||
/** | ||
* @var ContactFactoryInterface | ||
*/ | ||
private $contactFactory; | ||
|
||
public function __construct(ContactFactoryInterface $contactFactory) | ||
{ | ||
$this->contactFactory = $contactFactory; | ||
} | ||
|
||
public function map( | ||
array $data, | ||
DimensionContentCollectionInterface $dimensionContentCollection | ||
): void { | ||
$dimensionAttributes = $dimensionContentCollection->getDimensionAttributes(); | ||
$unlocalizedDimensionAttributes = array_merge($dimensionAttributes, ['locale' => null]); | ||
$unlocalizedObject = $dimensionContentCollection->getDimensionContent($unlocalizedDimensionAttributes); | ||
|
||
if (!$unlocalizedObject instanceof AuthorInterface) { | ||
return; | ||
} | ||
|
||
$localizedObject = $dimensionContentCollection->getDimensionContent($dimensionAttributes); | ||
|
||
if ($localizedObject) { | ||
if (!$localizedObject instanceof AuthorInterface) { | ||
throw new \RuntimeException(sprintf('Expected "$localizedObject" from type "%s" but "%s" given.', AuthorInterface::class, \get_class($localizedObject))); | ||
} | ||
|
||
$this->setAuthorData($localizedObject, $data); | ||
|
||
return; | ||
} | ||
|
||
$this->setAuthorData($unlocalizedObject, $data); | ||
} | ||
|
||
/** | ||
* @param mixed[] $data | ||
*/ | ||
private function setAuthorData(AuthorInterface $dimensionContent, array $data): void | ||
{ | ||
if (isset($data['author'])) { | ||
$dimensionContent->setAuthor($this->contactFactory->create($data['author'])); | ||
} | ||
|
||
if (isset($data['authored'])) { | ||
$dimensionContent->setAuthored(new \DateTimeImmutable($data['authored'])); | ||
} | ||
} | ||
} |
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,38 @@ | ||
<?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\ContentMerger\Merger; | ||
|
||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\AuthorInterface; | ||
|
||
class AuthorMerger implements MergerInterface | ||
{ | ||
public function merge(object $targetObject, object $sourceObject): void | ||
{ | ||
if (!$targetObject instanceof AuthorInterface) { | ||
return; | ||
} | ||
|
||
if (!$sourceObject instanceof AuthorInterface) { | ||
return; | ||
} | ||
|
||
if ($author = $sourceObject->getAuthor()) { | ||
$targetObject->setAuthor($author); | ||
} | ||
|
||
if ($authored = $sourceObject->getAuthored()) { | ||
$targetObject->setAuthored($authored); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
Content/Application/ContentNormalizer/Normalizer/AuthorNormalizer.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,39 @@ | ||
<?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\ContentNormalizer\Normalizer; | ||
|
||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\AuthorInterface; | ||
|
||
class AuthorNormalizer implements NormalizerInterface | ||
{ | ||
public function enhance(object $object, array $normalizedData): array | ||
{ | ||
if (!$object instanceof AuthorInterface) { | ||
return $normalizedData; | ||
} | ||
|
||
$normalizedData['author'] = null !== $object->getAuthor() ? $object->getAuthor()->getId() : null; | ||
|
||
return $normalizedData; | ||
} | ||
|
||
public function getIgnoredAttributes(object $object): array | ||
{ | ||
if (!$object instanceof AuthorInterface) { | ||
return []; | ||
} | ||
|
||
return ['author']; | ||
} | ||
} |
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,21 @@ | ||
<?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\Domain\Factory; | ||
|
||
use Sulu\Bundle\ContactBundle\Entity\ContactInterface; | ||
|
||
interface ContactFactoryInterface | ||
{ | ||
public function create(?int $contactId): ?ContactInterface; | ||
} |
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,27 @@ | ||
<?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\Domain\Model; | ||
|
||
use Sulu\Bundle\ContactBundle\Entity\ContactInterface; | ||
|
||
interface AuthorInterface | ||
{ | ||
public function getAuthor(): ?ContactInterface; | ||
|
||
public function setAuthor(?ContactInterface $author): void; | ||
|
||
public function getAuthored(): ?\DateTimeImmutable; | ||
|
||
public function setAuthored(?\DateTimeImmutable $authored): void; | ||
} |
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,52 @@ | ||
<?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\Domain\Model; | ||
|
||
use Sulu\Bundle\ContactBundle\Entity\ContactInterface; | ||
|
||
/** | ||
* Basic implementation of the AuthorInterface. | ||
*/ | ||
trait AuthorTrait | ||
{ | ||
/** | ||
* @var ContactInterface|null | ||
*/ | ||
private $author; | ||
|
||
/** | ||
* @var \DateTimeImmutable|null | ||
*/ | ||
private $authored; | ||
|
||
public function getAuthor(): ?ContactInterface | ||
{ | ||
return $this->author; | ||
} | ||
|
||
public function setAuthor(?ContactInterface $author): void | ||
{ | ||
$this->author = $author; | ||
} | ||
|
||
public function getAuthored(): ?\DateTimeImmutable | ||
{ | ||
return $this->authored; | ||
} | ||
|
||
public function setAuthored(?\DateTimeImmutable $authored): void | ||
{ | ||
$this->authored = $authored; | ||
} | ||
} |
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,46 @@ | ||
<?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\Infrastructure\Doctrine; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Sulu\Bundle\ContactBundle\Entity\ContactInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\ContactFactoryInterface; | ||
|
||
class ContactFactory implements ContactFactoryInterface | ||
{ | ||
/** | ||
* @var EntityManagerInterface | ||
*/ | ||
private $entityManager; | ||
|
||
public function __construct(EntityManagerInterface $entityManager) | ||
{ | ||
$this->entityManager = $entityManager; | ||
} | ||
|
||
public function create(?int $contactId): ?ContactInterface | ||
{ | ||
if (!$contactId) { | ||
return null; | ||
} | ||
|
||
/** @var ContactInterface|null $contact */ | ||
$contact = $this->entityManager->getPartialReference( | ||
ContactInterface::class, | ||
$contactId | ||
); | ||
|
||
return $contact; | ||
} | ||
} |
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.