Skip to content

Commit

Permalink
fix: rather skip setter than use null value for it
Browse files Browse the repository at this point in the history
  • Loading branch information
lukmzig committed Nov 28, 2024
1 parent cf6d7a5 commit b6b9d58
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 65 deletions.
34 changes: 31 additions & 3 deletions src/DataObject/Data/Adapter/BlockAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\FieldContextData;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\SetterDataInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Util\Trait\ValidateFieldTypeTrait;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidDataTypeException;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\ClassDefinition\Data\Block;
Expand All @@ -37,7 +39,10 @@
#[AutoconfigureTag(DataAdapterLoaderInterface::ADAPTER_TAG)]
final readonly class BlockAdapter implements SetterDataInterface, DataNormalizerInterface
{
use ValidateFieldTypeTrait;

public function __construct(
private DataAdapterServiceInterface $dataAdapterService,
private DataServiceInterface $dataService
) {
}
Expand Down Expand Up @@ -146,13 +151,24 @@ private function processBlockElement(
$fieldContextData = $this->createFieldContextData($element, $fieldDefinition, $contextData);

foreach ($fieldDefinitions as $elementName => $fd) {
$resultElement[$elementName] = $this->createBlockElement(
$adapter = $this->dataAdapterService->tryDataAdapter($fd->getFieldType());
if (!$adapter) {
continue;
}

$value = $this->createBlockElement(
$adapter,
$element,
$fd,
$elementName,
$blockElement,
$fieldContextData
);
if (!$value) {
continue;
}

$resultElement[$elementName] = $value;
}

return $resultElement;
Expand All @@ -162,19 +178,31 @@ private function processBlockElement(
* @throws Exception
*/
private function createBlockElement(
SetterDataInterface $adapter,
Concrete $element,
Data $fieldDefinition,
string $elementName,
?array $blockElement,
?FieldContextData $fieldContextData = null
): BlockElement {
): ?BlockElement {
$elementType = $fieldDefinition->getFieldtype();
$elementData = $blockElement[$elementName] ?? null;

$data = $adapter->getDataForSetter(
$element,
$fieldDefinition,
$elementName,
[$elementName => $elementData],
$fieldContextData
);
if (!$this->validateEncryptedField($fieldDefinition, $data)) {
return null;
}

return new BlockElement(
$elementName,
$elementType,
$this->dataService->getAdapterSetterValue(
$adapter->getDataForSetter(
$element,
$fieldDefinition,
$elementName,
Expand Down
15 changes: 14 additions & 1 deletion src/DataObject/Data/Adapter/ClassificationStoreAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\FieldContextData;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\SetterDataInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Util\Trait\ValidateFieldTypeTrait;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\DatabaseException;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\ClassDefinition\Data\Classificationstore as ClassificationstoreDefinition;
Expand All @@ -46,8 +48,11 @@
#[AutoconfigureTag(DataAdapterLoaderInterface::ADAPTER_TAG)]
final readonly class ClassificationStoreAdapter implements SetterDataInterface, DataNormalizerInterface
{
use ValidateFieldTypeTrait;

public function __construct(
private DefinitionCacheResolverInterface $definitionCacheResolver,
private DataAdapterServiceInterface $dataAdapterService,
private DataServiceInterface $dataService,
private GroupConfigResolverInterface $groupConfigResolver,
private ServiceResolverInterface $serviceResolver,
Expand Down Expand Up @@ -180,12 +185,20 @@ private function processGroupKeys(
continue;
}

$setterData = $this->dataService->getAdapterSetterValue(
$adapter = $this->dataAdapterService->tryDataAdapter($fieldDefinition->getFieldType());
if ($adapter === null) {
continue;
}

$setterData = $adapter->getDataForSetter(
$element,
$fieldDefinition,
$fieldDefinition->getName(),
[$fieldDefinition->getName() => $value]
);
if (!$this->validateEncryptedField($fieldDefinition, $setterData)) {
continue;
}

$container->setLocalizedKeyValue($groupId, $keyId, $setterData, $language);
}
Expand Down
37 changes: 31 additions & 6 deletions src/DataObject/Data/Adapter/EncryptedFieldAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\FieldContextData;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\SetterDataInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterServiceInterface;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\ClassDefinition\Data\EncryptedField as EncryptedFieldDefinition;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject\Data\EncryptedField;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
Expand All @@ -32,7 +33,7 @@
final readonly class EncryptedFieldAdapter implements SetterDataInterface
{
public function __construct(
private DataServiceInterface $dataService
private DataAdapterServiceInterface $dataAdapterService
) {
}

Expand All @@ -43,7 +44,7 @@ public function getDataForSetter(
array $data,
?FieldContextData $contextData = null
): ?EncryptedField {
if (!($fieldDefinition instanceof Data\EncryptedField)) {
if (!$fieldDefinition instanceof EncryptedFieldDefinition) {
return null;
}

Expand All @@ -52,9 +53,33 @@ public function getDataForSetter(
return null;
}

return new EncryptedField(
$fieldDefinition->getDelegate(),
$this->dataService->getAdapterSetterValue($element, $delegateFieldDefinition, $key, $data, $contextData)
return $this->handleDelegatedField(
$element,
$delegateFieldDefinition,
$fieldDefinition,
$key,
$data,
$contextData
);
}

private function handleDelegatedField(
Concrete $element,
Data $delegateFieldDefinition,
EncryptedFieldDefinition $fieldDefinition,
string $key,
array $data,
?FieldContextData $contextData = null
): ?EncryptedField
{
$adapter = $this->dataAdapterService->tryDataAdapter($fieldDefinition->getFieldType());
if ($adapter instanceof SetterDataInterface) {
return new EncryptedField(
$fieldDefinition->getDelegate(),
$adapter->getDataForSetter($element, $delegateFieldDefinition, $key, $data, $contextData)
);
}

return null;
}
}
15 changes: 13 additions & 2 deletions src/DataObject/Data/Adapter/FieldCollectionsAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\FieldContextData;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\SetterDataInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Util\Trait\ValidateFieldTypeTrait;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\ClassDefinition\Data\Fieldcollections;
use Pimcore\Model\DataObject\Concrete;
Expand All @@ -37,7 +39,10 @@
#[AutoconfigureTag(DataAdapterLoaderInterface::ADAPTER_TAG)]
final readonly class FieldCollectionsAdapter implements SetterDataInterface, DataNormalizerInterface
{
use ValidateFieldTypeTrait;

public function __construct(
private DataAdapterServiceInterface $dataAdapterService,
private DataServiceInterface $dataService,
private DefinitionResolverInterface $fieldCollectionDefinition,
private Factory $modelFactory
Expand Down Expand Up @@ -144,17 +149,23 @@ private function processCollectionRaw(

foreach ($collectionDef?->getFieldDefinitions() as $elementName => $fd) {
$elementValue = $blockElement[$elementName] ?? null;
if (!$elementValue) {
$adapter = $this->dataAdapterService->tryDataAdapter($fd->getFieldType());
if (!$elementValue || !$adapter) {
continue;
}

$collectionData[$elementName] = $this->dataService->getAdapterSetterValue(
$value = $adapter->getDataForSetter(
$element,
$fd,
$elementName,
[$elementName => $elementValue],
$fieldContextData
);
if (!$this->validateEncryptedField($fieldDefinition, $value)) {
continue;
}

$collectionData[$elementName] = $value;
}

return $collectionData;
Expand Down
29 changes: 20 additions & 9 deletions src/DataObject/Data/Adapter/LocalizedFieldsAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\FieldContextData;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\SetterDataInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Util\Trait\ValidateFieldTypeTrait;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\DatabaseException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
Expand All @@ -42,7 +44,10 @@
#[AutoconfigureTag(DataAdapterLoaderInterface::ADAPTER_TAG)]
final readonly class LocalizedFieldsAdapter implements SetterDataInterface, DataNormalizerInterface
{
use ValidateFieldTypeTrait;

public function __construct(
private DataAdapterServiceInterface $dataAdapterService,
private DataServiceInterface $dataService,
private SecurityServiceInterface $securityService,
) {
Expand Down Expand Up @@ -73,17 +78,23 @@ public function getDataForSetter(
continue;
}

$localizedField->setLocalizedValue(
$adapter = $this->dataAdapterService->tryDataAdapter($childFieldDefinition->getFieldType());
if (!$adapter) {
continue;
}

$value = $adapter->getDataForSetter(
$element,
$childFieldDefinition,
$name,
$this->dataService->getAdapterSetterValue(
$element,
$childFieldDefinition,
$name,
[$name => $fieldData],
new FieldContextData(language: $language)
),
$language
[$name => $fieldData],
new FieldContextData(language: $language)
);
if (!$this->validateEncryptedField($childFieldDefinition, $value)) {
continue;
}

$localizedField->setLocalizedValue($name, $value, $language);
}
}

Expand Down
15 changes: 13 additions & 2 deletions src/DataObject/Data/Adapter/ObjectBricksAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\FieldContextData;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\SetterDataInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Util\Trait\ValidateFieldTypeTrait;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\ClassDefinition\Data\Objectbricks;
use Pimcore\Model\DataObject\Concrete;
Expand All @@ -38,7 +40,10 @@
#[AutoconfigureTag(DataAdapterLoaderInterface::ADAPTER_TAG)]
final readonly class ObjectBricksAdapter implements SetterDataInterface, DataNormalizerInterface
{
use ValidateFieldTypeTrait;

public function __construct(
private DataAdapterServiceInterface $dataAdapterService,
private DataServiceInterface $dataService,
private DefinitionResolverInterface $definitionResolver
) {
Expand Down Expand Up @@ -175,18 +180,24 @@ private function getCollectionData(
): array {
$collectionData = [];
foreach ($collectionDef->getFieldDefinitions() as $fd) {
$adapter = $this->dataAdapterService->tryDataAdapter($fd->getFieldType());
$fieldName = $fd->getName();
if (!array_key_exists($fieldName, $rawData)) {
if (!array_key_exists($fieldName, $rawData) || !$adapter) {
continue;
}

$collectionData[$fd->getName()] = $this->dataService->getAdapterSetterValue(
$value = $adapter->getDataForSetter(
$element,
$fd,
$fieldName,
[$fieldName => $rawData[$fieldName]],
new FieldContextData($brick)
);
if (!$this->validateEncryptedField($fd, $value)) {
continue;
}

$collectionData[$fieldName] = $value;
}

return $collectionData;
Expand Down
10 changes: 10 additions & 0 deletions src/DataObject/Service/DataAdapterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,14 @@ public function getDataAdapter(string $fieldDefinitionType): SetterDataInterface
$this->getFieldDefinitionAdapterClass($fieldDefinitionType)
);
}

// ToDo: Consider removing this method and using getDataAdapter when field types from bundles are implemented
public function tryDataAdapter(string $fieldDefinitionType): ?SetterDataInterface
{
try {
return $this->getDataAdapter($fieldDefinitionType);
} catch (InvalidArgumentException) {
return null;
}
}
}
2 changes: 2 additions & 0 deletions src/DataObject/Service/DataAdapterServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ public function getFieldDefinitionAdapterClass(string $fieldDefinitionType): str
* @throws InvalidArgumentException
*/
public function getDataAdapter(string $fieldDefinitionType): SetterDataInterface;

public function tryDataAdapter(string $fieldDefinitionType): ?SetterDataInterface;
}
Loading

0 comments on commit b6b9d58

Please sign in to comment.