Skip to content

Commit

Permalink
[Data Object] Adjust getDataForSetter method of data object adapters (#…
Browse files Browse the repository at this point in the history
…701)

* adapt first adapter

* fix date and boolean adapters

* Fixed VideoAdapter, UrlSlugAdapter

* Fixed StructuredTableAdapter.php

* fix geo adapters

* Fixed qv and qvr adapter

* fix image adapters

* Fixed iqv adapter

* Make sure to use object as type for getting data objects from core

* fix advance relations adapters

* fix encrypted field adapter

* Fixed object bricks adapter

* Fixed block adapter

* Adaptions for ClassificationStoreAdapter

* Fixed ClassificationStoreAdapter.php

* Apply php-cs-fixer changes

* fix sonar

* Apply php-cs-fixer changes

* use denormalize method as default setter value

* Apply php-cs-fixer changes

* Apply php-cs-fixer changes

* fix typo

* add missing strict types

---------

Co-authored-by: Marco Perberschlager <[email protected]>
  • Loading branch information
lukmzig and mcop1 authored Jan 22, 2025
1 parent b6d4375 commit 1718ec4
Show file tree
Hide file tree
Showing 35 changed files with 199 additions and 194 deletions.
3 changes: 0 additions & 3 deletions config/data_objects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ services:
Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Adapter\CalculatedValueAdapter:
tags: [ 'pimcore.studio_backend.data_adapter' ]

Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Adapter\CheckboxAdapter:
tags: [ 'pimcore.studio_backend.data_adapter' ]

Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Adapter\ClassificationStoreAdapter:
tags: [ 'pimcore.studio_backend.data_adapter' ]

Expand Down
3 changes: 1 addition & 2 deletions config/pimcore/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ pimcore_studio_backend:
- "advancedManyToManyObjectRelation"
Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Adapter\BooleanAdapter:
- "booleanSelect"
- "checkbox"
Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Adapter\CalculatedValueAdapter:
- "calculatedValue"
Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Adapter\CheckboxAdapter:
- "checkbox"
Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Adapter\ClassificationStoreAdapter:
- "classificationstore"
Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Adapter\DateAdapter:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Util\Trait\RelationDataTrait;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Util\Trait\RelationMetadataTrait;
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\ElementProviderTrait;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\ClassDefinition\Data\AdvancedManyToManyRelation;
use Pimcore\Model\DataObject\Concrete;
Expand All @@ -37,6 +38,7 @@
#[AutoconfigureTag(DataAdapterLoaderInterface::ADAPTER_TAG)]
final readonly class AdvancedManyToManyRelationAdapter implements SetterDataInterface, DataNormalizerInterface
{
use ElementProviderTrait;
use RelationDataTrait;
use RelationMetadataTrait;

Expand Down
3 changes: 1 addition & 2 deletions src/DataObject/Data/Adapter/BlockAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ private function processBlockElement(
?FieldContextData $contextData = null
): array {
$resultElement = [];
$blockElement = $rawBlockElement['data'] ?? null;
$fieldDefinitions = $fieldDefinition->getFieldDefinitions();
$fieldContextData = $this->createFieldContextData($element, $fieldDefinition, $contextData);

Expand All @@ -161,7 +160,7 @@ private function processBlockElement(
$element,
$fd,
$elementName,
$blockElement,
$rawBlockElement,
$fieldContextData
);
if (!$value) {
Expand Down
7 changes: 2 additions & 5 deletions src/DataObject/Data/Adapter/BooleanAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ public function getDataForSetter(
array $data,
?FieldContextData $contextData = null
): ?bool {
return match((int) $data[$key]) {
1 => true,
-1 => false,
default => null,
};

return $data[$key] ?? null;
}
}
45 changes: 0 additions & 45 deletions src/DataObject/Data/Adapter/CheckboxAdapter.php

This file was deleted.

40 changes: 25 additions & 15 deletions src/DataObject/Data/Adapter/ClassificationStoreAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
use Pimcore\Model\DataObject\Concrete;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use function in_array;
use function is_array;

/**
* @internal
Expand Down Expand Up @@ -82,11 +81,17 @@ public function getDataForSetter(
}

$store = $data[$key];
$activeGroups = $store['activeGroups'] ?? [];
if (empty($activeGroups)) {
return null;
}
$groupCollectionMapping = $store['groupCollectionMapping'] ?? [];
$container = $this->getContainer($element, $key, $contextData);
$this->setMapping($container, $store);
if (is_array($store['data'])) {
$this->setStoreValues($element, $fieldDefinition, $container, $store);
if (!empty($groupCollectionMapping)) {
$this->setMapping($container, $store['activeGroups'], $store['groupCollectionMapping']);
}
unset($store['activeGroups'], $store['groupCollectionMapping']);
$this->setStoreValues($element, $fieldDefinition, $container, $store);
$this->cleanupStoreGroups($container);

return $container;
Expand All @@ -105,7 +110,10 @@ public function normalize(
$validLanguages = $this->getValidLanguages($fieldDefinition);
$resultItems = [];

foreach ($this->getActiveGroups($value) as $groupId => $groupConfig) {
$resultItems['activeGroups'] = $value->getActiveGroups();
$resultItems['groupCollectionMapping'] = $value->getGroupCollectionMappings();

foreach ($this->getActiveGroupsConfig($resultItems['activeGroups']) as $groupId => $groupConfig) {
$resultItems[$groupId] = [];
$keys = $this->getClassificationStoreKeysFromGroup($groupId);
foreach ($validLanguages as $validLanguage) {
Expand Down Expand Up @@ -174,11 +182,11 @@ private function getContainer(
return $container;
}

private function setMapping(Classificationstore $container, array $data): void
{
$activeGroups = $data['activeGroups'];
$groupCollectionMapping = $data['groupCollectionMapping'];

private function setMapping(
Classificationstore $container,
array $activeGroups,
array $groupCollectionMapping
): void {
$correctedMapping = array_filter($groupCollectionMapping, static function ($groupId) use ($activeGroups) {
return isset($activeGroups[$groupId]) && $activeGroups[$groupId];
}, ARRAY_FILTER_USE_KEY);
Expand All @@ -195,9 +203,11 @@ private function setStoreValues(
Classificationstore $container,
array $store
): void {
$activeGroups = $store['activeGroups'];
foreach ($store['data'] as $language => $groups) {
foreach ($groups as $groupId => $keys) {

$activeGroups = [];

foreach ($store as $groupId => $groupData) {
foreach ($groupData as $language => $keys) {
$this->processGroupKeys($element, $definition, $container, $language, $groupId, $keys);
$activeGroups[$groupId] = true;
}
Expand Down Expand Up @@ -293,10 +303,10 @@ private function getValidLanguages(ClassificationstoreDefinition $classification
/**
* @return GroupConfig[]
*/
private function getActiveGroups(ClassificationstoreModel $value): array
private function getActiveGroupsConfig(array $activeGroups): array
{
$groups = [];
foreach ($value->getActiveGroups() as $groupId => $active) {
foreach ($activeGroups as $groupId => $active) {
if ($active) {
$groupConfig = $this->groupConfigResolver->getById($groupId);
if ($groupConfig) {
Expand Down
6 changes: 1 addition & 5 deletions src/DataObject/Data/Adapter/ConsentAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,9 @@ public function getDataForSetter(
array $data,
?FieldContextData $contextData = null
): Consent {
$value = $data[$key] ?? null;
$value = $data[$key] ? $data[$key]['consent'] : null;
$noteId = null;

if ($value === 'false') {
$value = false;
}

/** @var Consent|null $oldData */
$oldData = $element->get($key);

Expand Down
2 changes: 1 addition & 1 deletion src/DataObject/Data/Adapter/DateAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getDataForSetter(

if (is_numeric($dateData)) {
/** @var Date|Datetime $fieldDefinition */
return $fieldDefinition->denormalize($dateData / 1000);
return $fieldDefinition->denormalize($dateData);
}

if (is_string($dateData)) {
Expand Down
26 changes: 14 additions & 12 deletions src/DataObject/Data/Adapter/DateRangeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@

namespace Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Adapter;

use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\DataNormalizerInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Model\FieldContextData;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\SetterDataInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\Concrete;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use function count;
use function is_array;

/**
* @internal
*/
#[AutoconfigureTag(DataAdapterLoaderInterface::ADAPTER_TAG)]
final readonly class DateRangeAdapter implements SetterDataInterface
final readonly class DateRangeAdapter implements SetterDataInterface, DataNormalizerInterface
{
public function getDataForSetter(
Concrete $element,
Expand All @@ -41,21 +42,22 @@ public function getDataForSetter(
): ?CarbonPeriod {

$dateData = $data[$key];
if (is_array($dateData) && isset($dateData['start_date'], $dateData['end_date'])) {
$startDate = $this->getDateFromTimestamp($data['start_date'] / 1000);
$endDate = $this->getDateFromTimestamp($data['end_date'] / 1000);

return CarbonPeriod::create($startDate, $endDate);
if (!is_array($dateData) || count($dateData) !== 2) {
return null;
}

return null;
return CarbonPeriod::create($dateData[0], $dateData[1]);
}

private function getDateFromTimestamp(float|int|string $timestamp): Carbon
public function normalize(mixed $value, Data $fieldDefinition): array
{
$date = new Carbon();
$date->setTimestamp($timestamp);
if (!$value instanceof CarbonPeriod) {
return [];
}

return $date;
return [
$value->getStartDate(),
$value->getEndDate(),
];
}
}
2 changes: 1 addition & 1 deletion src/DataObject/Data/Adapter/EncryptedFieldAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private function handleDelegatedField(
array $data,
?FieldContextData $contextData = null
): ?EncryptedField {
$adapter = $this->dataAdapterService->tryDataAdapter($fieldDefinition->getFieldType());
$adapter = $this->dataAdapterService->tryDataAdapter($delegateFieldDefinition->getFieldType());
if ($adapter instanceof SetterDataInterface) {
return new EncryptedField(
$fieldDefinition->getDelegate(),
Expand Down
9 changes: 3 additions & 6 deletions src/DataObject/Data/Adapter/ExternalImageAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@ public function getDataForSetter(
string $key,
array $data,
?FieldContextData $contextData = null
): ?string {
$imageData = $data[$key] ?? null;
if (!$imageData instanceof ExternalImage) {
return null;
}
): ?ExternalImage {
$url = $data[$key]['url'] ?? null;

return $imageData->getUrl();
return $url ? new ExternalImage($url) : null;
}
}
41 changes: 34 additions & 7 deletions src/DataObject/Data/Adapter/GeoBoundsAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Model\FieldContextData;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\SetterDataInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\DataObject\Coordinates;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject\Data\Geobounds;
Expand All @@ -32,6 +33,12 @@
#[AutoconfigureTag(DataAdapterLoaderInterface::ADAPTER_TAG)]
final readonly class GeoBoundsAdapter implements SetterDataInterface
{
private const string NORTH_EAST = 'northEast';

private const string SOUTH_WEST = 'southWest';

private const array DIRECTIONS = [self::NORTH_EAST, self::SOUTH_WEST];

public function getDataForSetter(
Concrete $element,
Data $fieldDefinition,
Expand All @@ -40,17 +47,37 @@ public function getDataForSetter(
?FieldContextData $contextData = null
): ?Geobounds {

$geoPointData = $data[$key];
if (!is_array($geoPointData) ||
$geoPointData['NElongitude'] === null || $geoPointData['NElatitude'] === null ||
$geoPointData['SWlongitude'] === null || $geoPointData['SWlatitude'] === null
) {
$geoBoundsData = $data[$key];
if ($this->validateBounds($geoBoundsData) === false) {
return null;
}

return new Geobounds(
new GeoCoordinates($data['NElatitude'], $data['NElongitude']),
new GeoCoordinates($data['SWlatitude'], $data['SWlongitude'])
new GeoCoordinates(
$geoBoundsData[self::NORTH_EAST][Coordinates::LATITUDE->value],
$geoBoundsData[self::NORTH_EAST][Coordinates::LONGITUDE->value]
),
new GeoCoordinates(
$geoBoundsData[self::SOUTH_WEST][Coordinates::LATITUDE->value],
$geoBoundsData[self::SOUTH_WEST][Coordinates::LONGITUDE->value]
)
);
}

private function validateBounds(array $data): bool
{
foreach (self::DIRECTIONS as $direction) {
if (!isset($data[$direction]) || !is_array($data[$direction])) {
return false;
}

foreach (Coordinates::values() as $coordinate) {
if (empty($data[$direction][$coordinate])) {
return false;
}
}
}

return true;
}
}
6 changes: 5 additions & 1 deletion src/DataObject/Data/Adapter/GeoPointAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Model\FieldContextData;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\SetterDataInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\DataObject\Coordinates;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject\Data\GeoCoordinates;
Expand All @@ -44,6 +45,9 @@ public function getDataForSetter(
return null;
}

return new GeoCoordinates($geoPointData['latitude'], $geoPointData['longitude']);
return new GeoCoordinates(
$geoPointData[Coordinates::LATITUDE->value],
$geoPointData[Coordinates::LONGITUDE->value]
);
}
}
Loading

0 comments on commit 1718ec4

Please sign in to comment.