Completed
Pull Request — 2.x (#124)
by Christian
02:47
created

PersonalTranslatableTrait::addTranslation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Sonata\TranslationBundle\Traits\Gedmo;
4
5
use Sonata\TranslationBundle\Traits\TranslatableTrait;
6
7
/**
8
 * If you don't want to use trait, you can extend AbstractPersonalTranslatable instead.
9
 *
10
 * @author Nicolas Bastien <[email protected]>
11
 */
12 View Code Duplication
trait PersonalTranslatableTrait
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
13
{
14
    use TranslatableTrait;
15
16
    /**
17
     * @return ArrayCollection|AbstractPersonalTranslation[]
18
     */
19
    public function getTranslations()
20
    {
21
        return $this->translations;
0 ignored issues
show
Bug introduced by
The property translations does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
    }
23
24
    /**
25
     * @param $field
26
     * @param $locale
27
     *
28
     * @return null|string
29
     */
30
    public function getTranslation($field, $locale)
31
    {
32
        foreach ($this->getTranslations() as $translation) {
33
            if (strcmp($translation->getField(), $field) === 0 && strcmp($translation->getLocale(), $locale) === 0) {
34
                return $translation->getContent();
35
            }
36
        }
37
38
        return;
39
    }
40
41
    /**
42
     * @param AbstractPersonalTranslation $translation
43
     *
44
     * @return $this
45
     */
46
    public function addTranslation(AbstractPersonalTranslation $translation)
47
    {
48
        if (!$this->translations->contains($translation)) {
49
            $translation->setObject($this);
50
            $this->translations->add($translation);
51
        }
52
53
        return $this;
54
    }
55
}
56