FontAwesome5Icon.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Tools\View\Icon;
  3. use Tools\View\Icon\Collector\FontAwesome5IconCollector;
  4. class FontAwesome5Icon extends AbstractIcon {
  5. /**
  6. * @param array<string, mixed> $config
  7. */
  8. public function __construct(array $config = []) {
  9. $config += [
  10. 'template' => '<span class="{{class}}"{{attributes}}></span>',
  11. 'namespace' => 'fas',
  12. ];
  13. parent::__construct($config);
  14. }
  15. /**
  16. * @return array<string>
  17. */
  18. public function names(): array {
  19. $path = $this->path();
  20. return FontAwesome5IconCollector::collect($path);
  21. }
  22. /**
  23. * @param string $icon
  24. * @param array $options
  25. * @param array $attributes
  26. *
  27. * @return string
  28. */
  29. public function render(string $icon, array $options = [], array $attributes = []): string {
  30. if (!empty($this->config['attributes'])) {
  31. $attributes += $this->config['attributes'];
  32. }
  33. // Shimming
  34. if (isset($options['title'])) {
  35. $attributes['title'] = $options['title'];
  36. unset($options['title']);
  37. }
  38. $class = [
  39. $this->config['namespace'],
  40. ];
  41. if (!empty($options['extra'])) {
  42. foreach ($options['extra'] as $i) {
  43. $class[] = 'fa-' . $i;
  44. }
  45. }
  46. if (!empty($options['size'])) {
  47. $class[] = 'fa-' . ($options['size'] === 'large' ? 'large' : $options['size'] . 'x');
  48. }
  49. if (!empty($options['pull'])) {
  50. $class[] = 'pull-' . $options['pull'];
  51. }
  52. if (!empty($options['rotate'])) {
  53. $class[] = 'fa-rotate-' . (int)$options['rotate'];
  54. }
  55. if (!empty($options['spin'])) {
  56. $class[] = 'fa-spin';
  57. }
  58. $options['class'] = implode(' ', $class) . ' ' . 'fa-' . $icon;
  59. if (!empty($attributes['class'])) {
  60. $options['class'] .= ' ' . $attributes['class'];
  61. }
  62. $options['attributes'] = $this->template->formatAttributes($attributes, ['class']);
  63. return $this->template->format('icon', $options);
  64. }
  65. }