Skip to content

Commit

Permalink
Extracted redundant Binary and Media SearchField into a common base
Browse files Browse the repository at this point in the history
  • Loading branch information
alongosz committed Jul 17, 2024
1 parent 8cd53fc commit 078461c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 124 deletions.
66 changes: 66 additions & 0 deletions src/lib/FieldType/BinaryBase/BinaryBaseSearchField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Core\FieldType\BinaryBase;

use Ibexa\Contracts\Core\FieldType\Indexable;
use Ibexa\Contracts\Core\Persistence\Content\Field;
use Ibexa\Contracts\Core\Persistence\Content\Type\FieldDefinition;
use Ibexa\Contracts\Core\Search;

/**
* @internal
*/
abstract class BinaryBaseSearchField implements Indexable
{
/**
* @return \Ibexa\Contracts\Core\Search\Field[]
*/
public function getIndexData(Field $field, FieldDefinition $fieldDefinition): array
{
return [
new Search\Field(
'file_name',
$field->value->externalData['fileName'] ?? null,
new Search\FieldType\StringField()
),
new Search\Field(
'file_size',
$field->value->externalData['fileSize'] ?? null,
new Search\FieldType\IntegerField()
),
new Search\Field(
'mime_type',
$field->value->externalData['mimeType'] ?? null,
new Search\FieldType\StringField()
),
];
}

/**
* @return array<string, \Ibexa\Contracts\Core\Search\FieldType>
*/
public function getIndexDefinition(): array
{
return [
'file_name' => new Search\FieldType\StringField(),
'file_size' => new Search\FieldType\IntegerField(),
'mime_type' => new Search\FieldType\StringField(),
];
}

public function getDefaultMatchField(): string
{
return 'file_name';
}

public function getDefaultSortField(): string
{
return $this->getDefaultMatchField();
}
}
65 changes: 3 additions & 62 deletions src/lib/FieldType/BinaryFile/SearchField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,15 @@
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Core\FieldType\BinaryFile;

use Ibexa\Contracts\Core\FieldType\Indexable;
use Ibexa\Contracts\Core\Persistence\Content\Field;
use Ibexa\Contracts\Core\Persistence\Content\Type\FieldDefinition;
use Ibexa\Contracts\Core\Search;
use Ibexa\Core\FieldType\BinaryBase\BinaryBaseSearchField;

/**
* Indexable definition for BinaryFile field type.
*/
class SearchField implements Indexable
class SearchField extends BinaryBaseSearchField
{
public function getIndexData(Field $field, FieldDefinition $fieldDefinition)
{
return [
new Search\Field(
'file_name',
$field->value->externalData['fileName'] ?? null,
new Search\FieldType\StringField()
),
new Search\Field(
'file_size',
$field->value->externalData['fileSize'] ?? null,
new Search\FieldType\IntegerField()
),
new Search\Field(
'mime_type',
$field->value->externalData['mimeType'] ?? null,
new Search\FieldType\StringField()
),
];
}

public function getIndexDefinition()
{
return [
'file_name' => new Search\FieldType\StringField(),
'file_size' => new Search\FieldType\IntegerField(),
'mime_type' => new Search\FieldType\StringField(),
];
}

/**
* Get name of the default field to be used for matching.
*
* As field types can index multiple fields (see MapLocation field type's
* implementation of this interface), this method is used to define default
* field for matching. Default field is typically used by Field criterion.
*
* @return string
*/
public function getDefaultMatchField()
{
return 'file_name';
}

/**
* Get name of the default field to be used for sorting.
*
* As field types can index multiple fields (see MapLocation field type's
* implementation of this interface), this method is used to define default
* field for sorting. Default field is typically used by Field sort clause.
*
* @return string
*/
public function getDefaultSortField()
{
return $this->getDefaultMatchField();
}
}
65 changes: 3 additions & 62 deletions src/lib/FieldType/Media/SearchField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,15 @@
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Core\FieldType\Media;

use Ibexa\Contracts\Core\FieldType\Indexable;
use Ibexa\Contracts\Core\Persistence\Content\Field;
use Ibexa\Contracts\Core\Persistence\Content\Type\FieldDefinition;
use Ibexa\Contracts\Core\Search;
use Ibexa\Core\FieldType\BinaryBase\BinaryBaseSearchField;

/**
* Indexable definition for Media field type.
*/
class SearchField implements Indexable
class SearchField extends BinaryBaseSearchField
{
public function getIndexData(Field $field, FieldDefinition $fieldDefinition)
{
return [
new Search\Field(
'file_name',
$field->value->externalData['fileName'] ?? null,
new Search\FieldType\StringField()
),
new Search\Field(
'file_size',
$field->value->externalData['fileSize'] ?? null,
new Search\FieldType\IntegerField()
),
new Search\Field(
'mime_type',
$field->value->externalData['mimeType'] ?? null,
new Search\FieldType\StringField()
),
];
}

public function getIndexDefinition()
{
return [
'file_name' => new Search\FieldType\StringField(),
'file_size' => new Search\FieldType\IntegerField(),
'mime_type' => new Search\FieldType\StringField(),
];
}

/**
* Get name of the default field to be used for matching.
*
* As field types can index multiple fields (see MapLocation field type's
* implementation of this interface), this method is used to define default
* field for matching. Default field is typically used by Field criterion.
*
* @return string
*/
public function getDefaultMatchField()
{
return 'file_name';
}

/**
* Get name of the default field to be used for sorting.
*
* As field types can index multiple fields (see MapLocation field type's
* implementation of this interface), this method is used to define default
* field for sorting. Default field is typically used by Field sort clause.
*
* @return string
*/
public function getDefaultSortField()
{
return $this->getDefaultMatchField();
}
}

0 comments on commit 078461c

Please sign in to comment.