Phrase::setRenderer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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;
10
11
use Gojira\Framework\Phrase\Renderer\Placeholder as RendererPlaceholder;
12
use Gojira\Framework\Phrase\RendererInterface;
13
14
/**
15
 * Class Phrase
16
 *
17
 * @package Gojira\Framework
18
 * @author  Toan Nguyen <[email protected]>
19
 */
20
class Phrase
21
{
22
    /**
23
     * Default phrase renderer. Allows stacking renderers that "don't know about each other"
24
     *
25
     * @var RendererInterface
26
     */
27
    private static $renderer;
28
29
    /**
30
     * String for rendering
31
     *
32
     * @var string
33
     */
34
    private $text;
35
36
    /**
37
     * Arguments for placeholder values
38
     *
39
     * @var array
40
     */
41
    private $arguments;
42
43
    /**
44
     * Set default Phrase renderer
45
     *
46
     * @param RendererInterface $renderer
47
     *
48
     * @return void
49
     */
50
    public static function setRenderer(RendererInterface $renderer)
51
    {
52
        self::$renderer = $renderer;
53
    }
54
55
    /**
56
     * Get default Phrase renderer
57
     *
58
     * @return RendererInterface
59
     */
60
    public static function getRenderer()
61
    {
62
        if (!self::$renderer) {
63
            self::$renderer = new RendererPlaceholder();
64
        }
65
        return self::$renderer;
66
    }
67
68
    /**
69
     * Phrase construct
70
     *
71
     * @param string $text
72
     * @param array  $arguments
73
     */
74
    public function __construct($text, array $arguments = [])
75
    {
76
        $this->text = (string)$text;
77
        $this->arguments = $arguments;
78
    }
79
80
    /**
81
     * Get phrase base text
82
     *
83
     * @return string
84
     */
85
    public function getText()
86
    {
87
        return $this->text;
88
    }
89
90
    /**
91
     * Get phrase message arguments
92
     *
93
     * @return array
94
     */
95
    public function getArguments()
96
    {
97
        return $this->arguments;
98
    }
99
100
    /**
101
     * Render phrase
102
     *
103
     * @return string
104
     */
105
    public function render()
106
    {
107
        try {
108
            return self::getRenderer()->render([$this->text], $this->getArguments());
109
        } catch (\Exception $e) {
110
            return $this->getText();
111
        }
112
    }
113
114
    /**
115
     * Defers rendering to the last possible moment (when converted to string)
116
     *
117
     * @return string
118
     */
119
    public function __toString()
120
    {
121
        return $this->render();
122
    }
123
}
124