|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file plugins/generic/thoth/classes/factories/ThothAbstractFactory.php |
| 5 | + * |
| 6 | + * Copyright (c) 2024-2026 Lepidus Tecnologia |
| 7 | + * Copyright (c) 2024-2026 Thoth |
| 8 | + * Distributed under the GNU GPL v3. For full terms see the file docs/COPYING. |
| 9 | + * |
| 10 | + * @class ThothAbstractFactory |
| 11 | + * |
| 12 | + * @ingroup plugins_generic_thoth |
| 13 | + * |
| 14 | + * @brief A factory to create Thoth abstracts |
| 15 | + */ |
| 16 | + |
| 17 | +namespace APP\plugins\generic\thoth\classes\factories; |
| 18 | + |
| 19 | +use APP\plugins\generic\thoth\classes\i18n\ThothLocaleCode; |
| 20 | +use ThothApi\GraphQL\Models\AbstractText as ThothAbstract; |
| 21 | + |
| 22 | +class ThothAbstractFactory |
| 23 | +{ |
| 24 | + public function createFromPublication($publication, string $workId, ?string $preferredLocale = null): array |
| 25 | + { |
| 26 | + return $this->create($publication, $workId, $preferredLocale); |
| 27 | + } |
| 28 | + |
| 29 | + public function createFromChapter($chapter, string $workId, ?string $preferredLocale = null): array |
| 30 | + { |
| 31 | + return $this->create($chapter, $workId, $preferredLocale); |
| 32 | + } |
| 33 | + |
| 34 | + private function create($entity, string $workId, ?string $preferredLocale = null): array |
| 35 | + { |
| 36 | + $canonicalLocale = $this->getCanonicalLocale($entity, $preferredLocale); |
| 37 | + $abstracts = $this->getLocalizedValues($entity, 'abstract', $canonicalLocale); |
| 38 | + $thothAbstracts = []; |
| 39 | + |
| 40 | + foreach ($abstracts as $locale => $abstract) { |
| 41 | + $localeCode = $this->getLocaleCode($locale); |
| 42 | + if ($localeCode === null) { |
| 43 | + $this->logUnsupportedLocale('abstract', $locale); |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + $thothAbstracts[$this->getLocaleKey($localeCode)] = new ThothAbstract([ |
| 48 | + 'workId' => $workId, |
| 49 | + 'localeCode' => $localeCode, |
| 50 | + 'content' => $this->wrapInParagraph($abstract), |
| 51 | + 'canonical' => $locale === $canonicalLocale, |
| 52 | + 'abstractType' => 'LONG', |
| 53 | + ]); |
| 54 | + } |
| 55 | + |
| 56 | + return $thothAbstracts; |
| 57 | + } |
| 58 | + |
| 59 | + public function getCanonicalLocale($entity, ?string $preferredLocale = null): ?string |
| 60 | + { |
| 61 | + $locales = $this->getSupportedLocales($this->getLocalizedValues($entity, 'abstract', $preferredLocale)); |
| 62 | + |
| 63 | + if ($preferredLocale && in_array($preferredLocale, $locales, true)) { |
| 64 | + return $preferredLocale; |
| 65 | + } |
| 66 | + |
| 67 | + return $locales[0] ?? $preferredLocale; |
| 68 | + } |
| 69 | + |
| 70 | + public function getLocaleKey(?string $localeCode): string |
| 71 | + { |
| 72 | + return $localeCode ?? ''; |
| 73 | + } |
| 74 | + |
| 75 | + private function getLocalizedValues($entity, string $key, ?string $fallbackLocale = null): array |
| 76 | + { |
| 77 | + $values = $entity->getData($key); |
| 78 | + if (is_array($values)) { |
| 79 | + return array_filter($values, fn ($value) => $value !== null && $value !== ''); |
| 80 | + } |
| 81 | + |
| 82 | + if ($values !== null && $values !== '' && $fallbackLocale) { |
| 83 | + return [$fallbackLocale => $values]; |
| 84 | + } |
| 85 | + |
| 86 | + return []; |
| 87 | + } |
| 88 | + |
| 89 | + private function getLocaleCode(?string $locale): ?string |
| 90 | + { |
| 91 | + return ThothLocaleCode::fromPkpLocale($locale); |
| 92 | + } |
| 93 | + |
| 94 | + private function getSupportedLocales(array $localizedValues): array |
| 95 | + { |
| 96 | + return array_values(array_filter( |
| 97 | + array_keys($localizedValues), |
| 98 | + fn (string $locale): bool => $this->getLocaleCode($locale) !== null |
| 99 | + )); |
| 100 | + } |
| 101 | + |
| 102 | + private function logUnsupportedLocale(string $entityType, ?string $locale): void |
| 103 | + { |
| 104 | + $normalizedLocaleCode = $locale ? strtoupper(str_replace(['-', '@'], '_', $locale)) : 'NULL'; |
| 105 | + error_log(sprintf( |
| 106 | + '[thoth] Skipping unsupported locale for %s: sourceLocale=%s normalizedLocaleCode=%s', |
| 107 | + $entityType, |
| 108 | + $locale ?? 'NULL', |
| 109 | + $normalizedLocaleCode |
| 110 | + )); |
| 111 | + } |
| 112 | + |
| 113 | + private function wrapInParagraph(string $content): string |
| 114 | + { |
| 115 | + $content = trim($content); |
| 116 | + if (preg_match('/^<p\b[^>]*>.*<\/p>$/is', $content) === 1) { |
| 117 | + return $content; |
| 118 | + } |
| 119 | + |
| 120 | + return sprintf('<p>%s</p>', $content); |
| 121 | + } |
| 122 | +} |
0 commit comments