Commit 3d1a625c by Paul Klimov

'yii\mail\MessageInterface' updated:

- 'body()' renamed to 'renderBody()' - new 'body()' method introduced
parent b0c5981d
...@@ -54,9 +54,10 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont ...@@ -54,9 +54,10 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
* - 'subject' argument for [[MessageInterface::subject()]] * - 'subject' argument for [[MessageInterface::subject()]]
* - 'text' argument for [[MessageInterface::text()]] * - 'text' argument for [[MessageInterface::text()]]
* - 'html' argument for [[MessageInterface::html()]] * - 'html' argument for [[MessageInterface::html()]]
* - 'body' argument for [[MessageInterface::body()]]
* - 'renderText' list of arguments for [[MessageInterface::renderText()]] * - 'renderText' list of arguments for [[MessageInterface::renderText()]]
* - 'renderHtml' list of arguments for [[MessageInterface::renderHtml()]] * - 'renderHtml' list of arguments for [[MessageInterface::renderHtml()]]
* - 'body' list of arguments for [[MessageInterface::body()]] * - 'renderBody' list of arguments for [[MessageInterface::renderBody()]]
* For example: * For example:
* ~~~ * ~~~
* array( * array(
...@@ -132,11 +133,12 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont ...@@ -132,11 +133,12 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
'subject', 'subject',
'text', 'text',
'html', 'html',
'body',
]; ];
$setupMethodNames = [ $setupMethodNames = [
'renderText', 'renderText',
'renderHtml', 'renderHtml',
'body', 'renderBody',
]; ];
$directSetterConfig = []; $directSetterConfig = [];
$setupMethodConfig = []; $setupMethodConfig = [];
......
...@@ -46,6 +46,21 @@ abstract class BaseMessage extends Object implements MessageInterface ...@@ -46,6 +46,21 @@ abstract class BaseMessage extends Object implements MessageInterface
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function body($body)
{
if (is_array($body)) {
$this->html($body['html']);
$this->text($body['text']);
} else {
$this->html($body);
$this->text(strip_tags($body));
}
return $this;
}
/**
* @inheritdoc
*/
public function renderHtml($view, $params = []) public function renderHtml($view, $params = [])
{ {
$this->html($this->getMailer()->render($view, $params, $this->getMailer()->htmlLayout)); $this->html($this->getMailer()->render($view, $params, $this->getMailer()->htmlLayout));
...@@ -64,15 +79,14 @@ abstract class BaseMessage extends Object implements MessageInterface ...@@ -64,15 +79,14 @@ abstract class BaseMessage extends Object implements MessageInterface
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function body($view, $params = []) public function renderBody($view, $params = [])
{ {
if (is_array($view)) { if (is_array($view)) {
$this->renderHtml($view['html'], $params); $this->renderHtml($view['html'], $params);
$this->renderText($view['text'], $params); $this->renderText($view['text'], $params);
} else { } else {
$html = $this->getMailer()->render($view, $params, $this->getMailer()->htmlLayout); $html = $this->getMailer()->render($view, $params, $this->getMailer()->htmlLayout);
$this->html($html); $this->body($html);
$this->text(strip_tags($html));
} }
return $this; return $this;
} }
......
...@@ -97,6 +97,16 @@ interface MessageInterface ...@@ -97,6 +97,16 @@ interface MessageInterface
public function html($html); public function html($html);
/** /**
* Sets message HTML and plain text content.
* @param string|array $body varies method behavior depending on type:
* - string - the HTML body content, in this case text body will be composed from
* html one using [[strip_tags()]] function.
* - array - list of body contents for each body type in format: ['html' => 'htmlContent', 'text' => 'textContent']
* @return static self reference.
*/
public function body($body);
/**
* Attaches existing file to the email message. * Attaches existing file to the email message.
* @param string $fileName full file name * @param string $fileName full file name
* @param array $options options for embed file. Valid options are: * @param array $options options for embed file. Valid options are:
...@@ -175,7 +185,7 @@ interface MessageInterface ...@@ -175,7 +185,7 @@ interface MessageInterface
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
* @return static self reference. * @return static self reference.
*/ */
public function body($view, $params = []); public function renderBody($view, $params = []);
/** /**
* Returns string representation of this message. * Returns string representation of this message.
......
...@@ -65,7 +65,7 @@ class BaseMessageTest extends TestCase ...@@ -65,7 +65,7 @@ class BaseMessageTest extends TestCase
$message = $mailer->compose(); $message = $mailer->compose();
$viewName = 'test/html/view'; $viewName = 'test/html/view';
$message->body($viewName); $message->renderBody($viewName);
$expectedHtml = 'view=' . $viewName . ' layout=' . $mailer->htmlLayout; $expectedHtml = 'view=' . $viewName . ' layout=' . $mailer->htmlLayout;
$this->assertEquals($expectedHtml, $message->html, 'Unable to compose html!'); $this->assertEquals($expectedHtml, $message->html, 'Unable to compose html!');
$expectedText = strip_tags($expectedHtml); $expectedText = strip_tags($expectedHtml);
...@@ -73,7 +73,7 @@ class BaseMessageTest extends TestCase ...@@ -73,7 +73,7 @@ class BaseMessageTest extends TestCase
$textViewName = 'test/text/view'; $textViewName = 'test/text/view';
$htmlViewName = 'test/html/view'; $htmlViewName = 'test/html/view';
$message->body(['text' => $textViewName, 'html' => $htmlViewName]); $message->renderBody(['text' => $textViewName, 'html' => $htmlViewName]);
$expectedHtml = 'view=' . $htmlViewName . ' layout=' . $mailer->htmlLayout; $expectedHtml = 'view=' . $htmlViewName . ' layout=' . $mailer->htmlLayout;
$this->assertEquals($expectedHtml, $message->html, 'Unable to compose html from separated view!'); $this->assertEquals($expectedHtml, $message->html, 'Unable to compose html from separated view!');
$expectedText = 'view=' . $textViewName . ' layout=' . $mailer->textLayout; $expectedText = 'view=' . $textViewName . ' layout=' . $mailer->textLayout;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment