Initial commit
This commit is contained in:
45
app/Service/Forms/FormLogicPropertyResolver.php
Normal file
45
app/Service/Forms/FormLogicPropertyResolver.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Forms;
|
||||
|
||||
use App\Service\Forms\FormLogicConditionChecker;
|
||||
|
||||
class FormLogicPropertyResolver
|
||||
{
|
||||
|
||||
private $property = [];
|
||||
private $formData = [];
|
||||
private $logic = false;
|
||||
|
||||
public function __construct(private array $prop, private array $values)
|
||||
{
|
||||
$this->property = $prop;
|
||||
$this->formData = $values;
|
||||
$this->logic = isset($this->property['logic']) ? $this->property['logic'] : false;
|
||||
}
|
||||
|
||||
public static function isRequired(array $property, array $values): bool
|
||||
{
|
||||
return (new self($property, $values))->shouldBeRequired();
|
||||
}
|
||||
|
||||
public function shouldBeRequired(): bool
|
||||
{
|
||||
if(!isset($this->property['required'])){
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$this->logic) {
|
||||
return $this->property['required'];
|
||||
}
|
||||
|
||||
$conditionsMet = FormLogicConditionChecker::conditionsMet($this->logic['conditions'], $this->formData);
|
||||
if ($conditionsMet && $this->property['required'] && count($this->logic['actions']) > 0 && in_array('make-it-optional', $this->logic['actions'])) {
|
||||
return false;
|
||||
} else if ($conditionsMet && !$this->property['required'] && count($this->logic['actions']) > 0 && in_array('require-answer', $this->logic['actions'])) {
|
||||
return true;
|
||||
} else {
|
||||
return $this->property['required'];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user