ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
Renderer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\MetaData\Copyright;
22 
29 
30 class Renderer implements RendererInterface
31 {
32  protected const FALLBACK_IMG = 'copyrights\all_rights_reserved.svg';
33 
34  protected Factory $factory;
35  protected IRSS $irss;
36 
37  public function __construct(
38  Factory $factory,
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  protected function buildIcon(CopyrightDataInterface $copyright): ?Icon
66  {
67  if (!$copyright->hasImage()) {
68  if ($copyright->fallBackToDefaultImage()) {
69  return $this->buildFallBackIcon($copyright);
70  }
71  return null;
72  }
73  if ($copyright->isImageLink()) {
74  return $this->buildIconFromLink($copyright);
75  } else {
76  return $this->buildIconFromFile($copyright);
77  }
78  }
79 
80  protected function buildIconFromLink(CopyrightDataInterface $copyright): Icon
81  {
82  return $this->customIcon(
83  (string) $copyright->imageLink(),
84  $copyright->altText()
85  );
86  }
87 
88  protected function buildIconFromFile(CopyrightDataInterface $copyright): ?Icon
89  {
90  if ($from_irss = $this->getSourceFromIRSS($copyright->imageFile())) {
91  $src = $from_irss;
92  } else {
93  return null;
94  }
95 
96  return $this->customIcon(
97  $src,
98  $copyright->altText()
99  );
100  }
101 
102  protected function buildFallBackIcon(CopyrightDataInterface $copyright): ?Icon
103  {
104  return $this->customIcon(
105  $this->getFallBackSrc(),
106  $copyright->altText()
107  );
108  }
109 
110  protected function getFallBackSrc(): string
111  {
112  return \ilUtil::getImagePath(self::FALLBACK_IMG);
113  }
114 
115  protected function buildLink(CopyrightDataInterface $copyright): ?Link
116  {
117  if (!$copyright->link()) {
118  return null;
119  }
120  return $this->standardLink(
121  $copyright->fullName() !== '' ? $copyright->fullName() : (string) $copyright->link(),
122  (string) $copyright->link()
123  )->withAdditionalRelationshipToReferencedResource(Relationship::LICENSE);
124  }
125 
126  protected function customIcon(string $src, string $alt): Icon
127  {
128  return $this->factory->symbol()->icon()->custom($src, $alt, Icon::MEDIUM);
129  }
130 
131  protected function standardLink(string $label, string $action): Link
132  {
133  return $this->factory->link()->standard($label, $action);
134  }
135 
136  protected function textInLegacy(string $text): Legacy
137  {
138  return $this->factory->legacy($text);
139  }
140 
141  protected function getSourceFromIRSS(string $string_id): string
142  {
143  if ($identifier = $this->irss->manage()->find($string_id)) {
144  return $this->irss->consume()->src($identifier)->getSrc();
145  }
146  return '';
147  }
148 }
$res
Definition: ltiservices.php:69
buildFallBackIcon(CopyrightDataInterface $copyright)
Definition: Renderer.php:102
buildIconFromLink(CopyrightDataInterface $copyright)
Definition: Renderer.php:80
This describes how an icon could be modified during construction of UI.
Definition: Icon.php:28
buildIcon(CopyrightDataInterface $copyright)
Definition: Renderer.php:65
getSourceFromIRSS(string $string_id)
Definition: Renderer.php:141
__construct(Factory $factory, IRSS $irss)
Definition: Renderer.php:37
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
buildIconFromFile(CopyrightDataInterface $copyright)
Definition: Renderer.php:88
buildLink(CopyrightDataInterface $copyright)
Definition: Renderer.php:115
customIcon(string $src, string $alt)
Definition: Renderer.php:126
standardLink(string $label, string $action)
Definition: Renderer.php:131
toUIComponents(CopyrightDataInterface $copyright)
Definition: Renderer.php:48