|
9 | 9 |
|
10 | 10 | namespace Webfactory\Bundle\PolyglotBundle\Doctrine; |
11 | 11 |
|
| 12 | +use Doctrine\Common\Collections\Collection; |
12 | 13 | use Doctrine\Common\Collections\Criteria; |
13 | 14 | use Doctrine\Common\Collections\Selectable; |
14 | 15 | use Doctrine\ORM\UnitOfWork; |
|
25 | 26 | use Webfactory\Bundle\PolyglotBundle\TranslatableInterface; |
26 | 27 |
|
27 | 28 | /** |
| 29 | + * @template T |
| 30 | + * @implements TranslatableInterface<T> |
| 31 | + * |
28 | 32 | * This class implements `TranslatableInterface` for entities that are managed by |
29 | 33 | * the entity manager. PolyglotListener will replace `Translatable` instances with |
30 | 34 | * instances of this class as soon as a new entity is passed to EntityManager::persist(). |
@@ -61,17 +65,17 @@ final class PersistentTranslatable implements TranslatableInterface |
61 | 65 | private LoggerInterface $logger; |
62 | 66 |
|
63 | 67 | /** |
64 | | - * @param UnitOfWork $unitOfWork The UoW managing the entity that contains this PersistentTranslatable |
65 | | - * @param class-string $class The class of the entity containing this PersistentTranslatable instance |
66 | | - * @param object $entity The entity containing this PersistentTranslatable instance |
67 | | - * @param string $primaryLocale The locale for which the translated value will be persisted in the "main" entity |
68 | | - * @param DefaultLocaleProvider $defaultLocaleProvider DefaultLocaleProvider that provides the locale to use when no explicit locale is passed to e. g. translate() |
69 | | - * @param ReflectionProperty $translationProperty ReflectionProperty pointing to the field in the translations class that holds the translated value to use |
70 | | - * @param ReflectionProperty $translationCollection ReflectionProperty pointing to the collection in the main class that holds translation instances |
71 | | - * @param ReflectionClass $translationClass ReflectionClass for the class holding translated values |
72 | | - * @param ReflectionProperty $localeField ReflectionProperty pointing to the field in the translations class that holds a translation's locale |
73 | | - * @param ReflectionProperty $translationMapping ReflectionProperty pointing to the field in the translations class that refers back to the main entity (the owning side of the one-to-many translations collection). |
74 | | - * @param ReflectionProperty $translatedProperty ReflectionProperty pointing to the field in the main entity where this PersistentTranslatable instance will be used |
| 68 | + * @param UnitOfWork $unitOfWork The UoW managing the entity that contains this PersistentTranslatable |
| 69 | + * @param class-string $class The class of the entity containing this PersistentTranslatable instance |
| 70 | + * @param object $entity The entity containing this PersistentTranslatable instance |
| 71 | + * @param string $primaryLocale The locale for which the translated value will be persisted in the "main" entity |
| 72 | + * @param DefaultLocaleProvider $defaultLocaleProvider DefaultLocaleProvider that provides the locale to use when no explicit locale is passed to e. g. translate() |
| 73 | + * @param ReflectionProperty $translationProperty ReflectionProperty pointing to the field in the translations class that holds the translated value to use |
| 74 | + * @param ReflectionProperty $translationCollection ReflectionProperty pointing to the collection in the main class that holds translation instances |
| 75 | + * @param ReflectionClass<object> $translationClass ReflectionClass for the class holding translated values |
| 76 | + * @param ReflectionProperty $localeField ReflectionProperty pointing to the field in the translations class that holds a translation's locale |
| 77 | + * @param ReflectionProperty $translationMapping ReflectionProperty pointing to the field in the translations class that refers back to the main entity (the owning side of the one-to-many translations collection). |
| 78 | + * @param ReflectionProperty $translatedProperty ReflectionProperty pointing to the field in the main entity where this PersistentTranslatable instance will be used |
75 | 79 | */ |
76 | 80 | public function __construct( |
77 | 81 | private readonly UnitOfWork $unitOfWork, |
@@ -150,7 +154,10 @@ private function createTranslationEntity(string $locale): object |
150 | 154 | $this->localeField->setValue($entity, $locale); |
151 | 155 |
|
152 | 156 | $this->translationMapping->setValue($entity, $this->entity); |
153 | | - $this->translationCollection->getValue($this->entity)->add($entity); |
| 157 | + |
| 158 | + /** @var Collection<array-key, object> $collection */ |
| 159 | + $collection = $this->translationCollection->getValue($this->entity); |
| 160 | + $collection->add($entity); |
154 | 161 |
|
155 | 162 | self::$_translations[$this->class][$this->oid][$locale] = $entity; |
156 | 163 | $this->unitOfWork->persist($entity); |
@@ -241,13 +248,17 @@ private function isTranslationCached(string $locale): bool |
241 | 248 | */ |
242 | 249 | private function cacheTranslation(string $locale): void |
243 | 250 | { |
244 | | - /** @var $translationsInAllLanguages Selectable */ |
| 251 | + /** @var Selectable<array-key, object> $translationsInAllLanguages */ |
245 | 252 | $translationsInAllLanguages = $this->translationCollection->getValue($this->entity); |
246 | 253 | $criteria = $this->createLocaleCriteria($locale); |
247 | 254 | $translationsFilteredByLocale = $translationsInAllLanguages->matching($criteria); |
248 | 255 |
|
249 | 256 | $translationInLocale = ($translationsFilteredByLocale->count() > 0) ? $translationsFilteredByLocale->first() : null; |
250 | 257 |
|
| 258 | + if (is_bool($translationInLocale)) { |
| 259 | + return; |
| 260 | + } |
| 261 | + |
251 | 262 | self::$_translations[$this->class][$this->oid][$locale] = $translationInLocale; |
252 | 263 | } |
253 | 264 |
|
|
0 commit comments