1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Shared |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Shared\Message; |
16
|
|
|
|
17
|
|
|
use Phossa2\Shared\Base\StaticAbstract; |
18
|
|
|
use Phossa2\Shared\Message\Mapping\MappingTrait; |
19
|
|
|
use Phossa2\Shared\Message\Mapping\MappingInterface; |
20
|
|
|
use Phossa2\Shared\Message\Loader\LoaderAwareInterface; |
21
|
|
|
use Phossa2\Shared\Message\Formatter\FormatterAwareTrait; |
22
|
|
|
use Phossa2\Shared\Message\Formatter\FormatterAwareInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* MessageAbstract |
26
|
|
|
* |
27
|
|
|
* - Used to convert message code into human-readable messages. |
28
|
|
|
* |
29
|
|
|
* - Subclass *MUST* define its own property `$messages` |
30
|
|
|
* |
31
|
|
|
* - Message loader maybe used to load different message mapping such as |
32
|
|
|
* language files. |
33
|
|
|
* |
34
|
|
|
* - Message formatter maybe used to output message in different format. |
35
|
|
|
* |
36
|
|
|
* - Once loader set for parent class, it will affect all the descendant |
37
|
|
|
* classes unless they have own loader set. |
38
|
|
|
* |
39
|
|
|
* - extending `MessageAbstract` |
40
|
|
|
* |
41
|
|
|
* ```php |
42
|
|
|
* use Phossa2\Shared\Message\MessageAbstract |
43
|
|
|
* |
44
|
|
|
* // define own message class |
45
|
|
|
* class MyMessage extends MessageAbstract |
46
|
|
|
* { |
47
|
|
|
* // define unique message codes, usually YearMonthDateMinute |
48
|
|
|
* const MSG_HELLO = 1606151214; |
49
|
|
|
* |
50
|
|
|
* // MUST define message template mappings |
51
|
|
|
* protected static $messages = [ |
52
|
|
|
* self::MSG_HELLO => 'Hello %s' |
53
|
|
|
* ]; |
54
|
|
|
* } |
55
|
|
|
* ``` |
56
|
|
|
* |
57
|
|
|
* - usage |
58
|
|
|
* |
59
|
|
|
* ```php |
60
|
|
|
* // print 'Hello World' |
61
|
|
|
* echo MyMessage::get(MyMessage::MSG_HELLO, 'World'); |
62
|
|
|
* |
63
|
|
|
* // used in exception |
64
|
|
|
* throw new \Exception(MyMessage::get(MyMessage::MSG_HELLO, 'John')); |
65
|
|
|
* ``` |
66
|
|
|
* |
67
|
|
|
* @package Phossa2\Shared |
68
|
|
|
* @author Hong Zhang <[email protected]> |
69
|
|
|
* @see StaticAbstract |
70
|
|
|
* @see MessageInterface |
71
|
|
|
* @see LoaderAwareInterface |
72
|
|
|
* @see FormatterAwareInterface |
73
|
|
|
* @version 2.0.0 |
74
|
|
|
* @since 2.0.0 added |
75
|
|
|
*/ |
76
|
|
|
abstract class MessageAbstract extends StaticAbstract implements MessageInterface, MappingInterface, LoaderAwareInterface, FormatterAwareInterface |
77
|
|
|
{ |
78
|
|
|
use MappingTrait, FormatterAwareTrait; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritDoc} |
82
|
|
|
*/ |
83
|
|
|
public static function get(/*# int */ $code)/*# : string */ |
84
|
|
|
{ |
85
|
|
|
// process code |
86
|
|
|
if (!is_numeric($code)) { |
87
|
|
|
return is_scalar($code) ? (string) $code : print_r($code, true); |
88
|
|
|
} else { |
89
|
|
|
$code = (int) $code; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// get remaining arguments if any |
93
|
|
|
$arguments = func_get_args(); |
94
|
|
|
array_shift($arguments); |
95
|
|
|
|
96
|
|
|
// build message and return it |
97
|
|
|
return self::getFormatter()->formatMessage( |
98
|
|
|
self::getTemplateByCode($code, get_called_class()), |
99
|
|
|
$arguments |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|