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