[347] | 1 | <?php
|
---|
| 2 | /**
|
---|
| 3 | * Html2Pdf Library - Exception class
|
---|
| 4 | *
|
---|
| 5 | * HTML => PDF converter
|
---|
| 6 | * distributed under the OSL-3.0 License
|
---|
| 7 | *
|
---|
| 8 | * @package Html2pdf
|
---|
| 9 | * @author Laurent MINGUET <webmaster@html2pdf.fr>
|
---|
| 10 | * @copyright 2017 Laurent MINGUET
|
---|
| 11 | */
|
---|
| 12 |
|
---|
| 13 | namespace Spipu\Html2Pdf\Exception;
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
| 16 | * Exception Formatter
|
---|
| 17 | */
|
---|
| 18 | class ExceptionFormatter
|
---|
| 19 | {
|
---|
| 20 | /**
|
---|
| 21 | * the text message
|
---|
| 22 | * @var string
|
---|
| 23 | */
|
---|
| 24 | protected $message;
|
---|
| 25 |
|
---|
| 26 | /**
|
---|
| 27 | * the html message
|
---|
| 28 | * @var string
|
---|
| 29 | */
|
---|
| 30 | protected $htmlMessage;
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * PHP Constructor
|
---|
| 34 | *
|
---|
| 35 | * @param Html2PdfException $e the exception to format
|
---|
| 36 | *
|
---|
| 37 | * @return ExceptionFormatter
|
---|
| 38 | */
|
---|
| 39 | public function __construct(Html2PdfException $e)
|
---|
| 40 | {
|
---|
| 41 | $data = $this->getAdditionalData($e);
|
---|
| 42 |
|
---|
| 43 | $this->buildTextMessage($e, $data);
|
---|
| 44 | $this->buildHtmlMessage($e, $data);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
| 48 | * get the txt message
|
---|
| 49 | *
|
---|
| 50 | * @return string
|
---|
| 51 | */
|
---|
| 52 | public function getMessage()
|
---|
| 53 | {
|
---|
| 54 | return $this->message;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
| 58 | * get tht HTML message
|
---|
| 59 | *
|
---|
| 60 | * @return string
|
---|
| 61 | */
|
---|
| 62 | public function getHtmlMessage()
|
---|
| 63 | {
|
---|
| 64 | return $this->htmlMessage;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | * get the additional data from the exception
|
---|
| 69 | *
|
---|
| 70 | * @param Html2PdfException $e the exception to display
|
---|
| 71 | *
|
---|
| 72 | * @return array
|
---|
| 73 | */
|
---|
| 74 | protected function getAdditionalData(Html2PdfException $e)
|
---|
| 75 | {
|
---|
| 76 | $data = array();
|
---|
| 77 |
|
---|
| 78 | // read the error
|
---|
| 79 | switch ($e->getCode()) {
|
---|
| 80 | case HtmlParsingException::ERROR_CODE:
|
---|
| 81 | /** @var HtmlParsingException $e */
|
---|
| 82 | $data['invalid tag'] = $e->getInvalidTag();
|
---|
| 83 | $data['html line'] = $e->getHtmlLine();
|
---|
| 84 | break;
|
---|
| 85 |
|
---|
| 86 | case ImageException::ERROR_CODE:
|
---|
| 87 | /** @var ImageException $e */
|
---|
| 88 | $data['image src'] = $e->getImage();
|
---|
| 89 | break;
|
---|
| 90 |
|
---|
| 91 | case LongSentenceException::ERROR_CODE:
|
---|
| 92 | /** @var LongSentenceException $e */
|
---|
| 93 | $data['sentence'] = $e->getSentence();
|
---|
| 94 | $data['box width'] = $e->getWidthBox();
|
---|
| 95 | $data['length'] = $e->getLength();
|
---|
| 96 | break;
|
---|
| 97 |
|
---|
| 98 | case TableException::ERROR_CODE:
|
---|
| 99 | case Html2PdfException::ERROR_CODE:
|
---|
| 100 | default:
|
---|
| 101 | break;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | return $data;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /**
|
---|
| 108 | * Build the text message
|
---|
| 109 | *
|
---|
| 110 | * @param Html2PdfException $e the exception of the error
|
---|
| 111 | * @param array $data additionnal data
|
---|
| 112 | *
|
---|
| 113 | * @return void
|
---|
| 114 | */
|
---|
| 115 | protected function buildTextMessage(Html2PdfException $e, $data)
|
---|
| 116 | {
|
---|
| 117 | $this->message = 'Html2Pdf Error ['.$e->getCode().']'."\n";
|
---|
| 118 | $this->message.= $e->getMessage()."\n";
|
---|
| 119 | $this->message.= ' File: '.$e->getFile()."\n";
|
---|
| 120 | $this->message.= ' Line: '.$e->getLine()."\n";
|
---|
| 121 |
|
---|
| 122 | if (!empty($data)) {
|
---|
| 123 | foreach ($data as $key => $value) {
|
---|
| 124 | $this->message .= ' '.ucwords($key).': '.trim($value)."\n";
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | /**
|
---|
| 130 | * build the html message
|
---|
| 131 | *
|
---|
| 132 | * @param Html2PdfException $e the exception of the error
|
---|
| 133 | * @param array $data additional data
|
---|
| 134 | *
|
---|
| 135 | * @return void
|
---|
| 136 | */
|
---|
| 137 | protected function buildHtmlMessage(Html2PdfException $e, $data)
|
---|
| 138 | {
|
---|
| 139 | $this->htmlMessage = '<span style="color: #A00; font-weight: bold;">';
|
---|
| 140 | $this->htmlMessage.= 'Html2Pdf Error ['.$e->getCode().']';
|
---|
| 141 | $this->htmlMessage.= '</span><br />'."\n";
|
---|
| 142 | $this->htmlMessage.= htmlentities($e->getMessage())."<br />\n";
|
---|
| 143 | $this->htmlMessage.= ' File: '.$e->getFile()."<br />\n";
|
---|
| 144 | $this->htmlMessage.= ' Line: '.$e->getLine()."<br />\n";
|
---|
| 145 |
|
---|
| 146 | if (!empty($data)) {
|
---|
| 147 | foreach ($data as $key => $value) {
|
---|
| 148 | $this->htmlMessage .= ' '.ucwords($key).': '.trim(htmlentities($value))."<br />\n";
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | }
|
---|