Passed
Pull Request — release/4.x (#44)
by Erik
07:43 queued 03:55
created

CallbackTranslator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 42
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A translateValueInput() 0 7 2
A __construct() 0 5 1
A translateValueOutput() 0 7 2
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Bundle\UrlBundle\Url\Params\Translator;
8
9
/**
10
 * Translator which uses callbacks for input/output translation
11
 */
12
class CallbackTranslator extends StaticTranslator
13
{
14
    protected $valueInputTranslator;
15
    protected $valueOutputTranslator;
16
17
    /**
18
     * Constructor.
19
     *
20
     * @param string $keyName
21
     * @param string $keyTranslation
22
     * @param callable $valueInputTranslator
23
     * @param callable $valueOutputTranslator
24
     */
25
    public function __construct($keyName, $keyTranslation, $valueInputTranslator, $valueOutputTranslator)
26
    {
27
        parent::__construct($keyName, $keyTranslation, []);
28
        $this->valueInputTranslator  = $valueInputTranslator;
29
        $this->valueOutputTranslator = $valueOutputTranslator;
30
    }
31
32
    /**
33
     * @{inheritDoc}
34
     */
35
    public function translateValueInput($keyName, $value)
36
    {
37
        if ($this->translateKeyInput($keyName) !== false) {
0 ignored issues
show
introduced by
The condition $this->translateKeyInput($keyName) !== false is always true.
Loading history...
38
            return call_user_func($this->valueInputTranslator, $value);
39
        }
40
41
        return false;
42
    }
43
44
    /**
45
     * @{inheritDoc}
46
     */
47
    public function translateValueOutput($keyName, $value)
48
    {
49
        if ($this->translateKeyOutput($keyName) !== false) {
0 ignored issues
show
introduced by
The condition $this->translateKeyOutput($keyName) !== false is always true.
Loading history...
50
            return call_user_func($this->valueOutputTranslator, $value);
51
        }
52
53
        return false;
54
    }
55
}
56