1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oqq\Minc\Log; |
4
|
|
|
|
5
|
|
|
use Psr\Log\InvalidArgumentException; |
6
|
|
|
use Psr\Log\LogLevel; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Eric Braun <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class Level |
12
|
|
|
{ |
13
|
|
|
const DEFAULT_VALUE = self::DEBUG; |
14
|
|
|
|
15
|
|
|
const DEBUG = 100; |
16
|
|
|
const INFO = 200; |
17
|
|
|
const NOTICE = 300; |
18
|
|
|
const WARNING = 400; |
19
|
|
|
const ERROR = 500; |
20
|
|
|
const CRITICAL = 600; |
21
|
|
|
const ALERT = 700; |
22
|
|
|
const EMERGENCY = 800; |
23
|
|
|
|
24
|
|
|
/** @var array */ |
25
|
|
|
public static $names = [ |
26
|
|
|
self::DEBUG => LogLevel::DEBUG, |
27
|
|
|
self::INFO => LogLevel::INFO, |
28
|
|
|
self::NOTICE => LogLevel::NOTICE, |
29
|
|
|
self::WARNING => LogLevel::WARNING, |
30
|
|
|
self::ERROR => LogLevel::ERROR, |
31
|
|
|
self::CRITICAL => LogLevel::CRITICAL, |
32
|
|
|
self::ALERT => LogLevel::ALERT, |
33
|
|
|
self::EMERGENCY => LogLevel::EMERGENCY, |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** @var int */ |
37
|
|
|
protected $level; |
38
|
|
|
|
39
|
|
|
/** @var string */ |
40
|
|
|
protected $name; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param mixed $value |
44
|
|
|
*/ |
45
|
32 |
|
public function __construct($value = self::DEFAULT_VALUE) |
46
|
|
|
{ |
47
|
32 |
|
$this->value = $this->hasValue($value) ? $value : self::DEFAULT_VALUE; |
|
|
|
|
48
|
32 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
1 |
|
public function __toString() |
54
|
|
|
{ |
55
|
1 |
|
return sprintf('%s (%s)', $this->getName(), $this->getValue()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $name |
60
|
|
|
* |
61
|
|
|
* @return Level |
62
|
|
|
* @throws InvalidArgumentException |
63
|
|
|
*/ |
64
|
13 |
|
public static function createFromName($name) |
65
|
|
|
{ |
66
|
13 |
|
$value = array_search($name, static::$names, true); |
67
|
|
|
|
68
|
13 |
|
if (!$value) { |
69
|
1 |
|
throw new InvalidArgumentException(sprintf( |
70
|
1 |
|
'Level with name "%s" is not in list: %s', |
71
|
1 |
|
$name, |
72
|
1 |
|
implode(', ', static::$names) |
73
|
1 |
|
)); |
74
|
|
|
} |
75
|
|
|
|
76
|
12 |
|
return new static($value); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return int |
81
|
|
|
*/ |
82
|
30 |
|
public function getValue() |
83
|
|
|
{ |
84
|
30 |
|
return $this->value; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
29 |
|
public function getName() |
91
|
|
|
{ |
92
|
29 |
|
if (null === $this->name) { |
93
|
29 |
|
$this->name = static::$names[$this->value]; |
94
|
29 |
|
} |
95
|
|
|
|
96
|
29 |
|
return $this->name; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param mixed $value |
101
|
|
|
* |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
32 |
|
protected function hasValue($value) |
105
|
|
|
{ |
106
|
32 |
|
$value = (int) $value; |
107
|
|
|
|
108
|
32 |
|
return isset(static::$names[$value]) || array_key_exists($value, static::$names); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: