Skip to content

Commit d0b9826

Browse files
alongoszSteveb-p
authored andcommitted
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]>
1 parent 71fcbd4 commit d0b9826

File tree

9 files changed

+261
-774
lines changed

9 files changed

+261
-774
lines changed

src/lib/FieldType/BaseTextType.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Core\FieldType;
10+
11+
use Ibexa\Contracts\Core\FieldType\Value as FieldTypeValueInterface;
12+
use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition;
13+
use Ibexa\Core\Base\Exceptions\InvalidArgumentType;
14+
use Ibexa\Core\FieldType\Value as BaseValue;
15+
use JMS\TranslationBundle\Translation\TranslationContainerInterface;
16+
17+
/**
18+
* @internal
19+
*
20+
* Base implementation for TextLine\Type and TextBlock\Type which extends TextLine\Type.
21+
*/
22+
abstract class BaseTextType extends FieldType implements TranslationContainerInterface
23+
{
24+
public function isSearchable(): bool
25+
{
26+
return true;
27+
}
28+
29+
/**
30+
* @param \Ibexa\Core\FieldType\TextLine\Value $value
31+
*/
32+
public function getName(
33+
FieldTypeValueInterface $value,
34+
FieldDefinition $fieldDefinition,
35+
string $languageCode
36+
): string {
37+
return (string)$value->text;
38+
}
39+
40+
/**
41+
* @param \Ibexa\Core\FieldType\TextLine\Value $value
42+
*/
43+
public function isEmptyValue(FieldTypeValueInterface $value): bool
44+
{
45+
return $value->text === null || trim($value->text) === '';
46+
}
47+
48+
/**
49+
* @param \Ibexa\Core\FieldType\TextLine\Value $value
50+
*
51+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException If the value does not match the expected structure.
52+
*/
53+
protected function checkValueStructure(BaseValue $value): void
54+
{
55+
if (!is_string($value->text)) {
56+
throw new InvalidArgumentType(
57+
'$value->text',
58+
'string',
59+
$value->text
60+
);
61+
}
62+
}
63+
64+
protected function buildUnknownValidatorError(string $parameterName, string $validatorIdentifier): ValidationError
65+
{
66+
return new ValidationError(
67+
"Validator '$parameterName' is unknown",
68+
null,
69+
[
70+
$parameterName => $validatorIdentifier,
71+
]
72+
);
73+
}
74+
75+
/**
76+
* @param \Ibexa\Core\FieldType\TextLine\Value $value
77+
*/
78+
public function toHash(FieldTypeValueInterface $value): ?string
79+
{
80+
if ($this->isEmptyValue($value)) {
81+
return null;
82+
}
83+
84+
return $value->text;
85+
}
86+
}

src/lib/FieldType/TextBlock/Type.php

Lines changed: 25 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,18 @@
77

88
namespace Ibexa\Core\FieldType\TextBlock;
99

10-
use Ibexa\Contracts\Core\FieldType\Value as SPIValue;
11-
use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition;
1210
use Ibexa\Core\Base\Exceptions\InvalidArgumentType;
13-
use Ibexa\Core\FieldType\FieldType;
11+
use Ibexa\Core\FieldType\BaseTextType;
1412
use Ibexa\Core\FieldType\ValidationError;
1513
use Ibexa\Core\FieldType\Value as BaseValue;
1614
use JMS\TranslationBundle\Model\Message;
17-
use JMS\TranslationBundle\Translation\TranslationContainerInterface;
1815

1916
/**
2017
* The TextBlock field type.
2118
*
2219
* Represents a larger body of text, such as text areas.
2320
*/
24-
class Type extends FieldType implements TranslationContainerInterface
21+
class Type extends BaseTextType
2522
{
2623
protected $settingsSchema = [
2724
'textRows' => [
@@ -32,50 +29,17 @@ class Type extends FieldType implements TranslationContainerInterface
3229

3330
protected $validatorConfigurationSchema = [];
3431

35-
/**
36-
* Returns the field type identifier for this field type.
37-
*
38-
* @return string
39-
*/
4032
public function getFieldTypeIdentifier()
4133
{
4234
return 'eztext';
4335
}
4436

45-
/**
46-
* @param \Ibexa\Core\FieldType\TextBlock\Value|\Ibexa\Contracts\Core\FieldType\Value $value
47-
*/
48-
public function getName(SPIValue $value, FieldDefinition $fieldDefinition, string $languageCode): string
49-
{
50-
return (string)$value->text;
51-
}
52-
53-
/**
54-
* Returns the fallback default value of field type when no such default
55-
* value is provided in the field definition in content types.
56-
*
57-
* @return \Ibexa\Core\FieldType\TextBlock\Value
58-
*/
59-
public function getEmptyValue()
37+
public function getEmptyValue(): Value
6038
{
6139
return new Value();
6240
}
6341

6442
/**
65-
* Returns if the given $value is considered empty by the field type.
66-
*
67-
* @param mixed $value
68-
*
69-
* @return bool
70-
*/
71-
public function isEmptyValue(SPIValue $value)
72-
{
73-
return $value->text === null || trim($value->text) === '';
74-
}
75-
76-
/**
77-
* Inspects given $inputValue and potentially converts it into a dedicated value object.
78-
*
7943
* @param string|\Ibexa\Core\FieldType\TextBlock\Value $inputValue
8044
*
8145
* @return \Ibexa\Core\FieldType\TextBlock\Value The potentially converted and structurally plausible value.
@@ -89,21 +53,10 @@ protected function createValueFromInput($inputValue)
8953
return $inputValue;
9054
}
9155

92-
/**
93-
* Throws an exception if value structure is not of expected format.
94-
*
95-
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException If the value does not match the expected structure.
96-
*
97-
* @param \Ibexa\Core\FieldType\TextBlock\Value $value
98-
*/
99-
protected function checkValueStructure(BaseValue $value)
56+
protected static function checkValueType(mixed $value): void
10057
{
101-
if (!is_string($value->text)) {
102-
throw new InvalidArgumentType(
103-
'$value->text',
104-
'string',
105-
$value->text
106-
);
58+
if (!$value instanceof Value) {
59+
throw new InvalidArgumentType('$value', Value::class, $value);
10760
}
10861
}
10962

@@ -112,24 +65,21 @@ protected function checkValueStructure(BaseValue $value)
11265
*
11366
* @param \Ibexa\Core\FieldType\TextBlock\Value $value
11467
*
115-
* @return string
68+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
11669
*/
117-
protected function getSortInfo(BaseValue $value)
70+
protected function getSortInfo(BaseValue $value): string
11871
{
119-
return $this->transformationProcessor->transformByGroup(
120-
mb_substr(strtok(trim($value->text), "\r\n"), 0, 255),
121-
'lowercase'
122-
);
72+
$tokens = strtok(trim($value->text), "\r\n");
73+
74+
return $tokens !== false
75+
? $this->transformationProcessor->transformByGroup(mb_substr($tokens, 0, 255), 'lowercase')
76+
: '';
12377
}
12478

12579
/**
126-
* Converts an $hash to the Value defined by the field type.
127-
*
128-
* @param mixed $hash
129-
*
130-
* @return \Ibexa\Core\FieldType\TextBlock\Value $value
80+
* @param string $hash
13181
*/
132-
public function fromHash($hash)
82+
public function fromHash($hash): Value
13383
{
13484
if ($hash === null) {
13585
return $this->getEmptyValue();
@@ -138,58 +88,28 @@ public function fromHash($hash)
13888
return new Value($hash);
13989
}
14090

141-
/**
142-
* Converts a $Value to a hash.
143-
*
144-
* @param \Ibexa\Core\FieldType\TextBlock\Value $value
145-
*
146-
* @return mixed
147-
*/
148-
public function toHash(SPIValue $value)
149-
{
150-
if ($this->isEmptyValue($value)) {
151-
return null;
152-
}
153-
154-
return $value->text;
155-
}
156-
157-
/**
158-
* Returns whether the field type is searchable.
159-
*
160-
* @return bool
161-
*/
162-
public function isSearchable()
163-
{
164-
return true;
165-
}
166-
16791
/**
16892
* Validates the fieldSettings of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
16993
*
17094
* @param mixed $fieldSettings
17195
*
17296
* @return \Ibexa\Contracts\Core\FieldType\ValidationError[]
17397
*/
174-
public function validateFieldSettings($fieldSettings)
98+
public function validateFieldSettings($fieldSettings): array
17599
{
176100
$validationErrors = [];
177101

178102
foreach ($fieldSettings as $name => $value) {
179103
if (isset($this->settingsSchema[$name])) {
180-
switch ($name) {
181-
case 'textRows':
182-
if (!is_int($value)) {
183-
$validationErrors[] = new ValidationError(
184-
"Setting '%setting%' value must be of integer type",
185-
null,
186-
[
187-
'%setting%' => $name,
188-
],
189-
"[$name]"
190-
);
191-
}
192-
break;
104+
if ($name === 'textRows' && !is_int($value)) {
105+
$validationErrors[] = new ValidationError(
106+
"Setting '%setting%' value must be of integer type",
107+
null,
108+
[
109+
'%setting%' => $name,
110+
],
111+
"[$name]"
112+
);
193113
}
194114
} else {
195115
$validationErrors[] = new ValidationError(

0 commit comments

Comments
 (0)