ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
Url.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 use Closure;
29 use Throwable;
30 
34 class Url extends FormInput implements C\Input\Field\Url
35 {
39  public function __construct(
40  DataFactory $data_factory,
42  string $label,
43  ?string $byline
44  ) {
45  parent::__construct($data_factory, $refinery, $label, $byline);
46  $this->addValidation();
47  $this->addTransformation();
48  }
49 
50  protected function addValidation(): void
51  {
52  $txt_id = 'ui_invalid_url';
53  $error = fn (callable $txt, $value) => $txt($txt_id, $value);
54  $is_ok = function ($v) {
55  if (is_string($v) && trim($v) === '') {
56  return true;
57  }
58  try {
59  $this->data_factory->uri($v);
60  } catch (Throwable $e) {
61  return false;
62  }
63  return true;
64  };
65 
66  $from_before_until = $this->refinery->custom()->constraint($is_ok, $error);
67  $this->setAdditionalTransformation($from_before_until);
68  }
69 
70  protected function addTransformation(): void
71  {
72  $trafo = $this->refinery->custom()->transformation(function ($v): ?\ILIAS\Data\URI {
73  if (is_string($v) && trim($v) === '') {
74  return null;
75  }
76  return $this->data_factory->uri($v);
77  });
78 
79  $this->setAdditionalTransformation($trafo);
80  }
81 
85  public static function getURIChecker(): Closure
86  {
87  return static function (string $value): bool {
88  try {
89  new URI($value);
90  } catch (Throwable $e) {
91  return false;
92  }
93  return true;
94  };
95  }
96 
100  protected function isClientSideValueOk($value): bool
101  {
102  if (is_string($value) && trim($value) === "") {
103  return true;
104  }
105 
106  if (!self::getURIChecker()) {
107  return false;
108  }
109  return true;
110  }
111 
116  {
117  if ($this->requirement_constraint !== null) {
118  return $this->requirement_constraint;
119  }
120 
121  return $this->refinery->custom()->constraint(self::getURIChecker(), 'Not an URI');
122  }
123 
127  public function getUpdateOnLoadCode(): Closure
128  {
129  return fn ($id) => "$('#$id').on('input', function(event) {
130  il.UI.input.onFieldUpdate(event, '$id', $('#$id').val());
131  });
132  il.UI.input.onFieldUpdate(event, '$id', $('#$id').val());";
133  }
134 }
Class ChatMainBarProvider .
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
A constraint encodes some resrtictions on values.
Definition: Constraint.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
__construct(DataFactory $data_factory, Factory $refinery, string $label, ?string $byline)
Definition: Url.php:39
The scope of this class is split ilias-conform URI&#39;s into components.
Definition: URI.php:34
$txt
Definition: error.php:13
This implements the URL input.
Definition: Url.php:34
__construct(Container $dic, ilPlugin $plugin)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
ilErrorHandling $error
Definition: class.ilias.php:55
This describes inputs that can be used in forms.
Definition: FormInput.php:31
getUpdateOnLoadCode()
Get update code.This method has to return JS code that calls il.UI.filter.onFieldUpdate(event, &#39;$id&#39;, string_value);initially "onload" andon every input change. It must pass a readable string representation of its value in parameter &#39;string_value&#39;.
Definition: Url.php:127
Refinery Factory $refinery