Completed
Push — master ( a7df58...69cda4 )
by Toan
16s
created

Placeholder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 12 2
A keyToPlaceholder() 0 4 2
1
<?php
2
/**
3
 * Copyright © 2017 Toan Nguyen. All rights reserved.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Gojira\Framework\Phrase\Renderer;
10
11
use Gojira\Framework\Phrase\RendererInterface;
12
13
/**
14
 * Placeholder renderer
15
 * __('format %1 %2 %3', $var1, $var2, $var3)
16
 *
17
 * @package Gojira\Framework\Phrase\Renderer
18
 * @author  Toan Nguyen <[email protected]>
19
 */
20
class Placeholder implements RendererInterface
21
{
22
    /**
23
     * Render source text
24
     *
25
     * @param array $source
26
     * @param array $arguments
27
     * @return string
28
     */
29
    public function render(array $source, array $arguments)
30
    {
31
        $text = end($source);
32
33
        if ($arguments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $arguments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
34
            $placeholders = array_map([$this, 'keyToPlaceholder'], array_keys($arguments));
35
            $pairs = array_combine($placeholders, $arguments);
36
            $text = strtr($text, $pairs);
37
        }
38
39
        return $text;
40
    }
41
42
    /**
43
     * Get key to placeholder
44
     *
45
     * @param string|int $key
46
     * @return string
47
     */
48
    private function keyToPlaceholder($key)
49
    {
50
        return '%' . (is_int($key) ? strval($key + 1) : $key);
51
    }
52
}
53