1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HtImgModule\View\Helper; |
4
|
|
|
|
5
|
|
|
use Zend\View\Helper\AbstractHtmlElement; |
6
|
|
|
|
7
|
|
|
class DisplayImage extends AbstractHtmlElement |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Attributes for HTML image tag |
11
|
|
|
* |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $attributes; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Gets valid HTML image tag or self when $relativeName is not provided |
18
|
|
|
* |
19
|
|
|
* @param string $relativeName Relative Path |
20
|
|
|
* @param string $filter Filter Service |
21
|
|
|
* @param array $attributes Attributes for HTML image tag |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
1 |
|
public function __invoke($relativeName = null, $filter = null, $attributes = null) |
25
|
|
|
{ |
26
|
1 |
|
if ($relativeName === null) { |
27
|
1 |
|
return $this; |
28
|
|
|
} |
29
|
1 |
|
if ($attributes !== null) { |
30
|
|
|
$this->setAttributes($attributes); |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
return $this->getImgTag($relativeName, $filter); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Return valid HTML image tag |
38
|
|
|
* |
39
|
|
|
* @param string $relativeName |
40
|
|
|
* @param string $filter |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
1 |
|
public function getImgTag($relativeName, $filter) |
44
|
|
|
{ |
45
|
1 |
|
$imgUrlHelper = $this->getView()->plugin('HtImgModule\View\Helper\ImgUrl'); |
|
|
|
|
46
|
1 |
|
$this->attributes['src'] = $imgUrlHelper($relativeName, $filter);; |
47
|
|
|
$html = '<img' |
48
|
1 |
|
. $this->htmlAttribs($this->getAttributes()) |
49
|
1 |
|
. $this->getClosingBracket(); |
50
|
|
|
|
51
|
1 |
|
return $html; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Sets attributes of HTML image tag |
56
|
|
|
* |
57
|
|
|
* @param array $attributes |
58
|
|
|
* @return self |
59
|
|
|
*/ |
60
|
2 |
|
public function setAttributes(array $attributes) |
61
|
|
|
{ |
62
|
2 |
|
$this->attributes = $attributes; |
63
|
|
|
|
64
|
2 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Gets attributes of img tag |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
2 |
|
public function getAttributes() |
73
|
|
|
{ |
74
|
2 |
|
return $this->attributes; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: