ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
CopyrightHandler.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\MetaData\Copyright\RepositoryInterface as CopyrightRepository;
25use ILIAS\MetaData\Copyright\RendererInterface as CopyrightRenderer;
28
30{
31 protected CopyrightRepository $copyright_repository;
32 protected IdentifierHandler $identifier_handler;
34
38 protected array $copyright_entries;
39
40 public function __construct(
41 CopyrightRepository $copyright_repository,
42 IdentifierHandler $identifier_handler,
44 ) {
45 $this->copyright_repository = $copyright_repository;
46 $this->identifier_handler = $identifier_handler;
47 $this->settings = $settings;
48 }
49
50 public function copyrightForExport(string $copyright): string
51 {
52 return $this->copyrightAsString($copyright);
53 }
54
55 public function copyrightFromExport(string $copyright): string
56 {
57 if (!$this->isCopyrightSelectionActive()) {
58 return $copyright;
59 }
60
61 // url should be made to match regardless of scheme
62 $normalized_copyright = str_replace('https://', 'http://', $copyright);
63
64 $matches_by_name = null;
65 foreach ($this->getAllCopyrightEntries() as $entry) {
66 $entry_link = (string) $entry->copyrightData()->link();
67 $normalized_link = str_replace('https://', 'http://', $entry_link);
68 if ($normalized_link !== '' && str_contains($normalized_copyright, $normalized_link)) {
69 return $this->identifier_handler->buildIdentifierFromEntryID($entry->id());
70 }
71
72 if (
73 is_null($matches_by_name) &&
74 trim($copyright) === trim($entry->copyrightData()->fullName())
75 ) {
76 $matches_by_name = $this->identifier_handler->buildIdentifierFromEntryID($entry->id());
77 }
78 }
79
80 if (!is_null($matches_by_name)) {
81 return $matches_by_name;
82 }
83 return $copyright;
84 }
85
86 public function copyrightAsString(string $copyright): string
87 {
88 if (!$this->isCopyrightSelectionActive()) {
89 return $copyright;
90 }
91
92 if (!$this->identifier_handler->isIdentifierValid($copyright) && $copyright !== '') {
93 return $copyright;
94 }
95
96 if ($copyright === '') {
97 $entry_data = $this->copyright_repository->getDefaultEntry()->copyrightData();
98 } else {
99 $entry_id = $this->identifier_handler->parseEntryIDFromIdentifier($copyright);
100 $entry_data = $this->copyright_repository->getEntry($entry_id)->copyrightData();
101 }
102 $full_name = $entry_data->fullName();
103 $link = $entry_data->link();
104
105 if (!is_null($link)) {
106 return (string) $link;
107 }
108 return $full_name;
109 }
110
114 protected function getAllCopyrightEntries(): \Generator
115 {
116 if (!isset($this->copyright_entries)) {
117 $this->copyright_entries = iterator_to_array($this->copyright_repository->getAllEntries());
118 }
120 }
121
122 public function isCopyrightSelectionActive(): bool
123 {
124 return $this->settings->isCopyrightSelectionActive();
125 }
126}
__construct(CopyrightRepository $copyright_repository, IdentifierHandler $identifier_handler, SettingsInterface $settings)