ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
4docs/LICENSE */
5
7
8use ILIAS\Data\Factory as DataFactory;
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}
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
withValue($value)
Get an input like this with another value displayed on the client side.InvalidArgumentException if va...
withRequired($is_required)
Get an input like this, but set the field to be required (or not).
__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.
getValue()
Get the value that is displayed in the input client side.mixed
language handling
A constraint encodes some resrtictions on values.
Definition: Constraint.php:15
A transformation is a function from one datatype to another.
This describes a group of inputs.
Definition: Group.php:12
getValue()
Get the value that is displayed in the input client side.
withValue($value)
Get an input like this with another value displayed on the client side.
isDisabled()
Is this input disabled?
withRequired($is_required)
Get an input like this, but set the field to be required (or not).
This describes switchable group inputs.
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:13
getOr($name, $default)
Get a named value from the data and fallback to default if that name does not exist.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
checkArgListElements($which, array &$values, $classes)
Check every element of the list if it is an instance of one of the given classes.
Class ChatMainBarProvider \MainMenu\Provider.
$lng