ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Link.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use ILIAS\Data\Factory as DataFactory;
28
32class Link extends Group implements C\Input\Field\Link
33{
34 public function __construct(
35 DataFactory $data_factory,
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
98 {
99 if ($this->requirement_constraint !== null) {
100 return $this->requirement_constraint;
101 }
102
103 return null;
104 }
105}
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 group input.
Definition: Group.php:41
ilErrorHandling $error
Definition: class.ilias.php:69
$txt
Definition: error.php:31
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
__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.
global $lng
Definition: privfeed.php:31