77
88namespace Ibexa \Core \FieldType \TextBlock ;
99
10- use Ibexa \Contracts \Core \FieldType \Value as SPIValue ;
11- use Ibexa \Contracts \Core \Repository \Values \ContentType \FieldDefinition ;
1210use Ibexa \Core \Base \Exceptions \InvalidArgumentType ;
13- use Ibexa \Core \FieldType \FieldType ;
11+ use Ibexa \Core \FieldType \BaseTextType ;
1412use Ibexa \Core \FieldType \ValidationError ;
1513use Ibexa \Core \FieldType \Value as BaseValue ;
1614use 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