@@ 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 |
@@ 21-64 (lines=44) @@ | ||
18 | * |
|
19 | * @author Nicolas Bastien <[email protected]> |
|
20 | */ |
|
21 | trait PersonalTranslatableTrait |
|
22 | { |
|
23 | use TranslatableTrait; |
|
24 | ||
25 | /** |
|
26 | * @return ArrayCollection|AbstractPersonalTranslation[] |
|
27 | */ |
|
28 | public function getTranslations() |
|
29 | { |
|
30 | return $this->translations; |
|
31 | } |
|
32 | ||
33 | /** |
|
34 | * @param $field |
|
35 | * @param $locale |
|
36 | * |
|
37 | * @return null|string |
|
38 | */ |
|
39 | public function getTranslation($field, $locale) |
|
40 | { |
|
41 | foreach ($this->getTranslations() as $translation) { |
|
42 | if (strcmp($translation->getField(), $field) === 0 && strcmp($translation->getLocale(), $locale) === 0) { |
|
43 | return $translation->getContent(); |
|
44 | } |
|
45 | } |
|
46 | ||
47 | return; |
|
48 | } |
|
49 | ||
50 | /** |
|
51 | * @param AbstractPersonalTranslation $translation |
|
52 | * |
|
53 | * @return $this |
|
54 | */ |
|
55 | public function addTranslation(AbstractPersonalTranslation $translation) |
|
56 | { |
|
57 | if (!$this->translations->contains($translation)) { |
|
58 | $translation->setObject($this); |
|
59 | $this->translations->add($translation); |
|
60 | } |
|
61 | ||
62 | return $this; |
|
63 | } |
|
64 | } |
|
65 |