From 2f03dccebba1c8f687f65cd538f795a1dc43339c Mon Sep 17 00:00:00 2001 From: philipp lang Date: Fri, 12 Jul 2024 00:16:50 +0200 Subject: [PATCH] Add data for conditions --- app/Form/Editor/FormConditionResolver.php | 17 +++---- app/Form/Fields/Field.php | 3 +- app/Form/Mails/ConfirmRegistrationMail.php | 6 +-- app/Form/Matchers/Matcher.php | 4 +- app/Form/Matchers/SingleValueMatcher.php | 12 +++-- app/Lib/Editor/Comparator.php | 12 +++++ app/Lib/Editor/Condition.php | 56 ++++++++++++++++++++++ app/Lib/Editor/ConditionMode.php | 9 ++++ app/Lib/Editor/ConditionResolver.php | 10 +--- app/Lib/Editor/Statement.php | 13 +++++ 10 files changed, 115 insertions(+), 27 deletions(-) create mode 100644 app/Lib/Editor/Comparator.php create mode 100644 app/Lib/Editor/Condition.php create mode 100644 app/Lib/Editor/ConditionMode.php create mode 100644 app/Lib/Editor/Statement.php diff --git a/app/Form/Editor/FormConditionResolver.php b/app/Form/Editor/FormConditionResolver.php index 7178b557..c6a71b4d 100644 --- a/app/Form/Editor/FormConditionResolver.php +++ b/app/Form/Editor/FormConditionResolver.php @@ -3,6 +3,7 @@ namespace App\Form\Editor; use App\Form\Models\Participant; +use App\Lib\Editor\Condition; use App\Lib\Editor\ConditionResolver; class FormConditionResolver extends ConditionResolver @@ -20,24 +21,24 @@ class FormConditionResolver extends ConditionResolver /** * @inheritdoc */ - public function filterCondition(string $mode, array $ifs): bool + public function filterCondition(Condition $condition): bool { - if (count($ifs) === 0) { + if (!$condition->hasStatements()) { return true; } - foreach ($ifs as $if) { - $field = $this->participant->getFields()->findByKey($if['field']); - $matches = $field->matches($if['comparator'], $if['value']); - if ($matches && $mode === 'any') { + foreach ($condition->ifs as $if) { + $field = $this->participant->getFields()->findByKey($if->field); + $matches = $field->matches($if->comparator, $if->value); + if ($matches && $condition->isAny()) { return true; } - if (!$matches && $mode === 'all') { + if (!$matches && $condition->isAll()) { return false; } } - if ($mode === 'any') { + if ($condition->isAny()) { return false; } diff --git a/app/Form/Fields/Field.php b/app/Form/Fields/Field.php index 82f23f06..043d1c07 100644 --- a/app/Form/Fields/Field.php +++ b/app/Form/Fields/Field.php @@ -11,6 +11,7 @@ use App\Form\Models\Form; use App\Form\Models\Participant; use App\Form\Presenters\DefaultPresenter; use App\Form\Presenters\Presenter; +use App\Lib\Editor\Comparator; use Faker\Generator; use Spatie\LaravelData\Data; use Spatie\LaravelData\Attributes\MapInputName; @@ -170,7 +171,7 @@ abstract class Field extends Data return $this->key . '_display'; } - public function matches(string $comparator, mixed $value): bool + public function matches(Comparator $comparator, mixed $value): bool { return $this->getMatcher()->setValue($this->value)->matches($comparator, $value); } diff --git a/app/Form/Mails/ConfirmRegistrationMail.php b/app/Form/Mails/ConfirmRegistrationMail.php index 49f89799..134fd8ab 100644 --- a/app/Form/Mails/ConfirmRegistrationMail.php +++ b/app/Form/Mails/ConfirmRegistrationMail.php @@ -5,6 +5,7 @@ namespace App\Form\Mails; use App\Form\Data\FormConfigData; use App\Form\Editor\FormConditionResolver; use App\Form\Models\Participant; +use App\Lib\Editor\Condition; use Illuminate\Bus\Queueable; use Illuminate\Mail\Attachment; use Illuminate\Mail\Mailable; @@ -71,10 +72,7 @@ class ConfirmRegistrationMail extends Mailable $conditionResolver = app(FormConditionResolver::class)->forParticipant($this->participant); return $this->participant->form->getMedia('mailattachments') - ->filter(fn ($media) => $conditionResolver->filterCondition( - data_get($media->getCustomProperty('conditions'), 'mode', 'all'), - data_get($media->getCustomProperty('conditions'), 'ifs', []), - )) + ->filter(fn ($media) => $conditionResolver->filterCondition(Condition::fromMedia($media))) ->map(fn ($media) => Attachment::fromStorageDisk($media->disk, $media->getPathRelativeToRoot())) ->all(); } diff --git a/app/Form/Matchers/Matcher.php b/app/Form/Matchers/Matcher.php index 4f093971..b9ebeefc 100644 --- a/app/Form/Matchers/Matcher.php +++ b/app/Form/Matchers/Matcher.php @@ -2,6 +2,8 @@ namespace App\Form\Matchers; +use App\Lib\Editor\Comparator; + abstract class Matcher { @@ -14,5 +16,5 @@ abstract class Matcher return $this; } - abstract public function matches(string $comparator, mixed $value): bool; + abstract public function matches(Comparator $comparator, mixed $value): bool; } diff --git a/app/Form/Matchers/SingleValueMatcher.php b/app/Form/Matchers/SingleValueMatcher.php index 80e98ad7..a7a64f2d 100644 --- a/app/Form/Matchers/SingleValueMatcher.php +++ b/app/Form/Matchers/SingleValueMatcher.php @@ -2,24 +2,26 @@ namespace App\Form\Matchers; +use App\Lib\Editor\Comparator; + class SingleValueMatcher extends Matcher { - public function matches(string $comparator, mixed $value): bool + public function matches(Comparator $comparator, mixed $value): bool { - if ($comparator === 'isEqual' && $value === $this->value) { + if ($comparator === Comparator::EQUAL && $value === $this->value) { return true; } - if ($comparator === 'isNotEqual' && $value !== $this->value) { + if ($comparator === Comparator::NOTEQUAL && $value !== $this->value) { return true; } - if ($comparator === 'isIn' && in_array($this->value, $value)) { + if ($comparator === Comparator::IN && in_array($this->value, $value)) { return true; } - if ($comparator === 'isNotIn' && !in_array($this->value, $value)) { + if ($comparator === Comparator::NOTIN && !in_array($this->value, $value)) { return true; } diff --git a/app/Lib/Editor/Comparator.php b/app/Lib/Editor/Comparator.php new file mode 100644 index 00000000..f6b288a8 --- /dev/null +++ b/app/Lib/Editor/Comparator.php @@ -0,0 +1,12 @@ + $ifs */ + public function __construct( + public ConditionMode $mode, + #[DataCollectionOf(Statement::class)] + public DataCollection $ifs, + ) { + } + + public static function fromMedia(Media $media): self + { + return static::withoutMagicalCreationFrom($media->getCustomProperty('conditions') ?: [ + 'mode' => 'any', + 'ifs' => [], + ]); + } + + /** + * @param array $block + */ + public static function fromBlock(array $block): self + { + + return static::withoutMagicalCreationFrom([ + 'mode' => data_get($block, 'tunes.condition.mode', 'any'), + 'ifs' => data_get($block, 'tunes.condition.ifs', []), + ]); + } + + public function hasStatements(): bool + { + return count($this->ifs) > 0; + } + + public function isAny(): bool + { + return $this->mode === ConditionMode::ANY; + } + + public function isAll(): bool + { + return $this->mode === ConditionMode::ALL; + } +} diff --git a/app/Lib/Editor/ConditionMode.php b/app/Lib/Editor/ConditionMode.php new file mode 100644 index 00000000..7c801aa5 --- /dev/null +++ b/app/Lib/Editor/ConditionMode.php @@ -0,0 +1,9 @@ + $ifs - */ - abstract public function filterCondition(string $mode, array $ifs): bool; + abstract public function filterCondition(Condition $condition): bool; /** * @param array $content @@ -24,9 +21,6 @@ abstract class ConditionResolver */ public function filterBlock(array $block): bool { - $mode = data_get($block, 'tunes.condition.mode', 'any'); - $ifs = data_get($block, 'tunes.condition.ifs', []); - - return $this->filterCondition($mode, $ifs); + return $this->filterCondition(Condition::fromBlock($block)); } } diff --git a/app/Lib/Editor/Statement.php b/app/Lib/Editor/Statement.php new file mode 100644 index 00000000..aba91d25 --- /dev/null +++ b/app/Lib/Editor/Statement.php @@ -0,0 +1,13 @@ +