Skip to content

Commit 75978a7

Browse files
committed
Merge branch '4.6'
2 parents 25804db + e8e73c7 commit 75978a7

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

src/lib/FieldType/Keyword/Type.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition;
1313
use Ibexa\Core\Base\Exceptions\InvalidArgumentType;
1414
use Ibexa\Core\FieldType\FieldType;
15+
use Ibexa\Core\FieldType\ValidationError;
1516
use Ibexa\Core\FieldType\Value as BaseValue;
1617
use JMS\TranslationBundle\Model\Message;
1718
use JMS\TranslationBundle\Translation\TranslationContainerInterface;
@@ -23,6 +24,8 @@
2324
*/
2425
class Type extends FieldType implements TranslationContainerInterface
2526
{
27+
public const MAX_KEYWORD_LENGTH = 255;
28+
2629
/**
2730
* Returns the field type identifier for this field type.
2831
*
@@ -88,6 +91,44 @@ protected function checkValueStructure(BaseValue $value)
8891
$value->values
8992
);
9093
}
94+
95+
foreach ($value->values as $keyword) {
96+
if (!is_string($keyword) || mb_strlen($keyword) > self::MAX_KEYWORD_LENGTH) {
97+
throw new InvalidArgumentType(
98+
'$value->values[]',
99+
'string up to ' . self::MAX_KEYWORD_LENGTH . ' characters',
100+
$keyword
101+
);
102+
}
103+
}
104+
}
105+
106+
/**
107+
* @param \Ibexa\Core\FieldType\Keyword\Value $fieldValue
108+
*/
109+
public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue): array
110+
{
111+
$validationErrors = [];
112+
113+
foreach ($fieldValue->values as $keyword) {
114+
if (!is_string($keyword)) {
115+
$validationErrors[] = new ValidationError(
116+
'Each keyword must be a string.',
117+
null,
118+
[],
119+
'values'
120+
);
121+
} elseif (mb_strlen($keyword) > self::MAX_KEYWORD_LENGTH) {
122+
$validationErrors[] = new ValidationError(
123+
'Keyword value must be less than or equal to ' . self::MAX_KEYWORD_LENGTH . ' characters.',
124+
null,
125+
[],
126+
'values'
127+
);
128+
}
129+
}
130+
131+
return $validationErrors;
91132
}
92133

93134
/**

tests/lib/FieldType/KeywordTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
1111
use Ibexa\Core\FieldType\Keyword\Type as KeywordType;
1212
use Ibexa\Core\FieldType\Keyword\Value as KeywordValue;
13+
use Ibexa\Core\FieldType\ValidationError;
1314

1415
/**
1516
* @group fieldType
@@ -118,4 +119,78 @@ public function provideDataForGetName(): array
118119
[new KeywordValue(['foo', 'bar']), 'foo, bar', [], 'en_GB'],
119120
];
120121
}
122+
123+
/**
124+
* @return iterable<string, array{0: array<string, mixed>, 1: \Ibexa\Core\FieldType\Keyword\Value}>
125+
*/
126+
public function provideValidDataForValidate(): iterable
127+
{
128+
yield 'multiple keywords' => [
129+
[],
130+
new KeywordValue(['foo', 'bar']),
131+
];
132+
133+
yield 'empty string keyword' => [
134+
[],
135+
new KeywordValue(['']),
136+
];
137+
138+
yield 'empty keyword list' => [
139+
[],
140+
new KeywordValue([]),
141+
];
142+
}
143+
144+
/**
145+
* @return iterable<string, array{
146+
* 0: array<string, mixed>,
147+
* 1: \Ibexa\Core\FieldType\Keyword\Value,
148+
* 2: array<\Ibexa\Contracts\Core\FieldType\ValidationError>
149+
* }>
150+
*/
151+
public function provideInvalidDataForValidate(): iterable
152+
{
153+
$maxLen = KeywordType::MAX_KEYWORD_LENGTH;
154+
155+
yield 'non-string keyword (int)' => [
156+
[],
157+
// @phpstan-ignore-next-line
158+
new KeywordValue(['valid', 123]),
159+
[
160+
new ValidationError(
161+
'Each keyword must be a string.',
162+
null,
163+
[],
164+
'values'
165+
),
166+
],
167+
];
168+
169+
yield 'non-string keyword (null)' => [
170+
[],
171+
// @phpstan-ignore-next-line
172+
new KeywordValue(['valid', null]),
173+
[
174+
new ValidationError(
175+
'Each keyword must be a string.',
176+
null,
177+
[],
178+
'values'
179+
),
180+
],
181+
];
182+
183+
yield 'too long keyword' => [
184+
[],
185+
new KeywordValue(['valid', str_repeat('a', $maxLen + 1)]),
186+
[
187+
new ValidationError(
188+
'Keyword value must be less than or equal to ' . $maxLen . ' characters.',
189+
null,
190+
[],
191+
'values'
192+
),
193+
],
194+
];
195+
}
121196
}

0 commit comments

Comments
 (0)