| @@ 25-79 (lines=55) @@ | ||
| 22 | * |
|
| 23 | * @see https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/translatable.md : Personal translations |
|
| 24 | */ |
|
| 25 | abstract class AbstractPersonalTranslatable extends AbstractTranslatable |
|
| 26 | { |
|
| 27 | /** |
|
| 28 | * @var ArrayCollection|AbstractPersonalTranslation[] |
|
| 29 | */ |
|
| 30 | protected $translations; |
|
| 31 | ||
| 32 | /** |
|
| 33 | * Constructor. |
|
| 34 | */ |
|
| 35 | public function __construct() |
|
| 36 | { |
|
| 37 | $this->translations = new ArrayCollection(); |
|
| 38 | } |
|
| 39 | ||
| 40 | /** |
|
| 41 | * @return ArrayCollection|AbstractPersonalTranslation[] |
|
| 42 | */ |
|
| 43 | public function getTranslations() |
|
| 44 | { |
|
| 45 | return $this->translations; |
|
| 46 | } |
|
| 47 | ||
| 48 | /** |
|
| 49 | * @param string $field |
|
| 50 | * @param string $locale |
|
| 51 | * |
|
| 52 | * @return null|string |
|
| 53 | */ |
|
| 54 | public function getTranslation($field, $locale) |
|
| 55 | { |
|
| 56 | foreach ($this->getTranslations() as $translation) { |
|
| 57 | if (strcmp($translation->getField(), $field) === 0 && strcmp($translation->getLocale(), $locale) === 0) { |
|
| 58 | return $translation->getContent(); |
|
| 59 | } |
|
| 60 | } |
|
| 61 | ||
| 62 | return; |
|
| 63 | } |
|
| 64 | ||
| 65 | /** |
|
| 66 | * @param AbstractPersonalTranslation $translation |
|
| 67 | * |
|
| 68 | * @return $this |
|
| 69 | */ |
|
| 70 | public function addTranslation(AbstractPersonalTranslation $translation) |
|
| 71 | { |
|
| 72 | if (!$this->translations->contains($translation)) { |
|
| 73 | $translation->setObject($this); |
|
| 74 | $this->translations->add($translation); |
|
| 75 | } |
|
| 76 | ||
| 77 | return $this; |
|
| 78 | } |
|
| 79 | } |
|
| 80 | ||
| @@ 22-65 (lines=44) @@ | ||
| 19 | * |
|
| 20 | * @author Nicolas Bastien <[email protected]> |
|
| 21 | */ |
|
| 22 | trait PersonalTranslatable |
|
| 23 | { |
|
| 24 | use Translatable; |
|
| 25 | ||
| 26 | /** |
|
| 27 | * @return ArrayCollection|AbstractPersonalTranslation[] |
|
| 28 | */ |
|
| 29 | public function getTranslations() |
|
| 30 | { |
|
| 31 | return $this->translations; |
|
| 32 | } |
|
| 33 | ||
| 34 | /** |
|
| 35 | * @param $field |
|
| 36 | * @param $locale |
|
| 37 | * |
|
| 38 | * @return null|string |
|
| 39 | */ |
|
| 40 | public function getTranslation($field, $locale) |
|
| 41 | { |
|
| 42 | foreach ($this->getTranslations() as $translation) { |
|
| 43 | if (strcmp($translation->getField(), $field) === 0 && strcmp($translation->getLocale(), $locale) === 0) { |
|
| 44 | return $translation->getContent(); |
|
| 45 | } |
|
| 46 | } |
|
| 47 | ||
| 48 | return; |
|
| 49 | } |
|
| 50 | ||
| 51 | /** |
|
| 52 | * @param AbstractPersonalTranslation $translation |
|
| 53 | * |
|
| 54 | * @return $this |
|
| 55 | */ |
|
| 56 | public function addTranslation(AbstractPersonalTranslation $translation) |
|
| 57 | { |
|
| 58 | if (!$this->translations->contains($translation)) { |
|
| 59 | $translation->setObject($this); |
|
| 60 | $this->translations->add($translation); |
|
| 61 | } |
|
| 62 | ||
| 63 | return $this; |
|
| 64 | } |
|
| 65 | } |
|
| 66 | ||