ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
SwitchableGroup.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2017 Timon Amstutz <timon.amstutz@ilub.unibe.ch> Extended GPL, see
4 docs/LICENSE */
5 
7 
9 use ILIAS\UI\Component as C;
16 
21 {
23  use Triggerer;
24 
35  public function __construct(
36  DataFactory $data_factory,
37  \ILIAS\Refinery\Factory $refinery,
39  array $inputs,
40  string $label,
41  string $byline = null
42  ) {
43  $this->checkArgListElements("inputs", $inputs, Group::class);
44  parent::__construct($data_factory, $refinery, $lng, $inputs, $label, $byline);
45  }
46 
50  protected function getConstraintForRequirement()
51  {
52  return null;
53  }
54 
58  protected function isClientSideValueOk($value) : bool
59  {
60  if (!is_string($value) && !is_int($value)) {
61  return false;
62  }
63  return array_key_exists($value, $this->inputs);
64  }
65 
66  public function withRequired($is_required)
67  {
68  return Input::withRequired($is_required);
69  }
70 
74  public function withValue($value)
75  {
76  if (is_string($value) || is_int($value)) {
77  return Input::withValue($value);
78  }
79  if (!is_array($value) || count($value) !== 2) {
80  throw new \InvalidArgumentException(
81  "Expected one key and a group value or one key only as value."
82  );
83  }
84  list($key, $group_value) = $value;
85  $clone = Input::withValue($key);
86  $clone->inputs[$key] = $clone->inputs[$key]->withValue($group_value);
87  return $clone;
88  }
89 
93  public function getValue()
94  {
95  $key = Input::getValue();
96  if (is_null($key)) {
97  return null;
98  }
99  return [$key, $this->inputs[$key]->getValue()];
100  }
101 
102 
106  public function withInput(InputData $post_input)
107  {
108  if ($this->getName() === null) {
109  throw new \LogicException("Can only collect if input has a name.");
110  }
111 
112  $key = null;
113  if (!$this->isDisabled()) {
114  $key = $post_input->getOr($this->getName(), $key);
115  if (null !== $key) {
116  $clone = $this->withValue($key);
117  $clone->inputs[$key] = $clone->inputs[$key]->withInput($post_input);
118  } else {
119  $clone = $this;
120  }
121  } else {
122  $clone = $this;
123  }
124 
125  if ($key === null || $clone->inputs[$key]->getContent()->isError()) {
126  $clone->content = $clone->data_factory->error($this->lng->txt("ui_error_in_group"));
127  } else {
128  $clone->content = $this->applyOperationsTo($clone->getValue());
129  if ($clone->content->isError()) {
130  return $clone->withError("" . $clone->content->error());
131  }
132  }
133 
134  return $clone;
135  }
136 }
isDisabled()
Is this input disabled?
This describes a group of inputs.
Definition: Group.php:11
getValue()
Get the value that is displayed in the input client side.mixed
Class ChatMainBarProvider .
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
getOr($name, $default)
Get a named value from the data and fallback to default if that name does not exist.
__construct(DataFactory $data_factory, \ILIAS\Refinery\Factory $refinery, \ilLanguage $lng, array $inputs, string $label, string $byline=null)
Only adds a check to the original group-constructor.
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:12
$lng
This describes switchable group inputs.
Builds data types.
Definition: Factory.php:19
withValue($value)
Get an input like this with another value displayed on the client side.
withValue($value)
Get an input like this with another value displayed on the client side.
__construct(Container $dic, ilPlugin $plugin)
language handling
withRequired($is_required)
Get an input like this, but set the field to be required (or not).
checkArgListElements($which, array &$values, $classes)
Check every element of the list if it is an instance of one of the given classes. ...
withRequired($is_required)
Get an input like this, but set the field to be required (or not).
getValue()
Get the value that is displayed in the input client side.