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

[Data Object] Adjust getDataForSetter method of data object adapters #701

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7c6523a
adapt first adapter
lukmzig Jan 15, 2025
9baf482
fix date and boolean adapters
lukmzig Jan 15, 2025
ceec5a0
Fixed VideoAdapter, UrlSlugAdapter
mcop1 Jan 15, 2025
a1b1e3e
Fixed StructuredTableAdapter.php
mcop1 Jan 16, 2025
b7615b1
fix geo adapters
lukmzig Jan 16, 2025
7e7e32a
Fixed qv and qvr adapter
mcop1 Jan 16, 2025
87df4b1
Merge remote-tracking branch 'origin/688-data-object-adjust-getdatafo…
mcop1 Jan 16, 2025
436de86
fix image adapters
lukmzig Jan 16, 2025
df3f337
Merge remote-tracking branch 'origin/688-data-object-adjust-getdatafo…
lukmzig Jan 16, 2025
e3972f1
Fixed iqv adapter
mcop1 Jan 16, 2025
f81e10b
Merge remote-tracking branch 'origin/688-data-object-adjust-getdatafo…
mcop1 Jan 16, 2025
9fc37cf
Make sure to use object as type for getting data objects from core
mcop1 Jan 16, 2025
01d0907
fix advance relations adapters
lukmzig Jan 16, 2025
74b4061
fix encrypted field adapter
lukmzig Jan 16, 2025
aed9661
Fixed object bricks adapter
mcop1 Jan 16, 2025
adea86e
Merge remote-tracking branch 'origin/688-data-object-adjust-getdatafo…
mcop1 Jan 16, 2025
e0e1b17
Fixed block adapter
mcop1 Jan 16, 2025
5b13d69
Adaptions for ClassificationStoreAdapter
mcop1 Jan 16, 2025
6b29235
Fixed ClassificationStoreAdapter.php
mcop1 Jan 17, 2025
b33290a
Apply php-cs-fixer changes
lukmzig Jan 17, 2025
b0b5439
fix sonar
lukmzig Jan 17, 2025
a397c46
Apply php-cs-fixer changes
lukmzig Jan 17, 2025
e669380
Merge branch '1.x' into 688-data-object-adjust-getdataforsetter-metho…
lukmzig Jan 20, 2025
8f9e19f
use denormalize method as default setter value
lukmzig Jan 20, 2025
a7f97be
Apply php-cs-fixer changes
lukmzig Jan 20, 2025
4e91556
Merge branch '1.x' into 688-data-object-adjust-getdataforsetter-metho…
lukmzig Jan 22, 2025
27cb2a5
Apply php-cs-fixer changes
lukmzig Jan 22, 2025
1b60c6e
fix typo
lukmzig Jan 22, 2025
35bbe8b
add missing strict types
lukmzig Jan 22, 2025
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
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
Loading