-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PD-95 Add AdvancedManyToManyRelationAdapter.
- Loading branch information
1 parent
383b976
commit 840eb9d
Showing
3 changed files
with
172 additions
and
4 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
79 changes: 79 additions & 0 deletions
79
...rvice/SearchIndex/DataObject/FieldDefinitionAdapter/AdvancedManyToManyRelationAdapter.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,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 GitHub Actions / Qodana for PHP'isset(...)' constructs can be merged
Check notice on line 65 in src/Service/SearchIndex/DataObject/FieldDefinitionAdapter/AdvancedManyToManyRelationAdapter.php GitHub Actions / Qodana for PHP'isset(...)' constructs can be merged
|
||
match ($column['type']) { | ||
'number' => $type[$column['key']] = [ | ||
'type' => AttributeType::LONG | ||
], | ||
default => $type[$column['key']] = [ | ||
'type' => AttributeType::KEYWORD | ||
] | ||
}; | ||
} | ||
} | ||
|
||
return $type; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...e/SearchIndex/DataObject/FieldDefinitionAdapter/AdvancedManyToManyRelationAdapterTest.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,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(); | ||
} | ||
} |