ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
BaseFactory.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
32 
33 abstract class BaseFactory
34 {
38 
39  public function __construct(
40  UIFactory $ui_factory,
41  PresenterInterface $presenter,
42  ConstraintDictionary $constraint_dictionary
43  ) {
44  $this->ui_factory = $ui_factory;
45  $this->presenter = $presenter;
46  $this->constraint_dictionary = $constraint_dictionary;
47  }
48 
49  abstract protected function rawInput(
50  ElementInterface $element,
51  ElementInterface $context_element,
52  string $condition_value = ''
53  ): FormInput;
54 
58  protected function dataValueForInput(
60  ): string|array {
61  return $data->value();
62  }
63 
64  final public function getInput(
65  ElementInterface $element,
66  ElementInterface $context_element,
67  ): FormInput {
68  $input = $this->rawInput(
69  $element,
70  $context_element
71  );
72 
73  return $this->finishInput($element, $context_element, $input);
74  }
75 
76  final public function getInputInCondition(
77  ElementInterface $element,
78  ElementInterface $context_element,
79  string $condition_value
80  ): FormInput {
81  $input = $this->rawInput(
82  $element,
83  $context_element,
84  $condition_value
85  );
86 
87  return $this->finishInputIgnoreValue($element, $context_element, $input);
88  }
89 
90  public function finishInput(
91  ElementInterface $element,
92  ElementInterface $context_element,
93  FormInput $input
94  ): FormInput {
95  if (($data = $element->getData())->type() !== Type::NULL) {
96  $input = $input->withValue(
97  $this->dataValueForInput($data)
98  );
99  }
100 
101  return $this->finishInputIgnoreValue($element, $context_element, $input);
102  }
103 
104  protected function finishInputIgnoreValue(
105  ElementInterface $element,
106  ElementInterface $context_element,
107  FormInput $input
108  ): FormInput {
109  $label = $this->presenter->elements()->nameWithParents(
110  $element,
111  $context_element,
112  false
113  );
114  $input = $input->withLabel($label);
115 
116  foreach ($this->constraint_dictionary->tagsForElement($element) as $tag) {
117  $input = $this->addConstraintFromTag($input, $tag);
118  }
119 
120  return $input;
121  }
122 
123  protected function addConstraintFromTag(
124  FormInput $input,
125  ConstraintTag $tag
126  ): FormInput {
127  switch ($tag->restriction()) {
128  case Restriction::PRESET_VALUE:
129  return $input->withValue($tag->value());
130 
131  case Restriction::NOT_DELETABLE:
132  return $input->withRequired(true);
133 
135  return $input->withDisabled(true);
136  }
137  return $input;
138  }
139 }
This is what a factory for input fields looks like.
Definition: Factory.php:28
finishInputIgnoreValue(ElementInterface $element, ElementInterface $context_element, FormInput $input)
getInputInCondition(ElementInterface $element, ElementInterface $context_element, string $condition_value)
Definition: BaseFactory.php:76
withDisabled(bool $is_disabled)
Get an input like this, but set it to a disabled state.
getInput(ElementInterface $element, ElementInterface $context_element,)
Definition: BaseFactory.php:64
withLabel(string $label)
Get an input like this, but with a replaced label.
withValue($value)
Get an input like this with another value displayed on the client side.
rawInput(ElementInterface $element, ElementInterface $context_element, string $condition_value='')
withRequired(bool $is_required, ?Constraint $requirement_constraint=null)
Get an input like this, but set the field to be required (or not).
This describes inputs that can be used in forms.
Definition: FormInput.php:31
__construct(UIFactory $ui_factory, PresenterInterface $presenter, ConstraintDictionary $constraint_dictionary)
Definition: BaseFactory.php:39
value()
Value of the data, in a format according to its type.
finishInput(ElementInterface $element, ElementInterface $context_element, FormInput $input)
Definition: BaseFactory.php:90