Skip to content

Commit 2cb46e2

Browse files
committed
Added support for mask and examples in Format definitions
1 parent 34117b2 commit 2cb46e2

File tree

22 files changed

+281
-143
lines changed

22 files changed

+281
-143
lines changed

src/FieldType/FormattedTextLine/Format.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,20 @@
88

99
interface Format
1010
{
11+
/**
12+
* Validate value against format.
13+
*/
1114
public function validate(FieldDefinition $fieldDefinition, string $text): bool;
15+
16+
/**
17+
* Returns input mask for format.
18+
*/
19+
public function getMask(FieldDefinition $fieldDefinition): ?string;
20+
21+
/**
22+
* Returns examples of correct value.
23+
*
24+
* @return string[]
25+
*/
26+
public function getExamples(FieldDefinition $fieldDefinition): array;
1227
}

src/FieldType/FormattedTextLine/Format/NullFormat.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,14 @@ public function validate(FieldDefinition $fieldDefinition, string $text): bool
1313
{
1414
return true;
1515
}
16+
17+
public function getMask(FieldDefinition $fieldDefinition): ?string
18+
{
19+
return null;
20+
}
21+
22+
public function getExamples(FieldDefinition $fieldDefinition): array
23+
{
24+
return [];
25+
}
1626
}

src/FieldType/FormattedTextLine/Format/PatternFormat.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,31 @@ final class PatternFormat implements Format
1212
/** @var string */
1313
private $pattern;
1414

15-
public function __construct(string $pattern)
15+
/** @var string|null */
16+
private $mask;
17+
18+
/** @var string[] */
19+
private $examples;
20+
21+
public function __construct(string $pattern, ?string $mask = null, array $examples = [])
1622
{
1723
$this->pattern = $pattern;
24+
$this->mask = $mask;
25+
$this->examples = $examples;
1826
}
1927

2028
public function validate(FieldDefinition $fieldDefinition, string $text): bool
2129
{
2230
return preg_match($this->pattern, $text) === 1;
2331
}
32+
33+
public function getMask(FieldDefinition $fieldDefinition): ?string
34+
{
35+
return $this->mask;
36+
}
37+
38+
public function getExamples(FieldDefinition $fieldDefinition): array
39+
{
40+
return $this->examples;
41+
}
2442
}

src/FieldType/MaskedTextLine/Format.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,14 @@ public function createPatternFromMask(string $mask): string
5656

5757
return '/^' . implode('', $pattern) . '$/';
5858
}
59+
60+
public function getMask(FieldDefinition $fieldDefinition): ?string
61+
{
62+
return $fieldDefinition->fieldSettings['mask'] ?? null;
63+
}
64+
65+
public function getExamples(FieldDefinition $fieldDefinition): array
66+
{
67+
return [];
68+
}
5969
}

src/Form/DataTransformer/TextLineFieldTypeTransformer.php renamed to src/Form/DataTransformer/FormattedTextLineFieldTypeTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use AdamWojs\IbexaFormattedTextLineBundle\FieldType\FormattedTextLine\Value;
88
use Symfony\Component\Form\DataTransformerInterface;
99

10-
final class TextLineFieldTypeTransformer implements DataTransformerInterface
10+
final class FormattedTextLineFieldTypeTransformer implements DataTransformerInterface
1111
{
1212
public function transform($value): ?array
1313
{

src/Form/Mapper/FormattedTextLine/FieldValueFormMapper.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,35 @@
44

55
namespace AdamWojs\IbexaFormattedTextLineBundle\Form\Mapper\FormattedTextLine;
66

7-
use AdamWojs\IbexaFormattedTextLineBundle\Form\Type\TextLineFieldType;
7+
use AdamWojs\IbexaFormattedTextLineBundle\FieldType\FormattedTextLine\Format;
8+
use AdamWojs\IbexaFormattedTextLineBundle\Form\Type\FormattedTextLineFieldType;
89
use EzSystems\EzPlatformContentForms\Data\Content\FieldData;
910
use EzSystems\EzPlatformContentForms\FieldType\FieldValueFormMapperInterface;
1011
use Symfony\Component\Form\FormInterface;
1112

12-
final class FieldValueFormMapper implements FieldValueFormMapperInterface
13+
class FieldValueFormMapper implements FieldValueFormMapperInterface
1314
{
15+
/** @var \AdamWojs\IbexaFormattedTextLineBundle\FieldType\FormattedTextLine\Format */
16+
private $format;
17+
18+
public function __construct(Format $format)
19+
{
20+
$this->format = $format;
21+
}
22+
1423
public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data): void
1524
{
1625
$definition = $data->fieldDefinition;
1726

18-
$fieldForm->add('value', TextLineFieldType::class, [
19-
'required' => $definition->isRequired,
20-
'label' => $definition->getName(),
21-
]);
27+
$fieldForm->add(
28+
'value',
29+
FormattedTextLineFieldType::class,
30+
[
31+
'required' => $definition->isRequired,
32+
'label' => $definition->getName(),
33+
'mask' => $this->format->getMask($definition),
34+
'examples' => $this->format->getExamples($definition),
35+
]
36+
);
2237
}
2338
}

src/Form/Mapper/MaskedTextLine/FieldValueFormMapper.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,8 @@
44

55
namespace AdamWojs\IbexaFormattedTextLineBundle\Form\Mapper\MaskedTextLine;
66

7-
use AdamWojs\IbexaFormattedTextLineBundle\Form\Type\MaskedTextLineFieldType;
8-
use EzSystems\EzPlatformContentForms\Data\Content\FieldData;
9-
use EzSystems\EzPlatformContentForms\FieldType\FieldValueFormMapperInterface;
10-
use Symfony\Component\Form\FormInterface;
7+
use AdamWojs\IbexaFormattedTextLineBundle\Form\Mapper\FormattedTextLine\FieldValueFormMapper as BaseFieldValueFormMapper;
118

12-
final class FieldValueFormMapper implements FieldValueFormMapperInterface
9+
final class FieldValueFormMapper extends BaseFieldValueFormMapper
1310
{
14-
public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data): void
15-
{
16-
$definition = $data->fieldDefinition;
17-
18-
$fieldForm->add('value', MaskedTextLineFieldType::class, [
19-
'required' => $definition->isRequired,
20-
'label' => $definition->getName(),
21-
'mask' => $definition->fieldSettings['mask'],
22-
]);
23-
}
2411
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AdamWojs\IbexaFormattedTextLineBundle\Form\Type;
6+
7+
use AdamWojs\IbexaFormattedTextLineBundle\Form\DataTransformer\FormattedTextLineFieldTypeTransformer;
8+
use Symfony\Component\Form\AbstractType;
9+
use Symfony\Component\Form\FormBuilderInterface;
10+
use Symfony\Component\OptionsResolver\OptionsResolver;
11+
12+
final class FormattedTextLineFieldType extends AbstractType
13+
{
14+
public function buildForm(FormBuilderInterface $builder, array $options)
15+
{
16+
$help = null;
17+
if (!empty($options['examples'])) {
18+
$help = 'e.g. ' . implode(', ', $options['examples']);
19+
}
20+
21+
$builder->add('text', MaskedTextType::class, [
22+
'label' => false,
23+
'mask' => $options['mask'],
24+
'help' => $help,
25+
]);
26+
27+
$builder->addModelTransformer(new FormattedTextLineFieldTypeTransformer());
28+
}
29+
30+
public function configureOptions(OptionsResolver $resolver): void
31+
{
32+
$resolver->setDefaults([
33+
'mask' => null,
34+
'examples' => [],
35+
]);
36+
37+
$resolver->setAllowedTypes('mask', ['string', 'null']);
38+
$resolver->setAllowedTypes('examples', ['array']);
39+
}
40+
}

src/Form/Type/MaskedTextLineFieldType.php

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/Form/Type/MaskedTextType.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AdamWojs\IbexaFormattedTextLineBundle\Form\Type;
6+
7+
use Symfony\Component\Form\AbstractType;
8+
use Symfony\Component\Form\Extension\Core\Type\TextType;
9+
use Symfony\Component\Form\FormInterface;
10+
use Symfony\Component\Form\FormView;
11+
use Symfony\Component\OptionsResolver\OptionsResolver;
12+
13+
final class MaskedTextType extends AbstractType
14+
{
15+
public function buildView(FormView $view, FormInterface $form, array $options): void
16+
{
17+
$view->vars += [
18+
'mask' => $options['mask'],
19+
];
20+
}
21+
22+
public function configureOptions(OptionsResolver $resolver): void
23+
{
24+
$resolver->setDefaults([
25+
'mask' => null,
26+
]);
27+
28+
$resolver->setAllowedTypes('mask', ['string', 'null']);
29+
}
30+
31+
public function getParent(): string
32+
{
33+
return TextType::class;
34+
}
35+
}

0 commit comments

Comments
 (0)