Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| IconBuilderTrait | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
| buildIcon | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2014 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | namespace oat\tao\model\iconBuilder; |
| 22 | |
| 23 | use DOMDocument; |
| 24 | |
| 25 | trait IconBuilderTrait |
| 26 | { |
| 27 | /** |
| 28 | * This function builds the actual HTML element and is used by all other functions. |
| 29 | * The doc for $options is the applicable for all other functions. |
| 30 | * |
| 31 | * @param string $iconName name of the icon to display |
| 32 | * @param array $options (optional) hashtable with HTML attributes, also allows to set |
| 33 | * element="almostAnyHtmlElement" |
| 34 | * |
| 35 | * @return string HTML element with icon |
| 36 | */ |
| 37 | protected static function buildIcon(string $iconName, array $options = []) |
| 38 | { |
| 39 | $dom = new DOMDocument(); |
| 40 | |
| 41 | $element = $options['element'] ?? 'span'; |
| 42 | $icon = $options['icon'] ?? ''; |
| 43 | $options['class'] = trim(implode(' ', [$icon, $iconName])); |
| 44 | unset($options['element']); // remove element from options as others should be mapped to html attributes |
| 45 | |
| 46 | $element = $dom->createElement($element); |
| 47 | foreach ($options as $key => $value) { |
| 48 | $element->setAttribute($key, $value); |
| 49 | } |
| 50 | |
| 51 | return $dom->saveHTML($element); |
| 52 | } |
| 53 | } |