MaterialIcon.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Tools\View\Icon;
  3. use Tools\View\Icon\Collector\MaterialIconCollector;
  4. class MaterialIcon extends AbstractIcon {
  5. /**
  6. * @param array<string, mixed> $config
  7. */
  8. public function __construct(array $config = []) {
  9. $config += [
  10. 'template' => '<span class="{{class}}"{{attributes}}>{{name}}</span>',
  11. 'namespace' => 'material-icons',
  12. ];
  13. parent::__construct($config);
  14. }
  15. /**
  16. * @return array<string>
  17. */
  18. public function names(): array {
  19. $path = $this->path();
  20. return MaterialIconCollector::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. $formatOptions = $attributes + [
  31. ];
  32. $options['name'] = $icon;
  33. $options['class'] = $this->config['namespace'];
  34. if (!empty($attributes['class'])) {
  35. $options['class'] .= ' ' . $attributes['class'];
  36. }
  37. $options['attributes'] = $this->template->formatAttributes($formatOptions, ['class']);
  38. return $this->template->format('icon', $options);
  39. }
  40. }