-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracted common base for TextBlock and TextLine field types (#406)
For more details see #406 Key changes: * Extracted common base for TextBlock and TextLine field types * [Tests] Aligned tests with TextBlock and TextLine changes * [Tests] Reduced complexity of TextBlock and TextLine test classes * [PHPStan] Aligned baseline with the changes --------- Co-authored-by: Paweł Niedzielski <[email protected]>
- Loading branch information
Showing
9 changed files
with
261 additions
and
774 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?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; | ||
|
||
use Ibexa\Contracts\Core\FieldType\Value as FieldTypeValueInterface; | ||
use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition; | ||
use Ibexa\Core\Base\Exceptions\InvalidArgumentType; | ||
use Ibexa\Core\FieldType\Value as BaseValue; | ||
use JMS\TranslationBundle\Translation\TranslationContainerInterface; | ||
|
||
/** | ||
* @internal | ||
* | ||
* Base implementation for TextLine\Type and TextBlock\Type which extends TextLine\Type. | ||
*/ | ||
abstract class BaseTextType extends FieldType implements TranslationContainerInterface | ||
{ | ||
public function isSearchable(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @param \Ibexa\Core\FieldType\TextLine\Value $value | ||
*/ | ||
public function getName( | ||
FieldTypeValueInterface $value, | ||
FieldDefinition $fieldDefinition, | ||
string $languageCode | ||
): string { | ||
return (string)$value->text; | ||
} | ||
|
||
/** | ||
* @param \Ibexa\Core\FieldType\TextLine\Value $value | ||
*/ | ||
public function isEmptyValue(FieldTypeValueInterface $value): bool | ||
{ | ||
return $value->text === null || trim($value->text) === ''; | ||
} | ||
|
||
/** | ||
* @param \Ibexa\Core\FieldType\TextLine\Value $value | ||
* | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException If the value does not match the expected structure. | ||
*/ | ||
protected function checkValueStructure(BaseValue $value): void | ||
{ | ||
if (!is_string($value->text)) { | ||
throw new InvalidArgumentType( | ||
'$value->text', | ||
'string', | ||
$value->text | ||
); | ||
} | ||
} | ||
|
||
protected function buildUnknownValidatorError(string $parameterName, string $validatorIdentifier): ValidationError | ||
{ | ||
return new ValidationError( | ||
"Validator '$parameterName' is unknown", | ||
null, | ||
[ | ||
$parameterName => $validatorIdentifier, | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @param \Ibexa\Core\FieldType\TextLine\Value $value | ||
*/ | ||
public function toHash(FieldTypeValueInterface $value): ?string | ||
{ | ||
if ($this->isEmptyValue($value)) { | ||
return null; | ||
} | ||
|
||
return $value->text; | ||
} | ||
} |
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
Oops, something went wrong.