ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Url.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use ILIAS\Data\Factory as DataFactory;
28use Closure;
29use Throwable;
30
34class 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}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Factory for Date Formats.
Definition: Factory.php:27
Builds data types.
Definition: Factory.php:36
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
This implements the URL input.
Definition: Url.php:35
getUpdateOnLoadCode()
Get update code.This method has to return JS code that calls il.UI.filter.onFieldUpdate(event,...
Definition: Url.php:127
__construct(DataFactory $data_factory, Factory $refinery, string $label, ?string $byline)
Definition: Url.php:39
ilErrorHandling $error
Definition: class.ilias.php:69
$txt
Definition: error.php:31
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
This describes inputs that can be used in forms.
Definition: FormInput.php:33
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.