ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Renderer.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
29
31{
32 protected const string FALLBACK_IMG = 'copyrights\all_rights_reserved.svg';
33
34 protected Factory $factory;
35 protected IRSS $irss;
36
37 public function __construct(
39 IRSS $irss
40 ) {
41 $this->factory = $factory;
42 $this->irss = $irss;
43 }
44
48 public function toUIComponents(CopyrightDataInterface $copyright): array
49 {
50 $res = [];
51 $has_link = false;
52 if (!is_null($image = $this->buildIcon($copyright))) {
53 $res[] = $image;
54 }
55 if (!is_null($link = $this->buildLink($copyright))) {
56 $res[] = $link;
57 $has_link = true;
58 }
59 if ($copyright->fullName() && !$has_link) {
60 $res[] = $this->textInLegacy($copyright->fullName());
61 }
62 return $res;
63 }
64
65 public function toString(CopyrightDataInterface $copyright): string
66 {
67 $full_name = $copyright->fullName();
68 $link = $copyright->link();
69
70 $res = [];
71 if ($full_name !== '') {
72 $res[] = $full_name;
73 }
74 if ($link !== null) {
75 $res[] = (string) $link;
76 }
77
78 return implode(' ', $res);
79 }
80
81 protected function buildIcon(CopyrightDataInterface $copyright): ?Icon
82 {
83 if (!$copyright->hasImage()) {
84 if ($copyright->fallBackToDefaultImage()) {
85 return $this->buildFallBackIcon($copyright);
86 }
87 return null;
88 }
89 if ($copyright->isImageLink()) {
90 return $this->buildIconFromLink($copyright);
91 } else {
92 return $this->buildIconFromFile($copyright);
93 }
94 }
95
96 protected function buildIconFromLink(CopyrightDataInterface $copyright): Icon
97 {
98 return $this->customIcon(
99 (string) $copyright->imageLink(),
100 $copyright->altText()
101 );
102 }
103
104 protected function buildIconFromFile(CopyrightDataInterface $copyright): ?Icon
105 {
106 if ($from_irss = $this->getSourceFromIRSS($copyright->imageFile())) {
107 $src = $from_irss;
108 } else {
109 return null;
110 }
111
112 return $this->customIcon(
113 $src,
114 $copyright->altText()
115 );
116 }
117
118 protected function buildFallBackIcon(CopyrightDataInterface $copyright): ?Icon
119 {
120 return $this->customIcon(
121 $this->getFallBackSrc(),
122 $copyright->altText()
123 );
124 }
125
126 protected function getFallBackSrc(): string
127 {
128 return \ilUtil::getImagePath(self::FALLBACK_IMG);
129 }
130
131 protected function buildLink(CopyrightDataInterface $copyright): ?Link
132 {
133 if (!$copyright->link()) {
134 return null;
135 }
136 return $this->standardLink(
137 $copyright->fullName() !== '' ? $copyright->fullName() : (string) $copyright->link(),
138 (string) $copyright->link()
139 )->withAdditionalRelationshipToReferencedResource(Relationship::LICENSE);
140 }
141
142 protected function customIcon(string $src, string $alt): Icon
143 {
144 return $this->factory->symbol()->icon()->custom($src, $alt, Icon::MEDIUM);
145 }
146
147 protected function standardLink(string $label, string $action): Link
148 {
149 return $this->factory->link()->standard($label, $action);
150 }
151
152 protected function textInLegacy(string $text): Content
153 {
154 return $this->factory->legacy()->content($text);
155 }
156
157 protected function getSourceFromIRSS(string $string_id): string
158 {
159 if ($identifier = $this->irss->manage()->find($string_id)) {
160 return $this->irss->consume()->src($identifier)->getSrc();
161 }
162 return '';
163 }
164}
factory()
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
getSourceFromIRSS(string $string_id)
Definition: Renderer.php:157
buildIcon(CopyrightDataInterface $copyright)
Definition: Renderer.php:81
toString(CopyrightDataInterface $copyright)
Definition: Renderer.php:65
toUIComponents(CopyrightDataInterface $copyright)
Definition: Renderer.php:48
buildLink(CopyrightDataInterface $copyright)
Definition: Renderer.php:131
__construct(Factory $factory, IRSS $irss)
Definition: Renderer.php:37
buildIconFromLink(CopyrightDataInterface $copyright)
Definition: Renderer.php:96
buildIconFromFile(CopyrightDataInterface $copyright)
Definition: Renderer.php:104
customIcon(string $src, string $alt)
Definition: Renderer.php:142
buildFallBackIcon(CopyrightDataInterface $copyright)
Definition: Renderer.php:118
standardLink(string $label, string $action)
Definition: Renderer.php:147
This describes how an icon could be modified during construction of UI.
Definition: Icon.php:29
This is how the factory for UI elements looks.
Definition: Factory.php:38
$res
Definition: ltiservices.php:69