|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Util |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Pimf\Util; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Responsible for general message formatting, used for message flashing or with combination with the translator. |
|
13
|
|
|
* |
|
14
|
|
|
* <code> |
|
15
|
|
|
* $message = new Message( |
|
16
|
|
|
* 'Hello %your_name my name is %my_name! ' |
|
17
|
|
|
* .'I am %my_age, how old are you? I like %object!' |
|
18
|
|
|
* ); |
|
19
|
|
|
* |
|
20
|
|
|
* $message->bind('your_name', 'Ben') |
|
21
|
|
|
* ->bind('my_name', 'Matt') |
|
22
|
|
|
* ->bind('my_age', '21') |
|
23
|
|
|
* ->bind('object', 'food'); |
|
24
|
|
|
* |
|
25
|
|
|
* print $message; |
|
26
|
|
|
* |
|
27
|
|
|
* .. or .. |
|
28
|
|
|
* |
|
29
|
|
|
* $msg = $message->format(); |
|
30
|
|
|
* |
|
31
|
|
|
* .. output will be = "Hello Ben my name is Matt! I am 21, how old are you? I like food!" |
|
32
|
|
|
* </code> |
|
33
|
|
|
* |
|
34
|
|
|
* @package Util |
|
35
|
|
|
* @author Gjero Krsteski <[email protected]> |
|
36
|
|
|
*/ |
|
37
|
|
|
class Message |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* @var string The message. |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $message = ''; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array A list of tokes which should be bind. |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $bindings = array(); |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string The prefixed delimiter for the tokens. |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $delimiter = '%'; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $message The message or the resource. |
|
56
|
|
|
* @param array $bindings (Optional) A List of tokes whitch should be bind. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct($message, array $bindings = array()) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->setMessage($message); |
|
61
|
|
|
$this->bindings = $bindings; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getMessage() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->message; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $message The message. |
|
74
|
|
|
* |
|
75
|
|
|
* @return Message |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setMessage($message) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->message = $message; |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $char The character for the prexied delimitation of the tokens. |
|
86
|
|
|
* |
|
87
|
|
|
* @return Message |
|
88
|
|
|
*/ |
|
89
|
|
|
public function setDelimiter($char) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->delimiter = $char; |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Sets/Updates the value for the given token. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $token The token. |
|
100
|
|
|
* @param string $value The value for replacement. |
|
101
|
|
|
* |
|
102
|
|
|
* @return Message |
|
103
|
|
|
*/ |
|
104
|
|
|
public function bind($token, $value) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->bindings[$token] = $value; |
|
107
|
|
|
|
|
108
|
|
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* If the object is treated as string. |
|
113
|
|
|
* |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function __toString() |
|
117
|
|
|
{ |
|
118
|
|
|
return (string)$this->format(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Retuns formated message. |
|
123
|
|
|
* |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
|
|
public function format() |
|
127
|
|
|
{ |
|
128
|
|
|
if ($this->getMessage() == '' || !$this->getMessage()) { |
|
129
|
|
|
return ''; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if (count($this->bindings) > 0) { |
|
133
|
|
|
foreach ($this->bindings as $token => $value) { |
|
134
|
|
|
$this->message = str_replace($this->delimiter . $token, $value, $this->message); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $this->message; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|