1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ZfcUser\View\Helper; |
4
|
|
|
|
5
|
|
|
use Zend\Authentication\AuthenticationService; |
6
|
|
|
use Zend\View\Helper\AbstractHelper; |
7
|
|
|
use ZfcUser\Entity\UserInterface; |
8
|
|
|
use ZfcUser\Exception; |
9
|
|
|
|
10
|
|
|
class ZfcUserDisplayName extends AbstractHelper |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var AuthenticationService |
14
|
|
|
*/ |
15
|
|
|
protected $authService; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* __invoke |
19
|
|
|
* |
20
|
|
|
* @access public |
21
|
|
|
* @param UserInterface $user |
22
|
|
|
* @throws Exception\DomainException |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
|
|
public function __invoke(UserInterface $user = null) |
26
|
|
|
{ |
27
|
|
|
if (null === $user) { |
28
|
|
|
if ($this->getAuthService()->hasIdentity()) { |
29
|
|
|
$user = $this->getAuthService()->getIdentity(); |
30
|
|
|
if (!$user instanceof UserInterface) { |
31
|
|
|
throw new Exception\DomainException( |
32
|
|
|
'$user is not an instance of User', |
33
|
|
|
500 |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
} else { |
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$displayName = $user->getDisplayName(); |
42
|
|
|
if (null === $displayName) { |
43
|
|
|
$displayName = $user->getUsername(); |
44
|
|
|
} |
45
|
|
|
// User will always have an email, so we do not have to throw error |
46
|
|
|
if (null === $displayName) { |
47
|
|
|
$displayName = $user->getEmail(); |
48
|
|
|
$displayName = substr($displayName, 0, strpos($displayName, '@')); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $displayName; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get authService. |
56
|
|
|
* |
57
|
|
|
* @return AuthenticationService |
58
|
|
|
*/ |
59
|
|
|
public function getAuthService() |
60
|
|
|
{ |
61
|
|
|
return $this->authService; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set authService. |
66
|
|
|
* |
67
|
|
|
* @param AuthenticationService $authService |
68
|
|
|
* @return self |
69
|
|
|
*/ |
70
|
|
|
public function setAuthService(AuthenticationService $authService) |
71
|
|
|
{ |
72
|
|
|
$this->authService = $authService; |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|