ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
OptionalGroup.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
29use LogicException;
30
34class OptionalGroup extends Group implements I\OptionalGroup
35{
37 use Triggerer;
38
39 protected bool $null_value_was_explicitly_set = false;
40
45 {
46 if ($this->requirement_constraint !== null) {
47 return $this->requirement_constraint;
48 }
49
50 return null;
51 }
52
53 public function withRequired($is_required, ?Constraint $requirement_constraint = null): self
54 {
56 return FormInput::withRequired($is_required, $requirement_constraint);
57 }
58
59 public function isRequired(): bool
60 {
61 return $this->is_required;
62 }
63
67 public function withValue($value): self
68 {
69 if ($value === null) {
70 $clone = Input::withValue($value);
71 $clone->null_value_was_explicitly_set = true;
72 return $clone;
73 }
74
75 $clone = parent::withValue($value);
76 $clone->null_value_was_explicitly_set = false;
77 return $clone;
78 }
79
83 public function getValue()
84 {
85 if ($this->null_value_was_explicitly_set) {
86 return null;
87 }
88 return parent::getValue();
89 }
90
91
95 public function withInput(InputData $input): self
96 {
97 if ($this->getName() === null) {
98 throw new LogicException("Can only collect if input has a name.");
99 }
100
101 if (!$this->isDisabled()) {
102 $value = $input->getOr($this->getName(), null);
103 if ($value === null) {
104 $clone = $this->withValue(null);
105 // Ugly hack to prevent shortcutting behaviour of applyOperationsTo
106 $temp = $clone->is_required;
107 $clone->is_required = true;
108 $clone->content = $clone->applyOperationsTo(null);
109 $clone->is_required = $temp;
110 return $clone;
111 }
112 }
113
114 $clone = parent::withInput($input);
115 // If disabled keep, else false, because the null case is already handled.
116 $clone->null_value_was_explicitly_set = $this->isDisabled() && $this->null_value_was_explicitly_set;
117 return $clone;
118 }
119}
This implements the group input.
Definition: Group.php:41
withInput(InputData $input)
Get an input like this with input from post data.static
withValue($value)
Get an input like this with another value displayed on the client side.static
withRequired($is_required, ?Constraint $requirement_constraint=null)
getValue()
Get the value that is displayed in the input client side.mixed
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Input.php:101
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
withRequired(bool $is_required, ?Constraint $requirement_constraint=null)
Get an input like this, but set the field to be required (or not).
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:30
getOr(string $name, $default)
Get a named value from the data and fallback to default if that name does not exist.
This describes commonalities between all inputs.
Definition: Input.php:47
getName()
The name of the input as used in HTML.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:61
withInput(InputData $input)
Collects the input, applies trafos and forwards the input to its children and returns a new input gro...
Definition: Group.php:79
getValue()
Get the value that is displayed in the input client side.
Definition: Group.php:49
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.