Skip to content

Commit 3e9fd27

Browse files
committed
Método replace passa a aceitar somente string.
Para usar arquivos de template use replaceFile.
1 parent a70a392 commit 3e9fd27

File tree

2 files changed

+58
-29
lines changed

2 files changed

+58
-29
lines changed

README.md

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,45 @@ include __DIR__.'/vendor/autoload.php';
1515

1616
use MailReplace\MailReplace;
1717

18-
// $template = __DIR__.'/template.html';
18+
$template = __DIR__.'/template.html';
1919

20-
$template = <<<TEXT
21-
<!DOCTYPE html>
22-
<html>
23-
<head>
24-
<body>
25-
<h1 style="font-size: 14pt;">
26-
{{title}} - [[[date_time]]]
27-
</h1>
28-
<p>Hi {{user}}, this is a contact message, how are you?</p>
29-
</body>
30-
</head>
31-
</html>
32-
TEXT;
33-
34-
35-
/*
36-
MailReplace::replaceToFile (__DIR__.'/new_template.html', $template, [
37-
'{{user}}' => 'ET Bilu',
38-
'{{title}}' => 'Mail',
39-
'[[[date_time]]]' => date ('m/d/y')
40-
]);
41-
*/
42-
43-
$new_html = MailReplace::replace ($template, [
20+
$new_html = MailReplace::replaceFile ($template, [
4421
'{{user}}' => 'ET Bilu',
4522
'{{title}}' => 'Mail',
4623
'[[[date_time]]]' => date ('m/d/y')
4724
]);
4825

4926
echo $new_html;
27+
28+
// $template = <<<TEXT
29+
// <!DOCTYPE html>
30+
// <html>
31+
// <head>
32+
// <body>
33+
// <h1 style="font-size: 14pt;">
34+
// {{title}} - [[[date_time]]]
35+
// </h1>
36+
// <p>Hi {{user}}, this is a contact message, how are you?</p>
37+
// </body>
38+
// </head>
39+
// </html>
40+
// TEXT;
41+
42+
## save in new_template.html
43+
// MailReplace::replaceToFile (__DIR__.'/new_template.html', $template, [
44+
// '{{user}}' => 'ET Bilu',
45+
// '{{title}}' => 'Mail',
46+
// '[[[date_time]]]' => date ('m/d/y')
47+
// ]);
48+
49+
// $html = MailReplace::replace ($template, [
50+
// '{{user}}' => 'ET Bilu',
51+
// '{{title}}' => 'Mail',
52+
// '[[[date_time]]]' => date ('m/d/y')
53+
// ]);
54+
55+
// echo $html;
56+
5057
/*
5158
<!DOCTYPE html>
5259
<html>

src/MailReplace.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,42 @@
88
class MailReplace {
99

1010
/**
11-
* Making changes within a string
11+
* Make changes within a string
1212
*
1313
* @param string $mail
1414
* @param array $data
1515
*
1616
* @return string
1717
*/
1818
public static function replace (string $mail, array $data) :string
19+
{
20+
21+
$mail_data = $mail;
22+
23+
foreach ($data as $key => $value){
24+
$search [] = $key;
25+
$replace [] = $value;
26+
}
27+
28+
return str_replace ($search, $replace, $mail_data);
29+
30+
}
31+
32+
/**
33+
* Make changes within a string
34+
*
35+
* @param string $file
36+
* @param array $data
37+
*
38+
* @return string
39+
*/
40+
public static function replaceFile (string $file, array $data) :string
1941
{
2042

21-
if (is_file ($mail)){
22-
$mail_data = self::openFile ($mail);
43+
if (is_file ($file)){
44+
$mail_data = self::openFile ($file);
2345
}else {
24-
$mail_data = $mail;
46+
throw new \Exception ('File not exists!');
2547
}
2648

2749
foreach ($data as $key => $value){

0 commit comments

Comments
 (0)