-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathError.php
More file actions
122 lines (112 loc) · 2.77 KB
/
Error.php
File metadata and controls
122 lines (112 loc) · 2.77 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
<?php
/**
* @package axy\ml
* @author Oleg Grigoriev <go.vasac@gmail.com>
*/
namespace axy\ml;
use axy\magic\LazyField;
use axy\magic\ReadOnly;
/**
* The item of parsing error
*
* @property-read string $code
* @property-read int $line
* @property-read string $message
* @property-read array $data
*/
class Error
{
use LazyField;
use ReadOnly;
const TAG_UNKNOWN = 'tag_unknown';
const TAG_NOT_CLOSED = 'tag_not_closed';
const TAG_INVALID = 'tag_invalid';
const HEADER_EMPTY = 'header_empty';
const META_EMPTY = 'meta_empty';
/**
* The constructor
*
* @param string $code
* @param int $line [optional]
* @param array $data [optional]
*/
public function __construct($code, $line = null, array $data = [])
{
$this->magicInit();
$this->magicFields['fields'] = [
'code' => $code,
'line' => $line,
'data' => $data,
];
}
/**
* {@inheritdoc}
*/
public function __toString()
{
$message = $this->__get('message');
$line = $this->magicFields['fields']['line'];
if ($line !== null) {
$message .= ' on line '.$line;
}
return $message;
}
/**
* Sorts the errors list by number of lines
*
* @param array $errors
* @return array
*/
public static function sortListByLine(array $errors)
{
$cmp = function ($a, $b) {
if ($a->line > $b->line) {
return 1;
} elseif ($a->line < $b->line) {
return -1;
}
return 0;
};
usort($errors, $cmp);
return $errors;
}
/**
* {@inheritdoc}
*/
protected $magicDefaults = [
'loaders' => [
'message' => '::createMessage'
],
];
/**
* @return string
*/
protected function createMessage()
{
$code = $this->magicFields['fields']['code'];
$data = $this->magicFields['fields']['data'];
if (isset($this->messages[$code])) {
$tpl = $this->messages[$code];
} else {
$tpl = $this->messages[''];
}
$callback = function ($m) use ($data) {
$m = trim($m[1]);
return isset($data[$m]) ? $data[$m] : '';
};
return preg_replace_callback('/{{(.*?)}}/', $callback, $tpl);
}
/**
* Message templates
*
* @var array
*/
private $messages = [
self::TAG_UNKNOWN => 'Unknown tag [{{tag}}]',
self::TAG_NOT_CLOSED => 'Tag [{{tag}}] is not closed',
self::TAG_INVALID => 'Invalid [{{tag}}]: {{info}}',
self::HEADER_EMPTY => 'Header is empty',
self::META_EMPTY => 'Meta is empty',
'' => 'Unknown error',
];
}