ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
OptionalGroup.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28use LogicException;
29
33class OptionalGroup extends Group implements I\OptionalGroup
34{
36 use Triggerer;
37
38 protected bool $null_value_was_explicitly_set = false;
39
44 {
45 if ($this->requirement_constraint !== null) {
46 return $this->requirement_constraint;
47 }
48
49 return null;
50 }
51
55 protected function isClientSideValueOk($value): bool
56 {
57 if ($value === null) {
58 return true;
59 }
60 return parent::isClientSideValueOk($value);
61 }
62
63 public function withRequired($is_required, ?Constraint $requirement_constraint = null): self
64 {
66 return FormInput::withRequired($is_required, $requirement_constraint);
67 }
68
69 public function isRequired(): bool
70 {
71 return $this->is_required;
72 }
73
77 public function withValue($value): self
78 {
79 if ($value === null) {
80 $clone = clone $this;
81 $clone->value = $value;
82 $clone->null_value_was_explicitly_set = true;
83 return $clone;
84 }
85
86 $clone = parent::withValue($value);
87 $clone->null_value_was_explicitly_set = false;
88 return $clone;
89 }
90
94 public function getValue()
95 {
96 if ($this->null_value_was_explicitly_set) {
97 return null;
98 }
99 return parent::getValue();
100 }
101
102
106 public function withInput(InputData $input): self
107 {
108 if ($this->getName() === null) {
109 throw new LogicException("Can only collect if input has a name.");
110 }
111
112 if (!$this->isDisabled()) {
113 $value = $input->getOr($this->getName(), null);
114 if ($value === null) {
115 $clone = $this->withValue(null);
116 // Ugly hack to prevent shortcutting behaviour of applyOperationsTo
117 $temp = $clone->is_required;
118 $clone->is_required = true;
119 $clone->content = $clone->applyOperationsTo(null);
120 $clone->is_required = $temp;
121 return $clone;
122 }
123 }
124
125 $clone = parent::withInput($input);
126 // If disabled keep, else false, because the null case is already handled.
127 $clone->null_value_was_explicitly_set = $this->isDisabled() && $this->null_value_was_explicitly_set;
128 return $clone;
129 }
130}
This implements the group input.
Definition: Group.php:41
isClientSideValueOk($value)
ATTENTION:GroupInternals::_isClientSideValueOk()
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
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.
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:77
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.