ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
OptionalGroup.php
Go to the documentation of this file.
1 <?php
2 
18 declare(strict_types=1);
19 
21 
27 use LogicException;
28 
32 class OptionalGroup extends Group implements I\OptionalGroup
33 {
35  use Triggerer;
36 
37  protected bool $null_value_was_explicitly_set = false;
38 
42  protected function getConstraintForRequirement(): ?Constraint
43  {
44  if ($this->requirement_constraint !== null) {
45  return $this->requirement_constraint;
46  }
47 
48  return null;
49  }
50 
54  protected function isClientSideValueOk($value): bool
55  {
56  if ($value === null) {
57  return true;
58  }
59  return parent::isClientSideValueOk($value);
60  }
61 
62  public function withRequired($is_required, ?Constraint $requirement_constraint = null): I\Input
63  {
65  return FormInput::withRequired($is_required, $requirement_constraint);
66  }
67 
68  public function isRequired(): bool
69  {
70  return $this->is_required;
71  }
72 
76  public function withValue($value): I\Input
77  {
78  if ($value === null) {
79  $clone = clone $this;
80  $clone->value = $value;
81  $clone->null_value_was_explicitly_set = true;
82  return $clone;
83  }
84 
85  $clone = parent::withValue($value);
86  $clone->null_value_was_explicitly_set = false;
87  return $clone;
88  }
89 
93  public function getValue()
94  {
95  if ($this->null_value_was_explicitly_set) {
96  return null;
97  }
98  return parent::getValue();
99  }
100 
101 
105  public function withInput(InputData $input): I\Input
106  {
107  if ($this->getName() === null) {
108  throw new LogicException("Can only collect if input has a name.");
109  }
110 
111  if (!$this->isDisabled()) {
112  $value = $input->getOr($this->getName(), null);
113  if ($value === null) {
114  $clone = $this->withValue(null);
115  // Ugly hack to prevent shortcutting behaviour of applyOperationsTo
116  $temp = $clone->is_required;
117  $clone->is_required = true;
118  $clone->content = $clone->applyOperationsTo(null);
119  $clone->is_required = $temp;
120  return $clone;
121  }
122  }
123 
124  $clone = parent::withInput($input);
125  // If disabled keep, else false, because the null case is already handled.
126  $clone->null_value_was_explicitly_set = $this->isDisabled() && $this->null_value_was_explicitly_set;
127  return $clone;
128  }
129 }
withValue($value)
Get an input like this with another value displayed on the client side.if value does not fit client s...
getOr(string $name, $default)
Get a named value from the data and fallback to default if that name does not exist.
This implements the group input.
Definition: Group.php:42
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getValue()
Get the value that is displayed in the input client side.
Definition: Group.php:47
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
A constraint encodes some resrtictions on values.
Definition: Constraint.php:31
withInput(InputData $input)
Collects the input, applies trafos and forwards the input to its children and returns a new input gro...
Definition: Group.php:75
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:29
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
getName()
The name of the input as used in HTML.
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: Group.php:59
withRequired(bool $is_required, ?Constraint $requirement_constraint=null)
Get an input like this, but set the field to be required (or not).
withInput(InputData $input)
Get an input like this with input from post data.static