From 33fcd6a92c6d91f2d7d80736fafddfca93bcc441 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Sat, 25 Feb 2023 14:49:06 +0100 Subject: [PATCH] User: add __toString() magic method --- src/Auth/User.php | 10 ++++++++++ src/Log.php | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Auth/User.php b/src/Auth/User.php index d0fd95d..882e69e 100644 --- a/src/Auth/User.php +++ b/src/Auth/User.php @@ -83,4 +83,14 @@ class User { ); } + /** + * Magic method to allow pretty cast to string + * @return string + */ + public function __toString() { + if (isset($this -> name)) + return sprintf('%s (%s)', $this -> name, $this -> username); + return $this -> namename; + } + } diff --git a/src/Log.php b/src/Log.php index 2ed71a2..fc98729 100644 --- a/src/Log.php +++ b/src/Log.php @@ -163,7 +163,7 @@ class Log { $_SERVER['REMOTE_ADDR'], ); if (Auth::enabled()) - $msg[] = (Auth::user()?Auth::user()->username:'anonymous'); + $msg[] = (Auth::user()?Auth::user():'anonymous'); $msg[] = $level; $msg[] = $message; $msg = implode(' - ', $msg)."\n";