示例#1
0
文件: Username.php 项目: coolms/user
 /**
  * __invoke
  *
  * @access public
  * @param UserInterface $user
  * @throws DomainException
  * @return string
  */
 public function __invoke(UserInterface $user = null)
 {
     if (null === $user) {
         if (!method_exists($this->getView(), 'plugin')) {
             return;
         }
         if (!($plugin = $this->getView()->plugin('cmsIdentity'))) {
             return;
         }
         if (!($user = $plugin())) {
             return 'guest';
         }
         if (!$user instanceof UserInterface) {
             throw new DomainException(sprintf('$user is not an instance of %s', UserInterface::class), 500);
         }
     }
     $username = null;
     if ($this->options->getEnableUsername()) {
         $username = $user->getUsername();
     }
     if (null === $username) {
         $username = $user->getEmail();
     }
     return $username;
 }
示例#2
0
 /**
  * __invoke
  *
  * @access public
  * @param UserInterface $user
  * @throws DomainException
  * @return string
  */
 public function __invoke(UserInterface $user = null)
 {
     if (null === $user) {
         if (!method_exists($this->getView(), 'plugin')) {
             return;
         }
         if (!($plugin = $this->getView()->plugin('cmsIdentity'))) {
             return;
         }
         if (!($user = $plugin())) {
             return $this->getView()->translate('Guest', strstr(__NAMESPACE__, '\\', true));
         }
         if (!$user instanceof UserInterface) {
             throw new DomainException(sprintf('$user is not an instance of %s', UserInterface::class), 500);
         }
     }
     $displayName = null;
     if ($this->options->getEnableDisplayName()) {
         $displayName = $user->getDisplayName();
     }
     if (null === $displayName && $this->options->getEnableUsername()) {
         $displayName = $user->getUsername();
     }
     if (null === $displayName) {
         $displayName = $user->getEmail();
         $displayName = substr($displayName, 0, strpos($displayName, '@'));
     }
     return $displayName;
 }