1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
7
|
|
|
* @license MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** Core view helpers */ |
11
|
|
|
namespace Core\View\Helper; |
12
|
|
|
|
13
|
|
|
use Zend\View\Helper\AbstractHelper; |
14
|
|
|
use Auth\Entity\Info; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This helper generates HTML email link markup from a user info entity. |
18
|
|
|
* |
19
|
|
|
* <code> |
20
|
|
|
* |
21
|
|
|
* $this->contactLink($user->getInfo()); |
22
|
|
|
* // Outputs: <a href="mailto:[email protected]">Fancy Name</a> |
23
|
|
|
* |
24
|
|
|
* $this->contactLink($user->getInfo()); |
25
|
|
|
* // Outputs: <a href="mailto:[email protected]">[email protected]</a> |
26
|
|
|
* |
27
|
|
|
* $this->contactLink($user->getInfo()); |
28
|
|
|
* // Outputs: Fancy Name |
29
|
|
|
* </code> |
30
|
|
|
* |
31
|
|
|
*/ |
32
|
|
|
class ContactLink extends AbstractHelper |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* generates an email link from a user info entity |
37
|
|
|
* |
38
|
|
|
* @param \Auth\Entity\Info $userInfo |
39
|
|
|
* @param array $attributes |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function __invoke(Info $userInfo, array $attributes = array()) |
43
|
|
|
{ |
44
|
|
|
$email = $userInfo->getEmail(); |
45
|
|
|
$displayName = $userInfo->getDisplayName(false); |
46
|
|
|
|
47
|
|
|
if ($email) { |
48
|
|
|
$label = $displayName |
49
|
|
|
? $displayName |
50
|
|
|
: $email; |
51
|
|
|
|
52
|
|
|
$attributesStr = $attributes |
53
|
|
|
? $this->createAttributesString($attributes) |
54
|
|
|
: ''; |
55
|
|
|
|
56
|
|
|
return sprintf('<a %s href="mailto:%s">%s</a>', $attributesStr, $email, $label); |
57
|
|
|
} else { |
58
|
|
|
return $displayName |
59
|
|
|
? $displayName |
60
|
|
|
: ''; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
protected function createAttributesString(array $attributes) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$renderer = $this->getView(); |
67
|
|
|
$escape = $renderer->plugin('escapehtml'); |
68
|
|
|
$escapeAttr = $renderer->plugin('escapehtmlattr'); |
69
|
|
|
$attr = array(); |
70
|
|
|
|
71
|
|
|
foreach ($attributes as $name => $value) { |
72
|
|
|
$attr[] = $escape($name) . (strlen($value) ? ('="' . $escapeAttr($value) . '"') : ''); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return implode(' ', $attr); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.