1 | <?php |
||
25 | class MessageFormatter |
||
26 | { |
||
27 | use CallbackChainHelperTrait; |
||
28 | |||
29 | const CALLBACK_POSITION_PREPEND = 1, |
||
30 | CALLBACK_POSITION_APPEND = 2; |
||
31 | |||
32 | const FORMAT_KEY_TARGET_ID = 'targetId', |
||
33 | FORMAT_KEY_LINE = 'line', |
||
34 | FORMAT_KEY_VERSION = 'version', |
||
35 | FORMAT_KEY_REASON_ID = 'reasonId', |
||
36 | FORMAT_KEY_REASON_NAME = 'reasonName'; |
||
37 | |||
38 | /** |
||
39 | * @var MessageLocator |
||
40 | */ |
||
41 | private $locator; |
||
42 | /** |
||
43 | * @var bool |
||
44 | */ |
||
45 | private $throwOnMissingTemplate = false; |
||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | private $messageFormatters = []; |
||
50 | |||
51 | /** |
||
52 | * @param array|MessageLocator $locator |
||
53 | * @param bool $addDefaultFormatter |
||
54 | * @param bool $addDefaultExclusiveMissingMessageLocatorHandler |
||
55 | * @param bool $throwOnMissingTemplate |
||
56 | */ |
||
57 | 74 | public function __construct( |
|
93 | |||
94 | /** |
||
95 | * @param callable $transformer A callback with the following signature: |
||
96 | * ``` |
||
97 | * function(int|string $msgId, string $template, MessageFormatter $f, array $data) : string |
||
98 | * ``` |
||
99 | * @param int $position |
||
100 | * @return $this |
||
101 | */ |
||
102 | 20 | public function addMessageFormatter(callable $transformer, $position = self::CALLBACK_POSITION_APPEND) |
|
112 | |||
113 | /** |
||
114 | * @param $msgId |
||
115 | * @param array $data |
||
116 | * @return mixed |
||
117 | * @throws \Exception |
||
118 | */ |
||
119 | 54 | public function getFormattedMessageFromId($msgId, array $data = []) |
|
123 | |||
124 | /** |
||
125 | * @param $messageInfo |
||
126 | * @param array $data |
||
127 | * @param bool $runUserFormatters |
||
128 | * @return mixed |
||
129 | */ |
||
130 | 56 | public function format($messageInfo, array $data = [], $runUserFormatters = true) |
|
156 | |||
157 | /** |
||
158 | * @param $msgId |
||
159 | * @return null|string |
||
160 | * @throws \Exception |
||
161 | */ |
||
162 | 58 | public function getMessageTemplate($msgId) |
|
170 | |||
171 | /** |
||
172 | * @return \Pvra\Result\MessageLocator |
||
173 | */ |
||
174 | 62 | public function getLocator() |
|
178 | |||
179 | /** |
||
180 | * @param $msgId |
||
181 | * @return bool |
||
182 | */ |
||
183 | 2 | public function messageForIdExists($msgId) |
|
187 | } |
||
188 |