Completed
Pull Request — master (#1)
by Gorka
04:16
created

Translatable::translations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the CMS Kernel library.
5
 *
6
 * Copyright (c) 2016 LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\CMSKernel\Infraestructure\Model\Translation;
13
14
use LIN3S\CMSKernel\Domain\Model\Translation\Locale;
15
use LIN3S\CMSKernel\Domain\Model\Translation\Translation;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, LIN3S\CMSKernel\Infraest...Translation\Translation.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
use LIN3S\CMSKernel\Domain\Model\Translation\Translatable as TranslatableInterface;
17
use LIN3S\CMSKernel\Domain\Model\Translation\TranslationCollection;
18
19
/**
20
 * @author Beñat Espiña <[email protected]>
21
 * @author Gorka Laucirica <[email protected]>
22
 */
23
abstract class Translatable implements TranslatableInterface
24
{
25
    protected $translations;
26
27
    public function __construct(Translation $translation)
28
    {
29
        $this->translations = new TranslationCollection();
30
        $this->addTranslation($translation);
31
    }
32
33
    public function translations()
34
    {
35
        return $this->translations;
36
    }
37
38
    public function addTranslation(Translation $translation)
39
    {
40
        $this->translations->addTranslation($translation, $this);
41
    }
42
43
    public function removeTranslation(Locale $locale)
44
    {
45
        $this->translations->removeTranslation($locale);
46
    }
47
48
    public function __call($locale, $args)
49
    {
50
        $this->translations->getTranslation($locale);
51
    }
52
}
53