wip: major refactoring
This commit is contained in:
1 parent
d719b17aec
commit
11825191cf
32 files changed
+692
-466
No files matched your search
@@ -4,11 +4,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Form\Service;
|
||||
|
||||
use App\Form\Model\BookingDtoInterface;
|
||||
use App\Form\ParticipantFieldHandler\Condition\ApplicantCondition;
|
||||
use App\Form\ParticipantFieldHandler\Condition\CompositeCondition;
|
||||
use App\Form\ParticipantFieldHandler\Condition\FieldConditionInterface;
|
||||
use App\Form\ParticipantFieldHandler\Condition\MutabilityCondition;
|
||||
use App\Form\Service\Condition\ApplicantCondition;
|
||||
use App\Form\Service\Condition\CompositeCondition;
|
||||
use App\Form\Service\Condition\MutabilityCondition;
|
||||
use App\Form\Service\Abstract\AbstractFieldStateProvider;
|
||||
|
||||
/**
|
||||
* Field state provider for the booking edit workflow.
|
||||
@@ -16,27 +15,19 @@ use App\Form\ParticipantFieldHandler\Condition\MutabilityCondition;
|
||||
* This service calculates dynamic field states (readonly, disabled, etc.)
|
||||
* for participant fields in the edit flow, using edit-specific conditions.
|
||||
*
|
||||
* It is designed to be extensible and composable, allowing reuse of
|
||||
* existing condition classes and easy registration of new logic.
|
||||
* It extends the common field state functionality provided by
|
||||
* AbstractFieldStateProvider and adds edit-specific field state logic.
|
||||
*/
|
||||
class EditFieldStateProvider implements FieldStateProviderInterface
|
||||
class EditFieldStateProvider extends AbstractFieldStateProvider
|
||||
{
|
||||
use FormTraversalTrait;
|
||||
|
||||
/** @var array<string, array<string, FieldConditionInterface>> */
|
||||
private array $fieldStateConditions = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->registerFieldStateConditions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers field state conditions for the edit workflow.
|
||||
*
|
||||
* Add or modify conditions as needed for your domain.
|
||||
* This method defines the conditional logic for field states in the
|
||||
* booking edit process. It makes personal data fields readonly when
|
||||
* the participant is the applicant or when the field is not mutable.
|
||||
*/
|
||||
private function registerFieldStateConditions(): void
|
||||
protected function registerFieldStateConditions(): void
|
||||
{
|
||||
// Make all personal data fields readonly if not mutable OR if applicant
|
||||
$personalDataFields = [
|
||||
@@ -56,75 +47,5 @@ class EditFieldStateProvider implements FieldStateProviderInterface
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
// Example: Only applicant can edit email
|
||||
$this->fieldStateConditions['email']['readonly'] = new ApplicantCondition();
|
||||
}
|
||||
|
||||
public function getFieldState(string $fieldName, BookingDtoInterface $bookingDto, int $participantIndex, array $formData = []): array
|
||||
{
|
||||
if (!isset($this->fieldStateConditions[$fieldName])) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$stateModifications = [];
|
||||
$attributes = [];
|
||||
|
||||
foreach ($this->fieldStateConditions[$fieldName] as $stateType => $condition) {
|
||||
if ($condition->evaluate($bookingDto, $participantIndex, $formData)) {
|
||||
switch ($stateType) {
|
||||
case 'readonly':
|
||||
$attributes['readonly'] = true;
|
||||
break;
|
||||
case 'disabled':
|
||||
$stateModifications['disabled'] = true;
|
||||
break;
|
||||
case 'required':
|
||||
$stateModifications['required'] = true;
|
||||
break;
|
||||
case 'hidden':
|
||||
$attributes['style'] = ($attributes['style'] ?? '').' display: none;';
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($attributes)) {
|
||||
$stateModifications['attr'] = $attributes;
|
||||
}
|
||||
|
||||
return $stateModifications;
|
||||
}
|
||||
|
||||
public function hasStateConditions(string $fieldName): bool
|
||||
{
|
||||
return isset($this->fieldStateConditions[$fieldName]) && !empty($this->fieldStateConditions[$fieldName]);
|
||||
}
|
||||
|
||||
public function getFieldStateDependencies(string $fieldName): array
|
||||
{
|
||||
if (!isset($this->fieldStateConditions[$fieldName])) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$dependencies = [];
|
||||
foreach ($this->fieldStateConditions[$fieldName] as $condition) {
|
||||
$dependencies = array_merge($dependencies, $condition->getDependentFields());
|
||||
}
|
||||
|
||||
return array_unique($dependencies);
|
||||
}
|
||||
|
||||
public function getAllFieldStates(BookingDtoInterface $bookingDto, int $participantIndex, array $formData = []): array
|
||||
{
|
||||
$allStates = [];
|
||||
foreach (array_keys($this->fieldStateConditions) as $fieldName) {
|
||||
$fieldState = $this->getFieldState($fieldName, $bookingDto, $participantIndex, $formData);
|
||||
if (!empty($fieldState)) {
|
||||
$allStates[$fieldName] = $fieldState;
|
||||
}
|
||||
}
|
||||
|
||||
return $allStates;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user