ILIAS  release_8 Revision v8.24
Link.php
Go to the documentation of this file.
1<?php
2
18declare(strict_types=1);
19
21
23use ILIAS\Data\Factory as DataFactory;
26use ilLanguage;
27
31class Link extends Group implements C\Input\Field\Link
32{
33 public function __construct(
34 DataFactory $data_factory,
35 \ILIAS\Refinery\Factory $refinery,
37 Factory $field_factory,
38 string $label,
39 ?string $byline
40 ) {
41 $inputs = [
42 $field_factory->text($lng->txt('ui_link_label'), null)->withDedicatedName('label'),
43 $field_factory->url($lng->txt('ui_link_url'), null)->withDedicatedName('url')
44 ];
45
46 parent::__construct($data_factory, $refinery, $lng, $inputs, $label, $byline);
47 $this->addValidation();
48 $this->addTransformation();
49 }
50
51 protected function addValidation(): void
52 {
53 $txt_id = 'label_cannot_be_empty_if_url_is_set';
54 $error = fn (callable $txt, $value) => $txt($txt_id, $value);
55 $is_ok = function ($v) {
56 list($label, $url) = $v;
57 return (
58 (is_null($label) || $label === '') &&
59 is_null($url)
60 ) || (
61 !is_null($label) && !is_null($url)
62 && strlen($label) > 0
63 && is_a($url, URI::class)
64 );
65 };
66
67 $label_is_set_for_url = $this->refinery->custom()->constraint($is_ok, $error);
68 $this->setAdditionalTransformation($label_is_set_for_url);
69 }
70
71
72 protected function addTransformation(): void
73 {
74 $trafo = $this->refinery->custom()->transformation(function ($v): ?\ILIAS\Data\Link {
75 list($label, $url) = $v;
76 if (is_null($url) || $url === "") {
77 return null;
78 }
79 return $this->data_factory->link($label ?? "", $url);
80 });
81
82 $this->setAdditionalTransformation($trafo);
83 }
84
88 protected function isClientSideValueOk($value): bool
89 {
90 return true;
91 }
92
97 {
98 if ($this->requirement_constraint !== null) {
99 return $this->requirement_constraint;
100 }
101
102 return null;
103 }
104}
Factory for Date Formats.
Definition: Factory.php:27
Builds data types.
Definition: Factory.php:21
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:43
ilErrorHandling $error
Definition: class.ilias.php:55
language handling
$txt
Definition: error.php:13
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
Refinery Factory $refinery
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
Class ChatMainBarProvider \MainMenu\Provider.
$lng