1 | <?php |
||
16 | class Autocomplete extends SimpleComponent { |
||
17 | |||
18 | public function __construct(JsUtils $js) { |
||
23 | |||
24 | /** |
||
25 | * Define source property with an ajax request based on $url |
||
26 | * $url must return a JSON array of values |
||
27 | * @param String $url |
||
28 | * @return $this |
||
29 | */ |
||
30 | public function setAjaxSource($url) { |
||
45 | |||
46 | /** |
||
47 | * Define the source property |
||
48 | * with a JSON Array of values |
||
49 | * Example : ["Bordeaux","Alsace","Bourgogne"] |
||
50 | * Example : [{value : "BO", label : "Bordeaux"}, {value : "AL", label : "Alsace"}, {value : "BOU", label : "Bourgogne"}] |
||
51 | * @param String $source |
||
52 | * @return $this |
||
53 | */ |
||
54 | public function setSource($source) { |
||
61 | |||
62 | /** |
||
63 | * If set to true the first item will automatically be focused when the menu is shown. |
||
64 | * default : false |
||
65 | * @param Boolean $value |
||
66 | * @return $this |
||
67 | */ |
||
68 | public function setAutofocus($value) { |
||
71 | |||
72 | /** |
||
73 | * The delay in milliseconds between when a keystroke occurs and when a search is performed. |
||
74 | * A zero-delay makes sense for local data (more responsive), but can produce a lot of load for remote data, |
||
75 | * while being less responsive. |
||
76 | * default : 300 |
||
77 | * @param int $value |
||
78 | * @return $this |
||
79 | */ |
||
80 | public function setDelay($value) { |
||
83 | |||
84 | /** |
||
85 | * Disables the autocomplete if set to true. |
||
86 | * @param Boolean $value default : false |
||
87 | * @return $this |
||
88 | */ |
||
89 | public function setDisabled($value) { |
||
92 | |||
93 | /** |
||
94 | * The minimum number of characters a user must type before a search is performed. |
||
95 | * Zero is useful for local data with just a few items, |
||
96 | * but a higher value should be used when a single character search could match a few thousand items. |
||
97 | * @param int $value default : 1 |
||
98 | * @return $this |
||
99 | */ |
||
100 | public function setMinLength($value) { |
||
103 | |||
104 | /** |
||
105 | * Identifies the position of the suggestions menu in relation to the associated input element. |
||
106 | * The of option defaults to the input element, but you can specify another element to position against. |
||
107 | * You can refer to the jQuery UI Position utility for more details about the various options. |
||
108 | * @param int $position default : { my: "left top", at: "left bottom", collision: "none" } |
||
109 | * @return $this |
||
110 | */ |
||
111 | public function setPosition(Position $position) { |
||
114 | |||
115 | /** |
||
116 | * Triggered when the field is blurred, if the value has changed. |
||
117 | * @param string $jsCode |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function onChange($jsCode) { |
||
123 | |||
124 | /** |
||
125 | * Triggered when the menu is hidden. |
||
126 | * Not every close event will be accompanied by a change event. |
||
127 | * @param string $jsCode |
||
128 | * @return $this |
||
129 | */ |
||
130 | public function onClose($jsCode) { |
||
133 | |||
134 | /** |
||
135 | * Triggered when focus is moved to an item (not selecting). |
||
136 | * The default action is to replace the text field's value with the value of the focused item, |
||
137 | * though only if the event was triggered by a keyboard interaction. |
||
138 | * Canceling this event prevents the value from being updated, but does not prevent the menu item from being focused. |
||
139 | * @param string $jsCode |
||
140 | * @return $this |
||
141 | */ |
||
142 | public function onFocus($jsCode) { |
||
145 | |||
146 | /** |
||
147 | * Triggered when the suggestion menu is opened or updated. |
||
148 | * @param string $jsCode |
||
149 | * @return $this |
||
150 | */ |
||
151 | public function onOpen($jsCode) { |
||
154 | |||
155 | /** |
||
156 | * Triggered after a search completes, before the menu is shown. |
||
157 | * Useful for local manipulation of suggestion data, where a custom source option callback is not required. |
||
158 | * This event is always triggered when a search completes, even if the menu will not be shown because there are no results or the Autocomplete is disabled. |
||
159 | * @param string $jsCode |
||
160 | * @return $this |
||
161 | */ |
||
162 | public function onResponse($jsCode) { |
||
165 | |||
166 | /** |
||
167 | * Triggered before a search is performed, after minLength and delay are met. |
||
168 | * If canceled, then no request will be started and no items suggested. |
||
169 | * @param string $jsCode |
||
170 | * @return $this |
||
171 | */ |
||
172 | public function onSearch($jsCode) { |
||
175 | |||
176 | /** |
||
177 | * Triggered when an item is selected from the menu. |
||
178 | * The default action is to replace the text field's value with the value of the selected item. |
||
179 | * Canceling this event prevents the value from being updated, but does not prevent the menu from closing. |
||
180 | * @param string $jsCode |
||
181 | * @return $this |
||
182 | */ |
||
183 | public function onSelect($jsCode) { |
||
186 | } |
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 sub-classes 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 parent class: