1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (C) 2015 Michael Herold <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace hemio\form; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Description of Document |
23
|
|
|
* |
24
|
|
|
* @author Michael Herold <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class Document extends \hemio\html\Document |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
public function __construct(\hemio\html\Interface_\ContentModelText $objTitleContent) |
30
|
|
|
{ |
31
|
|
|
parent::__construct($objTitleContent); |
32
|
|
|
|
33
|
|
|
$this->addHookToString( |
34
|
|
|
function (\hemio\html\Interface_\HtmlCode $elem) { |
35
|
|
|
$filter = function ($elem) { |
36
|
|
|
return $elem instanceof \hemio\form\Focusable; |
37
|
|
|
}; |
38
|
|
|
|
39
|
|
|
$curLevel = 0; |
40
|
|
|
$curFocusable = null; |
41
|
|
|
foreach ($elem->getRecursiveIterator($filter) as $focusable) { |
|
|
|
|
42
|
|
|
if ($focusable->getAutofocusLevel() > $curLevel) { |
43
|
|
|
$curFocusable = $focusable; |
44
|
|
|
$curLevel = $focusable->getAutofocusLevel(); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ($curFocusable !== null) |
49
|
|
|
$curFocusable->engageAutofocus(); |
50
|
|
|
} |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
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: