These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of the Sonata Project package. |
||
7 | * |
||
8 | * (c) Thomas Rabaix <[email protected]> |
||
9 | * |
||
10 | * For the full copyright and license information, please view the LICENSE |
||
11 | * file that was distributed with this source code. |
||
12 | */ |
||
13 | |||
14 | namespace Sonata\TranslationBundle\Model\Gedmo; |
||
15 | |||
16 | use Doctrine\Common\Collections\ArrayCollection; |
||
17 | use Gedmo\Mapping\Annotation as Gedmo; |
||
18 | |||
19 | /** |
||
20 | * This is your base class if you want to use gedmo personal translation |
||
21 | * ie: if you want to have a dedicated translation table by model table. |
||
22 | * |
||
23 | * @author Nicolas Bastien <[email protected]> |
||
24 | * |
||
25 | * @see https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/translatable.md : Personal translations |
||
26 | */ |
||
27 | View Code Duplication | abstract class AbstractPersonalTranslatable extends AbstractTranslatable |
|
0 ignored issues
–
show
|
|||
28 | { |
||
29 | /** |
||
30 | * @var ArrayCollection|AbstractPersonalTranslation[] |
||
31 | */ |
||
32 | protected $translations; |
||
33 | |||
34 | public function __construct() |
||
35 | { |
||
36 | $this->translations = new ArrayCollection(); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @return ArrayCollection|AbstractPersonalTranslation[] |
||
41 | */ |
||
42 | public function getTranslations() |
||
43 | { |
||
44 | return $this->translations; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param string $field |
||
49 | * @param string $locale |
||
50 | * |
||
51 | * @return string|null |
||
52 | */ |
||
53 | public function getTranslation($field, $locale) |
||
54 | { |
||
55 | foreach ($this->getTranslations() as $translation) { |
||
56 | if (0 === strcmp($translation->getField(), $field) && 0 === strcmp($translation->getLocale(), $locale)) { |
||
57 | return $translation->getContent(); |
||
58 | } |
||
59 | } |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @return $this |
||
64 | */ |
||
65 | public function addTranslation(AbstractPersonalTranslation $translation) |
||
66 | { |
||
67 | if (!$this->translations->contains($translation)) { |
||
68 | $translation->setObject($this); |
||
69 | $this->translations->add($translation); |
||
70 | } |
||
71 | |||
72 | return $this; |
||
73 | } |
||
74 | } |
||
75 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.