Skip to content

Commit

Permalink
PD-95 Add AdvancedManyToManyRelationAdapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
martineiber committed Feb 6, 2024
1 parent 383b976 commit 840eb9d
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ services:
- { name: "pimcore.generic_data_index.data-object.search_index_field_definition", type: "manyToManyRelation" }


#Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\DataObject\FieldDefinitionAdapter\AdvancedManyToManyRelationAdapter:
# shared: false
# tags:
# - { name: "pimcore.generic_data_index.data-object.search_index_field_definition", type: "advancedManyToManyRelation" }
Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\DataObject\FieldDefinitionAdapter\AdvancedManyToManyRelationAdapter:
shared: false
tags:
- { name: "pimcore.generic_data_index.data-object.search_index_field_definition", type: "advancedManyToManyRelation" }

#Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\DataObject\FieldDefinitionAdapter\AdvancedManyToManyObjectRelationAdapter:
# shared: false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/


namespace Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\DataObject\FieldDefinitionAdapter;

use InvalidArgumentException;
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\OpenSearch\AttributeType;
use Pimcore\Model\DataObject\ClassDefinition\Data\AdvancedManyToManyRelation;

/**
* @internal
*/
final class AdvancedManyToManyRelationAdapter extends AbstractAdapter
{

public function getOpenSearchMapping(): array
{
$fieldDefinition = $this->getFieldDefinition();

if (!$fieldDefinition instanceof AdvancedManyToManyRelation) {
throw new InvalidArgumentException('FieldDefinition must be of type AdvancedManyToManyRelation');
}

$columnDefinition = $this->getColumnDefinition($fieldDefinition->getColumns());

return [
'properties' => [
'fieldname' => [
'type' => AttributeType::KEYWORD,
],
'columns' => [
'type' => AttributeType::KEYWORD, // Is actually an array of strings
],
'element' => [
'properties' => [
'id' => [
'type' => AttributeType::LONG,
],
'type' => [
'type' => AttributeType::TEXT,
],
],
],
'data' => [
'properties' => $columnDefinition
],
],
];
}

private function getColumnDefinition(array $columns): array {
$type = [];
foreach ($columns as $column) {
if (isset($column['type']) && isset($column['key'])) {

Check notice on line 65 in src/Service/SearchIndex/DataObject/FieldDefinitionAdapter/AdvancedManyToManyRelationAdapter.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

'isset(...)' constructs can be merged

\[EA\] This can be merged into the previous 'isset(..., ...\[, ...\])'.

Check notice on line 65 in src/Service/SearchIndex/DataObject/FieldDefinitionAdapter/AdvancedManyToManyRelationAdapter.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

'isset(...)' constructs can be merged

\[EA\] This can be merged into the previous 'isset(..., ...\[, ...\])'.
match ($column['type']) {
'number' => $type[$column['key']] = [
'type' => AttributeType::LONG
],
default => $type[$column['key']] = [
'type' => AttributeType::KEYWORD
]
};
}
}

return $type;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/

namespace Pimcore\Bundle\GenericDataIndexBundle\Tests\Unit\Service\SearchIndex\DataObject\FieldDefinitionAdapter;

use Codeception\Test\Unit;
use InvalidArgumentException;
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\OpenSearch\AttributeType;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\DataObject\FieldDefinitionAdapter\AdvancedManyToManyRelationAdapter;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\SearchIndexConfigServiceInterface;
use Pimcore\Model\DataObject\ClassDefinition\Data\AdvancedManyToManyRelation;
use Pimcore\Model\DataObject\ClassDefinition\Data\Checkbox;

/**
* @internal
*/
final class AdvancedManyToManyRelationAdapterTest extends Unit
{
public function testGetOpenSearchMapping(): void
{
$searchIndexConfigServiceInterfaceMock = $this->makeEmpty(SearchIndexConfigServiceInterface::class);
$adapter = new AdvancedManyToManyRelationAdapter($searchIndexConfigServiceInterfaceMock);
$relation = new AdvancedManyToManyRelation();
$relation->setColumns([
[
'type' => 'number',
'key' => 'key1',
'position' => 1,
],
[
'type' => 'select',
'key' => 'key2',
'position' => 1,
],]);
$adapter->setFieldDefinition($relation);

$this->assertSame([
'properties' => [
'fieldname' => [
'type' => AttributeType::KEYWORD,
],
'columns' => [
'type' => AttributeType::KEYWORD,
],
'element' => [
'properties' => [
'id' => [
'type' => AttributeType::LONG,
],
'type' => [
'type' => AttributeType::TEXT,
],
],
],
'data' => [
'properties' => [
'key1' => [
'type' => AttributeType::LONG,
],
'key2' => [
'type' => AttributeType::KEYWORD,
],
],
]
],
], $adapter->getOpenSearchMapping());
}

public function testGetOpenSearchMappingException(): void
{
$searchIndexConfigServiceInterfaceMock = $this->makeEmpty(SearchIndexConfigServiceInterface::class);
$adapter = new AdvancedManyToManyRelationAdapter($searchIndexConfigServiceInterfaceMock);
$relation = new Checkbox();
$adapter->setFieldDefinition($relation);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('FieldDefinition must be of type AdvancedManyToManyRelation');
$adapter->getOpenSearchMapping();
}
}

0 comments on commit 840eb9d

Please sign in to comment.