]> BookStack Code Mirror - bookstack/blob - app/Actions/TagClassGenerator.php
Rolled out reference link updating logic usage
[bookstack] / app / Actions / TagClassGenerator.php
1 <?php
2
3 namespace BookStack\Actions;
4
5 class TagClassGenerator
6 {
7     protected array $tags;
8
9     /**
10      * @param Tag[] $tags
11      */
12     public function __construct(array $tags)
13     {
14         $this->tags = $tags;
15     }
16
17     /**
18      * @return string[]
19      */
20     public function generate(): array
21     {
22         $classes = [];
23
24         foreach ($this->tags as $tag) {
25             $name = $this->normalizeTagClassString($tag->name);
26             $value = $this->normalizeTagClassString($tag->value);
27             $classes[] = 'tag-name-' . $name;
28             if ($value) {
29                 $classes[] = 'tag-value-' . $value;
30                 $classes[] = 'tag-pair-' . $name . '-' . $value;
31             }
32         }
33
34         return array_unique($classes);
35     }
36
37     public function generateAsString(): string
38     {
39         return implode(' ', $this->generate());
40     }
41
42     protected function normalizeTagClassString(string $value): string
43     {
44         $value = str_replace(' ', '', strtolower($value));
45         $value = str_replace('-', '', strtolower($value));
46
47         return $value;
48     }
49 }