diff --git a/README.md b/README.md index 54d38c6..cf8953b 100644 --- a/README.md +++ b/README.md @@ -237,7 +237,7 @@ abstract class Handler { protected array $levels; protected bool $_bubbles = true; - protected string|\Closure|null $_messageTransform = null; + protected callable $_messageTransform = null; protected string $_timeFormat = 'M d H:i:s'; public function __construct(array $levels = [ 0, 1, 2, 3, 4, 5, 6, 7 ], array $options = []); diff --git a/lib/Logger/Handler.php b/lib/Logger/Handler.php index e09a2e9..0ddd6b0 100644 --- a/lib/Logger/Handler.php +++ b/lib/Logger/Handler.php @@ -12,7 +12,7 @@ abstract class Handler { protected array $levels; protected bool $_bubbles = true; - protected string|\Closure|null $_messageTransform = null; + protected $_messageTransform = null; protected string $_timeFormat = 'M d H:i:s'; @@ -20,14 +20,8 @@ abstract class Handler { public function __construct(array $levels = [ 0, 1, 2, 3, 4, 5, 6, 7 ], array $options = []) { $this->levels = $this->verifyLevels($levels); - $class = get_class($this); foreach ($options as $key => $value) { - $name = "_$key"; - if (!property_exists($class, $name)) { - trigger_error(sprintf('Undefined option in %s: %s', $class, $key), \E_USER_WARNING); - continue; - } - $this->$name = $value; + $this->setOption($key, $value); } } @@ -59,6 +53,10 @@ abstract class Handler { return; } + if ($name === 'messageTransform' && !is_callable($value)) { + throw new TypeError(sprintf('Value of messageTransform option must be callable')); + } + $name = "_$name"; $this->$name = $value; } diff --git a/lib/Logger/TypeError.php b/lib/Logger/TypeError.php new file mode 100644 index 0000000..a18ec0b --- /dev/null +++ b/lib/Logger/TypeError.php @@ -0,0 +1,11 @@ +setOption('messageTransform', 42); + } + ], [ InvalidArgumentException::class, 0,