-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErrorTypesParser.php
83 lines (72 loc) · 2.08 KB
/
ErrorTypesParser.php
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
<?php
/*
* This file is part of SentryBundle.
* (c) 2017 Maxim Falaleev
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mindy\Bundle\SentryBundle;
/**
* Evaluate an error types expression.
*/
class ErrorTypesParser
{
private $expression = null;
/**
* Initialize ErrorParser
*
* @param string $expression Error Types e.g. E_ALL & ~E_DEPRECATED & ~E_NOTICE
*/
public function __construct($expression)
{
$this->expression = $expression;
}
/**
* Parse and compute the error types expression
*
* @return int the parsed expression
*/
public function parse()
{
// convert constants to ints
$this->expression = $this->convertErrorConstants($this->expression);
$this->expression = str_replace(
[',', ' '],
['.', ''],
$this->expression
);
// remove anything which could be a security issue
$this->expression = preg_replace("/[^\d.+*%^|&~<>\/()-]/", '', $this->expression);
return $this->compute($this->expression);
}
/**
* Converts error constants from string to int.
*
* @param string $expression e.g. E_ALL & ~E_DEPRECATED & ~E_NOTICE
*
* @return string convertes expression e.g. 32767 & ~8192 & ~8
*/
private function convertErrorConstants($expression)
{
$output = preg_replace_callback('/(E_[a-zA-Z_]+)/', function ($errorConstant) {
if (defined($errorConstant[1])) {
return constant($errorConstant[1]);
}
return $errorConstant[0];
}, $expression);
return $output;
}
/**
* Let PHP compute the prepared expression for us.
*
* @param string $expression prepared expression e.g. 32767&~8192&~8
*
* @return int computed expression e.g. 24567
*/
private function compute($expression)
{
$compute = create_function('', 'return '.$expression.';');
return 0 + $compute();
}
}