Skip to content

Commit

Permalink
Merge branch 'release/1.1.0' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
cristoforocervino committed Apr 29, 2021
2 parents 5219fc4 + ca2e449 commit b3e8d3f
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 150 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,18 @@ $builder->add('period', PeriodType::class, [
]);
```

#### allow_null

**type**: `bool` **default**: `true`
Additional options to be used for the *boundaryType* form child.

```php
$builder->add('period', PeriodType::class, [
'allow_null' => false,
// Allow to trigger an error when your Period property is not nullable.
]);
```

## Configuration (completely optional)

This bundle is build thinking how to save you time and follow best practices as close as possible.
Expand Down
55 changes: 54 additions & 1 deletion src/Form/DataMapper/PeriodDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use League\Period\Exception;
use League\Period\Period;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\FormInterface;

Expand All @@ -18,6 +19,7 @@ class PeriodDataMapper implements DataMapperInterface
private string $startDateChildName;
private string $endDateChildName;
private string $boundaryTypeChildName;
private bool $allowNull;

private const BOUNDARY_TYPES = [
Period::INCLUDE_START_EXCLUDE_END,
Expand All @@ -30,13 +32,15 @@ public function __construct(
string $defaultBoundaryType = Period::INCLUDE_START_EXCLUDE_END,
string $startDateChildName = 'startDate',
string $endDateChildName = 'endDate',
string $boundaryTypeChildName = 'boundaryType'
string $boundaryTypeChildName = 'boundaryType',
bool $allowNull = true
) {
$this->assertValidBoundaryType($defaultBoundaryType);
$this->defaultBoundaryType = $defaultBoundaryType;
$this->startDateChildName = $startDateChildName;
$this->endDateChildName = $endDateChildName;
$this->boundaryTypeChildName = $boundaryTypeChildName;
$this->allowNull = $allowNull;
}

private function assertValidBoundaryType(string $boundaryType): void
Expand Down Expand Up @@ -95,11 +99,60 @@ public function mapFormsToData(iterable $forms, &$viewData): void

$viewData = null;

if (! $startDate instanceof \DateTimeInterface && $endDate instanceof \DateTimeInterface) {
$failure = new TransformationFailedException(\sprintf(
'Start date should be a %s',
\DateTimeInterface::class
));
$failure->setInvalidMessage(
'Start date should be valid. {{ startDate }} is not a valid date.',
['{{ startDate }}' => json_encode($startDate)]
);
throw $failure;
}

if (! $endDate instanceof \DateTimeInterface && $startDate instanceof \DateTimeInterface) {
$failure = new TransformationFailedException(\sprintf(
'End date should be a %s',
\DateTimeInterface::class
));
$failure->setInvalidMessage(
'End date should be valid. {{ endDate }} is not a valid date.',
['{{ endDate }}' => json_encode($endDate)]
);
throw $failure;
}

if ($startDate instanceof \DateTimeInterface && $endDate instanceof \DateTimeInterface) {
if ($startDate > $endDate) {
$failure = new TransformationFailedException('Start date should be greater or equals then the end date.');
$failure->setInvalidMessage('Start date should be greater or equals then the end date', [
'{{ startDate }}' => json_encode($startDate),
'{{ endDate }}' => json_encode($endDate),
]);
throw $failure;
}

try {
$viewData = Period::fromDatepoint($startDate, $endDate, $boundaryType);
} catch (Exception $e) {
$failure = new TransformationFailedException('Invalid Period', 0, $e);
$failure->setInvalidMessage('Invalid Period.', [
'{{ startDate }}' => json_encode($startDate),
'{{ endDate }}' => json_encode($endDate),
]);

throw $failure;
}
}

if (!$this->allowNull && $viewData === null) {
$failure = new TransformationFailedException('A valid Period is required');
$failure->setInvalidMessage('A valid Period is required.', [
'{{ startDate }}' => json_encode($startDate),
'{{ endDate }}' => json_encode($endDate),
]);
throw $failure;
}
}
}
18 changes: 7 additions & 11 deletions src/Form/PeriodType.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,17 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add($options['start_date_child_name'], $options['start_date_form_type'], \array_merge_recursive(
[
'label' => 'Start date',
'label' => 'Start',
'input' => 'datetime_immutable',
'property_path' => 'startDate',
],
$options['start_date_options']
));
$builder->add($options['end_date_child_name'], $options['end_date_form_type'], \array_merge_recursive(
[
'label' => 'End date',
'label' => 'End',
'input' => 'datetime_immutable',
'property_path' => 'endDate',
'constraints' => [
new GreaterThanOrEqualFormChildren([
'child' => $options['end_date_child_name'],
'gteChild' => $options['start_date_child_name'],
'useParent' => true,
]),
],
'property_path' => 'endDate'
],
$options['end_date_options']
));
Expand All @@ -56,7 +49,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$options['default_boundary_type'],
$options['start_date_child_name'],
$options['end_date_child_name'],
$options['boundary_type_child_name']
$options['boundary_type_child_name'],
$options['allow_null']
)
);
}
Expand Down Expand Up @@ -97,6 +91,7 @@ public function configureOptions(OptionsResolver $resolver): void
'end_date_options' => [],
'boundary_type_child_name' => 'boundary',
'boundary_type_options' => [],
'allow_null' => true
]);

$resolver->setAllowedValues('default_boundary_type', [
Expand All @@ -113,5 +108,6 @@ public function configureOptions(OptionsResolver $resolver): void
$resolver->setAllowedTypes('start_date_child_name', 'string');
$resolver->setAllowedTypes('end_date_child_name', 'string');
$resolver->setAllowedTypes('boundary_type_child_name', 'string');
$resolver->setAllowedTypes('allow_null', 'bool');
}
}
37 changes: 0 additions & 37 deletions src/Form/Validator/GreaterThanOrEqualFormChildren.php

This file was deleted.

92 changes: 0 additions & 92 deletions src/Form/Validator/GreaterThanOrEqualFormChildrenValidator.php

This file was deleted.

28 changes: 24 additions & 4 deletions src/Resources/translations/AndantePeriodBundle.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,37 @@
<target>Exclude both start and end</target>
</trans-unit>
<trans-unit id="5">
<source>Start date</source>
<target>Start date</target>
<source>Start</source>
<target>Start</target>
</trans-unit>
<trans-unit id="6">
<source>End date</source>
<target>End date</target>
<source>End</source>
<target>End</target>
</trans-unit>
<trans-unit id="7">
<source>Boundary type</source>
<target>Boundary type</target>
</trans-unit>
<trans-unit id="8">
<source>Start date should be valid. {{ startDate }} is not a valid date.</source>
<target>La data di inizio dovrebbe essere valida. {{ startDate }} non è una data valida.</target>
</trans-unit>
<trans-unit id="9">
<source>End date should be valid. {{ endDate }} is not a valid date.</source>
<target>La data di fine dovrebbe essere valida. {{ endDate }} non è una data valida.</target>
</trans-unit>
<trans-unit id="10">
<source>Start date should be greater or equals then the end date.</source>
<target>La data di inizio dovrebbe essere maggiore o uguale della data di fine.</target>
</trans-unit>
<trans-unit id="11">
<source>Invalid Period.</source>
<target>Periodo non valido.</target>
</trans-unit>
<trans-unit id="12">
<source>A valid Period is required.</source>
<target>Un periodo è obbligatorio.</target>
</trans-unit>
</body>
</file>
</xliff>
28 changes: 24 additions & 4 deletions src/Resources/translations/AndantePeriodBundle.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,37 @@
<target>Escludi sia inizio che fine</target>
</trans-unit>
<trans-unit id="5">
<source>Start date</source>
<target>Data di inizio</target>
<source>Start</source>
<target>Inizio</target>
</trans-unit>
<trans-unit id="6">
<source>End date</source>
<target>Data di fine</target>
<source>End</source>
<target>Fine</target>
</trans-unit>
<trans-unit id="7">
<source>Boundary type</source>
<target>Estremità</target>
</trans-unit>
<trans-unit id="8">
<source>Start date should be valid. {{ startDate }} is not a valid date.</source>
<target>Start date should be valid. {{ startDate }} is not a valid date.</target>
</trans-unit>
<trans-unit id="9">
<source>End date should be valid. {{ endDate }} is not a valid date.</source>
<target>End date should be valid. {{ endDate }} is not a valid date.</target>
</trans-unit>
<trans-unit id="10">
<source>Start date should be greater or equals then the end date.</source>
<target>Start date should be greater or equals then the end date.</target>
</trans-unit>
<trans-unit id="11">
<source>Invalid Period.</source>
<target>Invalid Period.</target>
</trans-unit>
<trans-unit id="12">
<source>A valid Period is required.</source>
<target>A valid Period is required.</target>
</trans-unit>
</body>
</file>
</xliff>
Loading

0 comments on commit b3e8d3f

Please sign in to comment.