Reputation: 19386
For a custom logger I want to force the caller to pass a valid class constant defined in Psr\Log\LogLevel
.
This class is defined like:
namespace Psr\Log;
/**
* Describes log levels.
*/
class LogLevel
{
const EMERGENCY = 'emergency';
const ALERT = 'alert';
const CRITICAL = 'critical';
const ERROR = 'error';
const WARNING = 'warning';
const NOTICE = 'notice';
const INFO = 'info';
const DEBUG = 'debug';
}
The loggers' function (wrong) looks like:
public static function log($log, LogLevel $logLevel = null): void {
// .....
}
This does not work because LogLevel::DEBUG for instance is a string and not an instance of the class. Is there any way to enforce a class constant in a PHP type declaration? Because if I define string
then you can pass any string obviously, but I just want to allow the declared constants.
Upvotes: 1
Views: 583
Reputation: 478
PHP doesn't have constant restrictions, only types.
But you can do a workaround like this:
class LogLevel
{
protected string $logName;
private function __construct(string $logName)
{
$this->logName = $logName;
}
public function getName(): string
{
return $this->logName;
}
public static function emergency(): self
{
return new self('emergency');
}
public static function alert(): self
{
return new self('alert');
}
public static function critical(): self
{
return new self('critical');
}
// create the other constants here
}
Now your static funcion works
public static function log($log, LogLevel $logLevel = null): void {
// .....
}
$logLevel
will receive LogLevel::emergency()
, LogLevel::critical()
, etc. and you can get the level name just calling $logLevel->getName()
Upvotes: 2
Reputation: 1827
There is not an easy way but you could check if the inserted string matches certain values
function checkLog(string $logLevel){
$acceptableValues = array("emergency", "error", "debug");
if(!in_array($logLevel, $acceptableValues) die();
}
Replace the strings in the array with the consts of your class.
Upvotes: 0
Reputation: 1547
If you have Phpstorm you could use new #[ExpectedValues]
attribute feature: https://blog.jetbrains.com/phpstorm/2020/10/phpstorm-2020-3-eap-4/#expectedvalues_examples
Upvotes: 0