ILIAS  trunk Revision v11.0_alpha-1851-ga8564da6fed
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Link.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ILIAS\UI\Component as C;
25 use ILIAS\Data\URI;
28 
32 class Link extends Group implements C\Input\Field\Link
33 {
34  public function __construct(
35  DataFactory $data_factory,
37  Language $lng,
38  Factory $field_factory,
39  string $label,
40  ?string $byline
41  ) {
42  $inputs = [
43  $field_factory->text($lng->txt('ui_link_label'), null)->withDedicatedName('label'),
44  $field_factory->url($lng->txt('ui_link_url'), null)->withDedicatedName('url')
45  ];
46 
47  parent::__construct($data_factory, $refinery, $lng, $inputs, $label, $byline);
48  $this->addValidation();
49  $this->addTransformation();
50  }
51 
52  protected function addValidation(): void
53  {
54  $txt_id = 'label_cannot_be_empty_if_url_is_set';
55  $error = fn(callable $txt, $value) => $txt($txt_id, $value);
56  $is_ok = function ($v) {
57  list($label, $url) = $v;
58  return (
59  (is_null($label) || $label === '') &&
60  is_null($url)
61  ) || (
62  !is_null($label) && !is_null($url)
63  && strlen($label) > 0
64  && is_a($url, URI::class)
65  );
66  };
67 
68  $label_is_set_for_url = $this->refinery->custom()->constraint($is_ok, $error);
69  $this->setAdditionalTransformation($label_is_set_for_url);
70  }
71 
72 
73  protected function addTransformation(): void
74  {
75  $trafo = $this->refinery->custom()->transformation(function ($v): ?\ILIAS\Data\Link {
76  list($label, $url) = $v;
77  if (is_null($url) || $url === "") {
78  return null;
79  }
80  return $this->data_factory->link($label ?? "", $url);
81  });
82 
83  $this->setAdditionalTransformation($trafo);
84  }
85 
89  protected function isClientSideValueOk($value): bool
90  {
91  return true;
92  }
93 
97  protected function getConstraintForRequirement(): ?Constraint
98  {
99  if ($this->requirement_constraint !== null) {
101  }
102 
103  return null;
104  }
105 }
Interface Observer Contains several chained tasks and infos about them.
A constraint encodes some resrtictions on values.
Definition: Constraint.php:31
$url
Definition: shib_logout.php:66
Factory for Date Formats.
Definition: Factory.php:26
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
txt(string $a_topic, string $a_default_lang_fallback_mod="")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
withDedicatedName(string $dedicated_name)
Definition: Input.php:182
string $error
This is an error on the input as displayed client side.
Definition: Input.php:57
$txt
Definition: error.php:31
setAdditionalTransformation(Transformation $trafo)
Apply a transformation to the current or future content.
Definition: Input.php:158
__construct(Container $dic, ilPlugin $plugin)
global $lng
Definition: privfeed.php:31